ChipMaster's bwBASIC This also includes history going back to v2.10. *WARN* some binary files might have been corrupted by CRLF.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2046 lines
45 KiB

  1. /****************************************************************
  2. bwb_mth.c Mathematical Functions
  3. for Bywater BASIC Interpreter
  4. Copyright (c) 1993, Ted A. Campbell
  5. Bywater Software
  6. email: tcamp@delphi.com
  7. Copyright and Permissions Information:
  8. All U.S. and international rights are claimed by the author,
  9. Ted A. Campbell.
  10. This software is released under the terms of the GNU General
  11. Public License (GPL), which is distributed with this software
  12. in the file "COPYING". The GPL specifies the terms under
  13. which users may copy and use the software in this distribution.
  14. A separate license is available for commercial distribution,
  15. for information on which you should contact the author.
  16. ****************************************************************/
  17. /*---------------------------------------------------------------*/
  18. /* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, */
  19. /* 11/1995 (eidetics@cerf.net). */
  20. /*---------------------------------------------------------------*/
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <math.h>
  24. #include <time.h>
  25. #include "bwbasic.h"
  26. #include "bwb_mes.h"
  27. #ifndef RAND_MAX /* added in v1.11 */
  28. #define RAND_MAX 32767
  29. #endif
  30. #if ANSI_C
  31. bnumber round_int( bnumber x );
  32. #else
  33. bnumber round_int();
  34. #endif
  35. #if MS_FUNCS
  36. union un_integer
  37. {
  38. int the_integer;
  39. unsigned char the_chars[ sizeof( int ) ];
  40. } an_integer;
  41. union un_single
  42. {
  43. float the_float;
  44. unsigned char the_chars[ sizeof( float) ];
  45. } a_float;
  46. union un_double
  47. {
  48. double the_double;
  49. unsigned char the_chars[ sizeof( double ) ];
  50. } a_double;
  51. #endif
  52. #if COMPRESS_FUNCS
  53. /***************************************************************
  54. FUNCTION: fnc_core()
  55. DESCRIPTION: This C function implements all core
  56. BASIC functions if COMPRESS_FUNCS is
  57. TRUE. This method saves program space.
  58. ***************************************************************/
  59. #if ANSI_C
  60. struct bwb_variable *
  61. fnc_core( int argc, struct bwb_variable *argv, int unique_id )
  62. #else
  63. struct bwb_variable *
  64. fnc_core( argc, argv, unique_id )
  65. int argc;
  66. struct bwb_variable *argv;
  67. int unique_id;
  68. #endif
  69. {
  70. static struct bwb_variable nvar;
  71. static int init = FALSE;
  72. bnumber nval;
  73. #if INTENSIVE_DEBUG
  74. sprintf( bwb_ebuf, "in fnc_core(): entered function" );
  75. bwb_debug( bwb_ebuf );
  76. #endif
  77. /* initialize the variable if necessary */
  78. if ( init == FALSE )
  79. {
  80. init = TRUE;
  81. strncpy( nvar.name, "(core var)", MAXVARNAMESIZE );
  82. #if INTENSIVE_DEBUG
  83. sprintf( bwb_ebuf, "in fnc_core(): ready to make local variable <%s>",
  84. nvar.name );
  85. bwb_debug( bwb_ebuf );
  86. #endif
  87. var_make( &nvar, NUMBER );
  88. }
  89. #if INTENSIVE_DEBUG
  90. sprintf( bwb_ebuf, "in fnc_core(): received f_arg <%f> nvar type <%c>",
  91. var_getnval( &( argv[ 0 ] ) ), nvar.type );
  92. bwb_debug( bwb_ebuf );
  93. #endif
  94. /* check for number of arguments as appropriate */
  95. switch ( unique_id )
  96. {
  97. case F_RND: /* no arguments necessary for RND */
  98. break;
  99. default:
  100. #if PROG_ERRORS
  101. if ( argc < 1 )
  102. {
  103. sprintf( bwb_ebuf, "Not enough parameters (%d) to core function.",
  104. argc );
  105. bwb_error( bwb_ebuf );
  106. return NULL;
  107. }
  108. else if ( argc > 1 )
  109. {
  110. sprintf( bwb_ebuf, "Too many parameters (%d) to core function.",
  111. argc );
  112. bwb_error( bwb_ebuf );
  113. return NULL;
  114. }
  115. #else
  116. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  117. {
  118. return NULL;
  119. }
  120. #endif
  121. }
  122. /* assign values */
  123. #if INTENSIVE_DEBUG
  124. sprintf( bwb_ebuf, "in fnc_core(): nvar type <%c>; calling findnval()",
  125. nvar.type );
  126. bwb_debug( bwb_ebuf );
  127. #endif
  128. switch( unique_id )
  129. {
  130. case F_ABS:
  131. /* Added double recast here (JBV) */
  132. * var_findnval( &nvar, nvar.array_pos ) =
  133. (bnumber) fabs( (double) var_getnval( &( argv[ 0 ] ) ) );
  134. break;
  135. case F_ATN:
  136. * var_findnval( &nvar, nvar.array_pos )
  137. = (bnumber) atan( (double) var_getnval( &( argv[ 0 ] ) ) );
  138. break;
  139. case F_COS:
  140. * var_findnval( &nvar, nvar.array_pos )
  141. = (bnumber) cos( (double) var_getnval( &( argv[ 0 ] ) ) );
  142. break;
  143. case F_EXP:
  144. /* Added double recast here (JBV) */
  145. * var_findnval( &nvar, nvar.array_pos )
  146. = (bnumber) exp( (double) var_getnval( &( argv[ 0 ] ) ) );
  147. break;
  148. case F_INT:
  149. * var_findnval( &nvar, nvar.array_pos )
  150. = (bnumber) floor( (double) var_getnval( &( argv[ 0 ] ) ) );
  151. break;
  152. case F_LOG:
  153. * var_findnval( &nvar, nvar.array_pos )
  154. = (bnumber) log( (double) var_getnval( &( argv[ 0 ] ) ) );
  155. break;
  156. case F_RND:
  157. /* Added bnumber recast here (JBV) */
  158. * var_findnval( &nvar, nvar.array_pos )
  159. = (bnumber) ( (float) rand() / RAND_MAX );
  160. break;
  161. case F_SGN:
  162. nval = var_getnval( &( argv[ 0 ] ));
  163. if ( nval == (bnumber) 0.0 )
  164. {
  165. * var_findnval( &nvar, nvar.array_pos ) = (bnumber) 0;
  166. }
  167. else if ( nval > (bnumber) 0.0 )
  168. {
  169. * var_findnval( &nvar, nvar.array_pos ) = (bnumber) 1;
  170. }
  171. else
  172. {
  173. * var_findnval( &nvar, nvar.array_pos ) = (bnumber) -1;
  174. }
  175. break;
  176. case F_SIN:
  177. * var_findnval( &nvar, nvar.array_pos )
  178. = (bnumber) sin( (double) var_getnval( &( argv[ 0 ] ) ) );
  179. break;
  180. case F_SQR:
  181. * var_findnval( &nvar, nvar.array_pos )
  182. = (bnumber) sqrt( (double) var_getnval( &( argv[ 0 ] ) ) );
  183. break;
  184. case F_TAN:
  185. * var_findnval( &nvar, nvar.array_pos )
  186. = (bnumber) tan( (double) var_getnval( &( argv[ 0 ] ) ) );
  187. break;
  188. }
  189. return &nvar;
  190. }
  191. #else
  192. /***************************************************************
  193. FUNCTION: fnc_abs()
  194. DESCRIPTION: This C function implements the BASIC
  195. predefined ABS function, returning the
  196. absolute value of the argument.
  197. SYNTAX: ABS( number )
  198. ***************************************************************/
  199. #if ANSI_C
  200. struct bwb_variable *
  201. fnc_abs( int argc, struct bwb_variable *argv, int unique_id )
  202. #else
  203. struct bwb_variable *
  204. fnc_abs( argc, argv, unique_id )
  205. int argc;
  206. struct bwb_variable *argv;
  207. int unique_id;
  208. #endif
  209. {
  210. static struct bwb_variable nvar;
  211. static int init = FALSE;
  212. #if INTENSIVE_DEBUG
  213. sprintf( bwb_ebuf, "in fnc_abs(): entered function" );
  214. bwb_debug( bwb_ebuf );
  215. #endif
  216. /* initialize the variable if necessary */
  217. if ( init == FALSE )
  218. {
  219. init = TRUE;
  220. strncpy( nvar.name, "(abs var)", MAXVARNAMESIZE );
  221. #if INTENSIVE_DEBUG
  222. sprintf( bwb_ebuf, "in fnc_abs(): ready to make local variable <%s>",
  223. nvar.name );
  224. bwb_debug( bwb_ebuf );
  225. #endif
  226. var_make( &nvar, NUMBER );
  227. }
  228. #if INTENSIVE_DEBUG
  229. sprintf( bwb_ebuf, "in fnc_abs(): received f_arg <%f> nvar type <%c>",
  230. var_getnval( &( argv[ 0 ] ) ), nvar.type );
  231. bwb_debug( bwb_ebuf );
  232. #endif
  233. #if PROG_ERRORS
  234. if ( argc < 1 )
  235. {
  236. sprintf( bwb_ebuf, "Not enough parameters (%d) to function ABS().",
  237. argc );
  238. bwb_error( bwb_ebuf );
  239. return NULL;
  240. }
  241. else if ( argc > 1 )
  242. {
  243. sprintf( bwb_ebuf, "Too many parameters (%d) to function ABS().",
  244. argc );
  245. bwb_error( bwb_ebuf );
  246. return NULL;
  247. }
  248. #else
  249. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  250. {
  251. return NULL;
  252. }
  253. #endif
  254. /* assign values */
  255. #if INTENSIVE_DEBUG
  256. sprintf( bwb_ebuf, "in fnc_abs(): nvar type <%c>; calling finnval()",
  257. nvar.type );
  258. bwb_debug( bwb_ebuf );
  259. #endif
  260. /* Added double recast here (JBV) */
  261. * var_findnval( &nvar, nvar.array_pos ) =
  262. (bnumber) fabs( (double) var_getnval( &( argv[ 0 ] ) ) );
  263. return &nvar;
  264. }
  265. /***************************************************************
  266. FUNCTION: fnc_rnd()
  267. DESCRIPTION: This C function implements the BASIC
  268. predefined RND function, returning a
  269. pseudo-random number in the range
  270. 0 to 1. It is affected by the RANDOMIZE
  271. command statement.
  272. SYNTAX: RND( number )
  273. ***************************************************************/
  274. #if ANSI_C
  275. struct bwb_variable *
  276. fnc_rnd( int argc, struct bwb_variable *argv, int unique_id )
  277. #else
  278. struct bwb_variable *
  279. fnc_rnd( argc, argv, unique_id )
  280. int argc;
  281. struct bwb_variable *argv;
  282. int unique_id;
  283. #endif
  284. {
  285. static struct bwb_variable nvar;
  286. static int init = FALSE;
  287. /* initialize the variable if necessary */
  288. if ( init == FALSE )
  289. {
  290. init = TRUE;
  291. var_make( &nvar, NUMBER );
  292. }
  293. /* Added bnumber recast here (JBV) */
  294. * var_findnval( &nvar, nvar.array_pos )
  295. = (bnumber) ( (float) rand() / RAND_MAX );
  296. return &nvar;
  297. }
  298. /***************************************************************
  299. FUNCTION: fnc_atn()
  300. DESCRIPTION: This C function implements the BASIC
  301. predefined ATN function, returning the
  302. arctangent of the argument.
  303. SYNTAX: ATN( number )
  304. ***************************************************************/
  305. #if ANSI_C
  306. struct bwb_variable *
  307. fnc_atn( int argc, struct bwb_variable *argv, int unique_id )
  308. #else
  309. struct bwb_variable *
  310. fnc_atn( argc, argv, unique_id )
  311. int argc;
  312. struct bwb_variable *argv;
  313. int unique_id;
  314. #endif
  315. {
  316. static struct bwb_variable nvar;
  317. static int init = FALSE;
  318. /* initialize the variable if necessary */
  319. if ( init == FALSE )
  320. {
  321. init = TRUE;
  322. var_make( &nvar, NUMBER );
  323. }
  324. #if INTENSIVE_DEBUG
  325. sprintf( bwb_ebuf, "in fnc_atn(): received f_arg <%f> ",
  326. var_getnval( &( argv[ 0 ] ) ) );
  327. bwb_debug( bwb_ebuf );
  328. #endif
  329. #if PROG_ERRORS
  330. if ( argc < 1 )
  331. {
  332. sprintf( bwb_ebuf, "Not enough parameters (%d) to function ATN().",
  333. argc );
  334. bwb_error( bwb_ebuf );
  335. return NULL;
  336. }
  337. else if ( argc > 1 )
  338. {
  339. sprintf( bwb_ebuf, "Too many parameters (%d) to function ATN().",
  340. argc );
  341. bwb_error( bwb_ebuf );
  342. return NULL;
  343. }
  344. #else
  345. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  346. {
  347. return NULL;
  348. }
  349. #endif
  350. /* assign values */
  351. * var_findnval( &nvar, nvar.array_pos )
  352. = (bnumber) atan( (double) var_getnval( &( argv[ 0 ] ) ) );
  353. return &nvar;
  354. }
  355. /***************************************************************
  356. FUNCTION: fnc_cos()
  357. DESCRIPTION: This C function implements the BASIC
  358. predefined COS function, returning the
  359. cosine of the argument.
  360. SYNTAX: COS( number )
  361. ***************************************************************/
  362. #if ANSI_C
  363. struct bwb_variable *
  364. fnc_cos( int argc, struct bwb_variable *argv, int unique_id )
  365. #else
  366. struct bwb_variable *
  367. fnc_cos( argc, argv, unique_id )
  368. int argc;
  369. struct bwb_variable *argv;
  370. int unique_id;
  371. #endif
  372. {
  373. static struct bwb_variable nvar;
  374. static int init = FALSE;
  375. /* initialize the variable if necessary */
  376. if ( init == FALSE )
  377. {
  378. init = TRUE;
  379. var_make( &nvar, NUMBER );
  380. }
  381. #if INTENSIVE_DEBUG
  382. sprintf( bwb_ebuf, "in fnc_cos(): received f_arg <%f> ",
  383. var_getnval( &( argv[ 0 ] ) ) );
  384. bwb_debug( bwb_ebuf );
  385. #endif
  386. #if PROG_ERRORS
  387. if ( argc < 1 )
  388. {
  389. sprintf( bwb_ebuf, "Not enough parameters (%d) to function COS().",
  390. argc );
  391. bwb_error( bwb_ebuf );
  392. return NULL;
  393. }
  394. else if ( argc > 1 )
  395. {
  396. sprintf( bwb_ebuf, "Too many parameters (%d) to function COS().",
  397. argc );
  398. bwb_error( bwb_ebuf );
  399. return NULL;
  400. }
  401. #else
  402. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  403. {
  404. return NULL;
  405. }
  406. #endif
  407. /* assign values */
  408. * var_findnval( &nvar, nvar.array_pos )
  409. = (bnumber) cos( (double) var_getnval( &( argv[ 0 ] ) ) );
  410. return &nvar;
  411. }
  412. /***************************************************************
  413. FUNCTION: fnc_log()
  414. DESCRIPTION: This C function implements the BASIC
  415. predefined LOG function, returning the
  416. natural logarithm of the argument.
  417. SYNTAX: LOG( number )
  418. ***************************************************************/
  419. #if ANSI_C
  420. struct bwb_variable *
  421. fnc_log( int argc, struct bwb_variable *argv, int unique_id )
  422. #else
  423. struct bwb_variable *
  424. fnc_log( argc, argv, unique_id )
  425. int argc;
  426. struct bwb_variable *argv;
  427. int unique_id;
  428. #endif
  429. {
  430. static struct bwb_variable nvar;
  431. static int init = FALSE;
  432. /* initialize the variable if necessary */
  433. if ( init == FALSE )
  434. {
  435. init = TRUE;
  436. var_make( &nvar, NUMBER );
  437. }
  438. #if INTENSIVE_DEBUG
  439. sprintf( bwb_ebuf, "in fnc_log(): received f_arg <%f> ",
  440. var_getnval( &( argv[ 0 ] ) ) );
  441. bwb_debug( bwb_ebuf );
  442. #endif
  443. #if PROG_ERRORS
  444. if ( argc < 1 )
  445. {
  446. sprintf( bwb_ebuf, "Not enough parameters (%d) to function LOG().",
  447. argc );
  448. bwb_error( bwb_ebuf );
  449. return NULL;
  450. }
  451. else if ( argc > 1 )
  452. {
  453. sprintf( bwb_ebuf, "Too many parameters (%d) to function LOG().",
  454. argc );
  455. bwb_error( bwb_ebuf );
  456. return NULL;
  457. }
  458. #else
  459. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  460. {
  461. return NULL;
  462. }
  463. #endif
  464. /* assign values */
  465. * var_findnval( &nvar, nvar.array_pos )
  466. = (bnumber) log( (double) var_getnval( &( argv[ 0 ] ) ) );
  467. return &nvar;
  468. }
  469. /***************************************************************
  470. FUNCTION: fnc_sin()
  471. DESCRIPTION: This C function implements the BASIC
  472. predefined SIN function, returning
  473. the sine of the argument.
  474. SYNTAX: SIN( number )
  475. ***************************************************************/
  476. #if ANSI_C
  477. struct bwb_variable *
  478. fnc_sin( int argc, struct bwb_variable *argv, int unique_id )
  479. #else
  480. struct bwb_variable *
  481. fnc_sin( argc, argv, unique_id )
  482. int argc;
  483. struct bwb_variable *argv;
  484. int unique_id;
  485. #endif
  486. {
  487. static struct bwb_variable nvar;
  488. static int init = FALSE;
  489. /* initialize the variable if necessary */
  490. if ( init == FALSE )
  491. {
  492. init = TRUE;
  493. var_make( &nvar, NUMBER );
  494. }
  495. #if INTENSIVE_DEBUG
  496. sprintf( bwb_ebuf, "in fnc_sin(): received f_arg <%f> ",
  497. var_getnval( &( argv[ 0 ] ) ) );
  498. bwb_debug( bwb_ebuf );
  499. #endif
  500. #if PROG_ERRORS
  501. if ( argc < 1 )
  502. {
  503. sprintf( bwb_ebuf, "Not enough parameters (%d) to function SIN().",
  504. argc );
  505. bwb_error( bwb_ebuf );
  506. return NULL;
  507. }
  508. else if ( argc > 1 )
  509. {
  510. sprintf( bwb_ebuf, "Too many parameters (%d) to function SIN().",
  511. argc );
  512. bwb_error( bwb_ebuf );
  513. return NULL;
  514. }
  515. #else
  516. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  517. {
  518. return NULL;
  519. }
  520. #endif
  521. /* assign values */
  522. * var_findnval( &nvar, nvar.array_pos )
  523. = (bnumber) sin( (double) var_getnval( &( argv[ 0 ] ) ) );
  524. return &nvar;
  525. }
  526. /***************************************************************
  527. FUNCTION: fnc_sqr()
  528. DESCRIPTION: This C function implements the BASIC
  529. predefined SQR function, returning
  530. the square root of the argument.
  531. SYNTAX: SQR( number )
  532. ***************************************************************/
  533. #if ANSI_C
  534. struct bwb_variable *
  535. fnc_sqr( int argc, struct bwb_variable *argv, int unique_id )
  536. #else
  537. struct bwb_variable *
  538. fnc_sqr( argc, argv, unique_id )
  539. int argc;
  540. struct bwb_variable *argv;
  541. int unique_id;
  542. #endif
  543. {
  544. static struct bwb_variable nvar;
  545. static int init = FALSE;
  546. /* initialize the variable if necessary */
  547. if ( init == FALSE )
  548. {
  549. init = TRUE;
  550. var_make( &nvar, NUMBER );
  551. }
  552. #if INTENSIVE_DEBUG
  553. sprintf( bwb_ebuf, "in fnc_sqr(): received f_arg <%f> ",
  554. var_getnval( &( argv[ 0 ] ) ) );
  555. bwb_debug( bwb_ebuf );
  556. #endif
  557. #if PROG_ERRORS
  558. if ( argc < 1 )
  559. {
  560. sprintf( bwb_ebuf, "Not enough parameters (%d) to function SQR().",
  561. argc );
  562. bwb_error( bwb_ebuf );
  563. return NULL;
  564. }
  565. else if ( argc > 1 )
  566. {
  567. sprintf( bwb_ebuf, "Too many parameters (%d) to function SQR().",
  568. argc );
  569. bwb_error( bwb_ebuf );
  570. return NULL;
  571. }
  572. #else
  573. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  574. {
  575. return NULL;
  576. }
  577. #endif
  578. /* assign values */
  579. * var_findnval( &nvar, nvar.array_pos )
  580. = (bnumber) sqrt( (double) var_getnval( &( argv[ 0 ] ) ) );
  581. return &nvar;
  582. }
  583. /***************************************************************
  584. FUNCTION: fnc_tan()
  585. DESCRIPTION: This C function implements the BASIC
  586. predefined TAN function, returning the
  587. tangent of the argument.
  588. SYNTAX: TAN( number )
  589. ***************************************************************/
  590. #if ANSI_C
  591. struct bwb_variable *
  592. fnc_tan( int argc, struct bwb_variable *argv, int unique_id )
  593. #else
  594. struct bwb_variable *
  595. fnc_tan( argc, argv, unique_id )
  596. int argc;
  597. struct bwb_variable *argv;
  598. int unique_id;
  599. #endif
  600. {
  601. static struct bwb_variable nvar;
  602. static int init = FALSE;
  603. /* initialize the variable if necessary */
  604. if ( init == FALSE )
  605. {
  606. init = TRUE;
  607. var_make( &nvar, NUMBER );
  608. }
  609. #if INTENSIVE_DEBUG
  610. sprintf( bwb_ebuf, "in fnc_tan(): received f_arg <%f> ",
  611. var_getnval( &( argv[ 0 ] ) ) );
  612. bwb_debug( bwb_ebuf );
  613. #endif
  614. #if PROG_ERRORS
  615. if ( argc < 1 )
  616. {
  617. sprintf( bwb_ebuf, "Not enough parameters (%d) to function TAN().",
  618. argc );
  619. bwb_error( bwb_ebuf );
  620. return NULL;
  621. }
  622. else if ( argc > 1 )
  623. {
  624. sprintf( bwb_ebuf, "Too many parameters (%d) to function TAN().",
  625. argc );
  626. bwb_error( bwb_ebuf );
  627. return NULL;
  628. }
  629. #else
  630. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  631. {
  632. return NULL;
  633. }
  634. #endif
  635. /* assign values */
  636. * var_findnval( &nvar, nvar.array_pos )
  637. = (bnumber) tan( (double) var_getnval( &( argv[ 0 ] ) ) );
  638. return &nvar;
  639. }
  640. /***************************************************************
  641. FUNCTION: fnc_sgn()
  642. DESCRIPTION: This C function implements the BASIC
  643. predefined SGN function, returning 0
  644. if the argument is 0, -1 if the argument
  645. is less than 0, or 1 if the argument
  646. is more than 0.
  647. SYNTAX: SGN( number )
  648. ***************************************************************/
  649. #if ANSI_C
  650. struct bwb_variable *
  651. fnc_sgn( int argc, struct bwb_variable *argv, int unique_id )
  652. #else
  653. struct bwb_variable *
  654. fnc_sgn( argc, argv, unique_id )
  655. int argc;
  656. struct bwb_variable *argv;
  657. int unique_id;
  658. #endif
  659. {
  660. static struct bwb_variable nvar;
  661. bnumber nval;
  662. static int init = FALSE;
  663. /* initialize the variable if necessary */
  664. if ( init == FALSE )
  665. {
  666. init = TRUE;
  667. var_make( &nvar, NUMBER );
  668. }
  669. #if INTENSIVE_DEBUG
  670. sprintf( bwb_ebuf, "in fnc_sgn(): received f_arg <%f> ",
  671. var_getnval( &( argv[ 0 ] ) ) );
  672. bwb_debug( bwb_ebuf );
  673. #endif
  674. #if PROG_ERRORS
  675. if ( argc < 1 )
  676. {
  677. sprintf( bwb_ebuf, "Not enough parameters (%d) to function SGN().",
  678. argc );
  679. bwb_error( bwb_ebuf );
  680. return NULL;
  681. }
  682. else if ( argc > 1 )
  683. {
  684. sprintf( bwb_ebuf, "Too many parameters (%d) to function SGN().",
  685. argc );
  686. bwb_error( bwb_ebuf );
  687. return NULL;
  688. }
  689. #else
  690. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  691. {
  692. return NULL;
  693. }
  694. #endif
  695. /* assign values */
  696. nval = var_getnval( &( argv[ 0 ] ));
  697. if ( nval == (bnumber) 0.0 )
  698. {
  699. * var_findnval( &nvar, nvar.array_pos ) = (bnumber) 0;
  700. }
  701. else if ( nval > (bnumber) 0.0 )
  702. {
  703. * var_findnval( &nvar, nvar.array_pos ) = (bnumber) 1;
  704. }
  705. else
  706. {
  707. * var_findnval( &nvar, nvar.array_pos ) = (bnumber) -1;
  708. }
  709. return &nvar;
  710. }
  711. /***************************************************************
  712. FUNCTION: fnc_int()
  713. DESCRIPTION: This C function implements the BASIC
  714. predefined INT function, returning an
  715. integer value less then or equal to the
  716. argument.
  717. SYNTAX: INT( number )
  718. ***************************************************************/
  719. #if ANSI_C
  720. struct bwb_variable *
  721. fnc_int( int argc, struct bwb_variable *argv, int unique_id )
  722. #else
  723. struct bwb_variable *
  724. fnc_int( argc, argv, unique_id )
  725. int argc;
  726. struct bwb_variable *argv;
  727. int unique_id;
  728. #endif
  729. {
  730. static struct bwb_variable nvar;
  731. static int init = FALSE;
  732. /* initialize the variable if necessary */
  733. if ( init == FALSE )
  734. {
  735. init = TRUE;
  736. var_make( &nvar, NUMBER );
  737. }
  738. #if INTENSIVE_DEBUG
  739. sprintf( bwb_ebuf, "in fnc_int(): received f_arg <%f> ",
  740. var_getnval( &( argv[ 0 ] ) ) );
  741. bwb_debug( bwb_ebuf );
  742. #endif
  743. #if PROG_ERRORS
  744. if ( argc < 1 )
  745. {
  746. sprintf( bwb_ebuf, "Not enough parameters (%d) to function INT().",
  747. argc );
  748. bwb_error( bwb_ebuf );
  749. return NULL;
  750. }
  751. else if ( argc > 1 )
  752. {
  753. sprintf( bwb_ebuf, "Too many parameters (%d) to function INT().",
  754. argc );
  755. bwb_error( bwb_ebuf );
  756. return NULL;
  757. }
  758. #else
  759. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  760. {
  761. return NULL;
  762. }
  763. #endif
  764. /* assign values */
  765. * var_findnval( &nvar, nvar.array_pos )
  766. = (bnumber) floor( (double) var_getnval( &( argv[ 0 ] ) ) );
  767. return &nvar;
  768. }
  769. /***************************************************************
  770. FUNCTION: fnc_exp()
  771. DESCRIPTION: This C function implements the BASIC
  772. EXP() function, returning the exponential
  773. value of the argument.
  774. SYNTAX: EXP( number )
  775. ***************************************************************/
  776. #if ANSI_C
  777. struct bwb_variable *
  778. fnc_exp( int argc, struct bwb_variable *argv, int unique_id )
  779. #else
  780. struct bwb_variable *
  781. fnc_exp( argc, argv, unique_id )
  782. int argc;
  783. struct bwb_variable *argv;
  784. int unique_id;
  785. #endif
  786. {
  787. static struct bwb_variable nvar;
  788. static int init = FALSE;
  789. /* initialize the variable if necessary */
  790. if ( init == FALSE )
  791. {
  792. init = TRUE;
  793. var_make( &nvar, NUMBER );
  794. }
  795. #if PROG_ERRORS
  796. if ( argc < 1 )
  797. {
  798. sprintf( bwb_ebuf, "Not enough parameters (%d) to function EXP().",
  799. argc );
  800. bwb_error( bwb_ebuf );
  801. return NULL;
  802. }
  803. else if ( argc > 1 )
  804. {
  805. sprintf( bwb_ebuf, "Too many parameters (%d) to function EXP().",
  806. argc );
  807. bwb_error( bwb_ebuf );
  808. return NULL;
  809. }
  810. #else
  811. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  812. {
  813. return NULL;
  814. }
  815. #endif
  816. /* assign values */
  817. /* Added double recast here (JBV) */
  818. * var_findnval( &nvar, nvar.array_pos )
  819. = (bnumber) exp( (double) var_getnval( &( argv[ 0 ] ) ) );
  820. return &nvar;
  821. }
  822. #endif /* COMPRESS_FUNCS */
  823. #if COMMON_FUNCS
  824. /***************************************************************
  825. FUNCTION: fnc_val()
  826. DESCRIPTION: This C function implements the BASIC
  827. VAL() function, returning the numerical
  828. value of its string argument.
  829. SYNTAX: VAL( string$ )
  830. ***************************************************************/
  831. #if ANSI_C
  832. struct bwb_variable *
  833. fnc_val( int argc, struct bwb_variable *argv, int unique_id )
  834. #else
  835. struct bwb_variable *
  836. fnc_val( argc, argv, unique_id )
  837. int argc;
  838. struct bwb_variable *argv;
  839. int unique_id;
  840. #endif
  841. {
  842. static struct bwb_variable nvar;
  843. static char *tbuf;
  844. static int init = FALSE;
  845. /* initialize the variable if necessary */
  846. if ( init == FALSE )
  847. {
  848. init = TRUE;
  849. var_make( &nvar, NUMBER );
  850. /* Revised to CALLOC pass-thru call by JBV */
  851. if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_val" )) == NULL )
  852. {
  853. #if PROG_ERRORS
  854. bwb_error( "in fnc_val(): failed to get memory for tbuf" );
  855. #else
  856. bwb_error( err_getmem );
  857. #endif
  858. }
  859. }
  860. /* check arguments */
  861. #if PROG_ERRORS
  862. if ( argc < 1 )
  863. {
  864. sprintf( bwb_ebuf, "Not enough arguments to function VAL()" );
  865. bwb_error( bwb_ebuf );
  866. return NULL;
  867. }
  868. else if ( argc > 1 )
  869. {
  870. sprintf( bwb_ebuf, "Too many parameters (%d) to function VAL().",
  871. argc );
  872. bwb_error( bwb_ebuf );
  873. return NULL;
  874. }
  875. #else
  876. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  877. {
  878. return NULL;
  879. }
  880. #endif
  881. if ( argv[ 0 ].type != STRING )
  882. {
  883. #if PROG_ERRORS
  884. sprintf( bwb_ebuf, "Argument to function VAL() must be a string." );
  885. bwb_error( bwb_ebuf );
  886. #else
  887. bwb_error( err_mismatch );
  888. #endif
  889. return NULL;
  890. }
  891. /* read the value */
  892. str_btoc( tbuf, var_getsval( &( argv[ 0 ] ) ));
  893. *var_findnval( &nvar, nvar.array_pos ) = (bnumber) 0; /* JBV 1/97 */
  894. if ( strlen( tbuf ) != 0 ) /* JBV 1/97 (was == 0 with else) */
  895. #if NUMBER_DOUBLE
  896. sscanf( tbuf, "%lf",
  897. var_findnval( &nvar, nvar.array_pos ) );
  898. #else
  899. sscanf( tbuf, "%f",
  900. var_findnval( &nvar, nvar.array_pos ) );
  901. #endif
  902. return &nvar;
  903. }
  904. /***************************************************************
  905. FUNCTION: fnc_str()
  906. DESCRIPTION: This C function implements the BASIC
  907. STR$() function, returning an ASCII string
  908. with the decimal value of the numerical argument.
  909. SYNTAX: STR$( number )
  910. ***************************************************************/
  911. #if ANSI_C
  912. struct bwb_variable *
  913. fnc_str( int argc, struct bwb_variable *argv, int unique_id )
  914. #else
  915. struct bwb_variable *
  916. fnc_str( argc, argv, unique_id )
  917. int argc;
  918. struct bwb_variable *argv;
  919. int unique_id;
  920. #endif
  921. {
  922. static struct bwb_variable nvar;
  923. static char *tbuf;
  924. static int init = FALSE;
  925. /* initialize the variable if necessary */
  926. if ( init == FALSE )
  927. {
  928. init = TRUE;
  929. var_make( &nvar, STRING );
  930. /* Revised to CALLOC pass-thru call by JBV */
  931. if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_str" )) == NULL )
  932. {
  933. #if PROG_ERRORS
  934. bwb_error( "in fnc_str(): failed to get memory for tbuf" );
  935. #else
  936. bwb_error( err_getmem );
  937. #endif
  938. }
  939. }
  940. /* check parameters */
  941. #if PROG_ERRORS
  942. if ( argc < 1 )
  943. {
  944. sprintf( bwb_ebuf, "Not enough parameters (%d) to function STR$().",
  945. argc );
  946. bwb_error( bwb_ebuf );
  947. return NULL;
  948. }
  949. else if ( argc > 1 )
  950. {
  951. sprintf( bwb_ebuf, "Too many parameters (%d) to function STR$().",
  952. argc );
  953. bwb_error( bwb_ebuf );
  954. return NULL;
  955. }
  956. #else
  957. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  958. {
  959. return NULL;
  960. }
  961. #endif
  962. /* format as decimal number */
  963. sprintf( tbuf, " %.*f", prn_precision( &( argv[ 0 ] ) ),
  964. var_getnval( &( argv[ 0 ] ) ) );
  965. str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );
  966. return &nvar;
  967. }
  968. #endif /* COMMON_FUNCS */
  969. #if MS_FUNCS
  970. /***************************************************************
  971. FUNCTION: fnc_hex()
  972. DESCRIPTION: This C function implements the BASIC
  973. HEX$() function, returning a string
  974. containing the hexadecimal value of
  975. the numerical argument.
  976. SYNTAX: HEX$( number )
  977. ***************************************************************/
  978. #if ANSI_C
  979. struct bwb_variable *
  980. fnc_hex( int argc, struct bwb_variable *argv, int unique_id )
  981. #else
  982. struct bwb_variable *
  983. fnc_hex( argc, argv, unique_id )
  984. int argc;
  985. struct bwb_variable *argv;
  986. int unique_id;
  987. #endif
  988. {
  989. static struct bwb_variable nvar;
  990. static char *tbuf;
  991. static int init = FALSE;
  992. /* initialize the variable if necessary */
  993. if ( init == FALSE )
  994. {
  995. init = TRUE;
  996. var_make( &nvar, STRING );
  997. /* Revised to CALLOC pass-thru call by JBV */
  998. if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_hex" )) == NULL )
  999. {
  1000. #if PROG_ERRORS
  1001. bwb_error( "in fnc_hex(): failed to get memory for tbuf" );
  1002. #else
  1003. bwb_error( err_getmem );
  1004. #endif
  1005. }
  1006. }
  1007. /* check parameters */
  1008. #if PROG_ERRORS
  1009. if ( argc < 1 )
  1010. {
  1011. sprintf( bwb_ebuf, "Not enough parameters (%d) to function HEX$().",
  1012. argc );
  1013. bwb_error( bwb_ebuf );
  1014. return NULL;
  1015. }
  1016. else if ( argc > 1 )
  1017. {
  1018. sprintf( bwb_ebuf, "Too many parameters (%d) to function HEX$().",
  1019. argc );
  1020. bwb_error( bwb_ebuf );
  1021. return NULL;
  1022. }
  1023. #else
  1024. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1025. {
  1026. return NULL;
  1027. }
  1028. #endif
  1029. /* format as hex integer */
  1030. sprintf( tbuf, "%X", (int) trnc_int( (bnumber) var_getnval( &( argv[ 0 ] )) ) );
  1031. str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );
  1032. return &nvar;
  1033. }
  1034. /***************************************************************
  1035. FUNCTION: fnc_oct()
  1036. DESCRIPTION: This C function implements the BASIC
  1037. OCT$() function, returning a string
  1038. with the octal value of the numerical
  1039. argument.
  1040. SYNTAX: OCT$( number )
  1041. ***************************************************************/
  1042. #if ANSI_C
  1043. struct bwb_variable *
  1044. fnc_oct( int argc, struct bwb_variable *argv, int unique_id )
  1045. #else
  1046. struct bwb_variable *
  1047. fnc_oct( argc, argv, unique_id )
  1048. int argc;
  1049. struct bwb_variable *argv;
  1050. int unique_id;
  1051. #endif
  1052. {
  1053. static struct bwb_variable nvar;
  1054. static char *tbuf;
  1055. static int init = FALSE;
  1056. /* initialize the variable if necessary */
  1057. if ( init == FALSE )
  1058. {
  1059. init = TRUE;
  1060. var_make( &nvar, STRING );
  1061. /* Revised to CALLOC pass-thru call by JBV */
  1062. if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_oct" )) == NULL )
  1063. {
  1064. #if PROG_ERRORS
  1065. bwb_error( "in fnc_oct(): failed to get memory for tbuf" );
  1066. #else
  1067. bwb_error( err_getmem );
  1068. #endif
  1069. }
  1070. }
  1071. /* check parameters */
  1072. #if PROG_ERRORS
  1073. if ( argc < 1 )
  1074. {
  1075. sprintf( bwb_ebuf, "Not enough parameters (%d) to function OCT$().",
  1076. argc );
  1077. bwb_error( bwb_ebuf );
  1078. return NULL;
  1079. }
  1080. else if ( argc > 1 )
  1081. {
  1082. sprintf( bwb_ebuf, "Too many parameters (%d) to function OCT$().",
  1083. argc );
  1084. bwb_error( bwb_ebuf );
  1085. return NULL;
  1086. }
  1087. #else
  1088. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1089. {
  1090. return NULL;
  1091. }
  1092. #endif
  1093. /* format as octal integer */
  1094. /* Revised by JBV */
  1095. /* sprintf( tbuf, "%o", (int) var_getnval( &( argv[ 0 ] ) ) ); */
  1096. sprintf( tbuf, "%o", (int) trnc_int( (bnumber) var_getnval( &( argv[ 0 ] )) ) );
  1097. str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );
  1098. return &nvar;
  1099. }
  1100. /***************************************************************
  1101. FUNCTION: fnc_mki()
  1102. DESCRIPTION: This C function implements the BASIC
  1103. predefined MKI$() function.
  1104. NOTE: As implemented in bwBASIC, this is a
  1105. pseudo-function, since bwBASIC does
  1106. not recognize precision levels.
  1107. SYNTAX: MKI$( number )
  1108. ***************************************************************/
  1109. #if ANSI_C
  1110. struct bwb_variable *
  1111. fnc_mki( int argc, struct bwb_variable *argv, int unique_id )
  1112. #else
  1113. struct bwb_variable *
  1114. fnc_mki( argc, argv, unique_id )
  1115. int argc;
  1116. struct bwb_variable *argv;
  1117. int unique_id;
  1118. #endif
  1119. {
  1120. register int i;
  1121. static struct bwb_variable nvar;
  1122. bstring *b;
  1123. static char tbuf[ sizeof( int ) ];
  1124. static int init = FALSE;
  1125. /* initialize the variable if necessary */
  1126. if ( init == FALSE )
  1127. {
  1128. init = TRUE;
  1129. var_make( &nvar, STRING );
  1130. }
  1131. #if PROG_ERRORS
  1132. if ( argc < 1 )
  1133. {
  1134. sprintf( bwb_ebuf, "Not enough parameters (%d) to function MKI$().",
  1135. argc );
  1136. bwb_error( bwb_ebuf );
  1137. return NULL;
  1138. }
  1139. else if ( argc > 1 )
  1140. {
  1141. sprintf( bwb_ebuf, "Too many parameters (%d) to function MKI$().",
  1142. argc );
  1143. bwb_error( bwb_ebuf );
  1144. return NULL;
  1145. }
  1146. #else
  1147. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1148. {
  1149. return NULL;
  1150. }
  1151. #endif
  1152. /* assign values */
  1153. an_integer.the_integer = (int) var_getnval( &( argv[ 0 ] ) );
  1154. for ( i = 0; i < sizeof( int ); ++i )
  1155. {
  1156. tbuf[ i ] = an_integer.the_chars[ i ];
  1157. tbuf[ i + 1 ] = '\0';
  1158. }
  1159. b = var_getsval( &nvar );
  1160. b->length = sizeof( int );
  1161. b->sbuffer = tbuf;
  1162. b->rab = FALSE;
  1163. return &nvar;
  1164. }
  1165. /***************************************************************
  1166. FUNCTION: fnc_mkd()
  1167. DESCRIPTION: This C function implements the BASIC
  1168. predefined MKD$() function.
  1169. NOTE: As implemented in bwBASIC, this is a
  1170. pseudo-function, since bwBASIC does
  1171. not recognize precision levels.
  1172. SYNTAX: MKD$( number )
  1173. ***************************************************************/
  1174. #if ANSI_C
  1175. struct bwb_variable *
  1176. fnc_mkd( int argc, struct bwb_variable *argv, int unique_id )
  1177. #else
  1178. struct bwb_variable *
  1179. fnc_mkd( argc, argv, unique_id )
  1180. int argc;
  1181. struct bwb_variable *argv;
  1182. int unique_id;
  1183. #endif
  1184. {
  1185. register int i;
  1186. static struct bwb_variable nvar;
  1187. bstring *b;
  1188. static char tbuf[ sizeof ( double ) ];
  1189. static int init = FALSE;
  1190. /* initialize the variable if necessary */
  1191. if ( init == FALSE )
  1192. {
  1193. init = TRUE;
  1194. var_make( &nvar, STRING );
  1195. }
  1196. #if PROG_ERRORS
  1197. if ( argc < 1 )
  1198. {
  1199. sprintf( bwb_ebuf, "Not enough parameters (%d) to function MKD$().",
  1200. argc );
  1201. bwb_error( bwb_ebuf );
  1202. return NULL;
  1203. }
  1204. else if ( argc > 1 )
  1205. {
  1206. sprintf( bwb_ebuf, "Too many parameters (%d) to function MKD$().",
  1207. argc );
  1208. bwb_error( bwb_ebuf );
  1209. return NULL;
  1210. }
  1211. #else
  1212. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1213. {
  1214. return NULL;
  1215. }
  1216. #endif
  1217. /* assign values */
  1218. a_double.the_double = var_getnval( &( argv[ 0 ] ) );
  1219. for ( i = 0; i < sizeof ( double ); ++i )
  1220. {
  1221. tbuf[ i ] = a_double.the_chars[ i ];
  1222. tbuf[ i + 1 ] = '\0';
  1223. }
  1224. b = var_getsval( &nvar );
  1225. b->length = sizeof( double );
  1226. b->sbuffer = tbuf;
  1227. b->rab = FALSE;
  1228. return &nvar;
  1229. }
  1230. /***************************************************************
  1231. FUNCTION: fnc_mks()
  1232. DESCRIPTION: This C function implements the BASIC
  1233. predefined MKS$() function.
  1234. NOTE: As implemented in bwBASIC, this is a
  1235. pseudo-function, since bwBASIC does
  1236. not recognize precision levels.
  1237. SYNTAX: MKS$( number )
  1238. ***************************************************************/
  1239. #if ANSI_C
  1240. struct bwb_variable *
  1241. fnc_mks( int argc, struct bwb_variable *argv, int unique_id )
  1242. #else
  1243. struct bwb_variable *
  1244. fnc_mks( argc, argv, unique_id )
  1245. int argc;
  1246. struct bwb_variable *argv;
  1247. int unique_id;
  1248. #endif
  1249. {
  1250. register int i;
  1251. static struct bwb_variable nvar;
  1252. static char tbuf[ 5 ];
  1253. bstring *b;
  1254. static int init = FALSE;
  1255. /* initialize the variable if necessary */
  1256. if ( init == FALSE )
  1257. {
  1258. init = TRUE;
  1259. var_make( &nvar, STRING );
  1260. }
  1261. #if PROG_ERRORS
  1262. if ( argc < 1 )
  1263. {
  1264. sprintf( bwb_ebuf, "Not enough parameters (%d) to function MKS$().",
  1265. argc );
  1266. bwb_error( bwb_ebuf );
  1267. return NULL;
  1268. }
  1269. else if ( argc > 1 )
  1270. {
  1271. sprintf( bwb_ebuf, "Too many parameters (%d) to function MKS$().",
  1272. argc );
  1273. bwb_error( bwb_ebuf );
  1274. return NULL;
  1275. }
  1276. #else
  1277. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1278. {
  1279. return NULL;
  1280. }
  1281. #endif
  1282. /* assign values */
  1283. a_float.the_float = var_getnval( &( argv[ 0 ] ) );
  1284. for ( i = 0; i < sizeof( float ); ++i )
  1285. {
  1286. tbuf[ i ] = a_float.the_chars[ i ];
  1287. tbuf[ i + 1 ] = '\0';
  1288. }
  1289. b = var_getsval( &nvar );
  1290. b->length = sizeof( float );
  1291. b->sbuffer = tbuf;
  1292. b->rab = FALSE;
  1293. #if INTENSIVE_DEBUG
  1294. sprintf( bwb_ebuf, "in fnc_mks(): string <%s> hex vals <%X><%X><%X><%X>",
  1295. tbuf, tbuf[ 0 ], tbuf[ 1 ], tbuf[ 2 ], tbuf[ 3 ] );
  1296. bwb_debug( bwb_ebuf );
  1297. #endif
  1298. return &nvar;
  1299. }
  1300. /***************************************************************
  1301. FUNCTION: fnc_cvi()
  1302. DESCRIPTION: This C function implements the BASIC
  1303. predefined CVI() function.
  1304. NOTE: As implemented in bwBASIC, this is a
  1305. pseudo-function, since bwBASIC does
  1306. not recognize precision levels.
  1307. SYNTAX: CVI( string$ )
  1308. ***************************************************************/
  1309. #if ANSI_C
  1310. struct bwb_variable *
  1311. fnc_cvi( int argc, struct bwb_variable *argv, int unique_id )
  1312. #else
  1313. struct bwb_variable *
  1314. fnc_cvi( argc, argv, unique_id )
  1315. int argc;
  1316. struct bwb_variable *argv;
  1317. int unique_id;
  1318. #endif
  1319. {
  1320. register int i;
  1321. struct bwb_variable *v;
  1322. bstring *b;
  1323. static struct bwb_variable nvar;
  1324. static int init = FALSE;
  1325. /* initialize the variable if necessary */
  1326. if ( init == FALSE )
  1327. {
  1328. init = TRUE;
  1329. var_make( &nvar, NUMBER );
  1330. }
  1331. #if PROG_ERRORS
  1332. if ( argc < 1 )
  1333. {
  1334. sprintf( bwb_ebuf, "Not enough parameters (%d) to function CVI().",
  1335. argc );
  1336. bwb_error( bwb_ebuf );
  1337. return NULL;
  1338. }
  1339. else if ( argc > 1 )
  1340. {
  1341. sprintf( bwb_ebuf, "Too many parameters (%d) to function CVI().",
  1342. argc );
  1343. bwb_error( bwb_ebuf );
  1344. return NULL;
  1345. }
  1346. #else
  1347. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1348. {
  1349. return NULL;
  1350. }
  1351. #endif
  1352. /* assign values */
  1353. v = &( argv[ 0 ] );
  1354. b = var_findsval( v, v->array_pos );
  1355. for ( i = 0; i < sizeof( int ); ++i )
  1356. {
  1357. an_integer.the_chars[ i ] = b->sbuffer[ i ];
  1358. }
  1359. * var_findnval( &nvar, nvar.array_pos ) = (bnumber) an_integer.the_integer;
  1360. return &nvar;
  1361. }
  1362. /***************************************************************
  1363. FUNCTION: fnc_cvd()
  1364. DESCRIPTION: This C function implements the BASIC
  1365. predefined CVD() function.
  1366. NOTE: As implemented in bwBASIC, this is a
  1367. pseudo-function, since bwBASIC does
  1368. not recognize precision levels.
  1369. SYNTAX: CVD( string$ )
  1370. ***************************************************************/
  1371. #if ANSI_C
  1372. struct bwb_variable *
  1373. fnc_cvd( int argc, struct bwb_variable *argv, int unique_id )
  1374. #else
  1375. struct bwb_variable *
  1376. fnc_cvd( argc, argv, unique_id )
  1377. int argc;
  1378. struct bwb_variable *argv;
  1379. int unique_id;
  1380. #endif
  1381. {
  1382. register int i;
  1383. struct bwb_variable *v;
  1384. bstring *b;
  1385. static struct bwb_variable nvar;
  1386. static int init = FALSE;
  1387. /* initialize the variable if necessary */
  1388. if ( init == FALSE )
  1389. {
  1390. init = TRUE;
  1391. var_make( &nvar, NUMBER );
  1392. }
  1393. #if PROG_ERRORS
  1394. if ( argc < 1 )
  1395. {
  1396. sprintf( bwb_ebuf, "Not enough parameters (%d) to function CVD().",
  1397. argc );
  1398. bwb_error( bwb_ebuf );
  1399. return NULL;
  1400. }
  1401. else if ( argc > 1 )
  1402. {
  1403. sprintf( bwb_ebuf, "Too many parameters (%d) to function CVD().",
  1404. argc );
  1405. bwb_error( bwb_ebuf );
  1406. return NULL;
  1407. }
  1408. #else
  1409. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1410. {
  1411. return NULL;
  1412. }
  1413. #endif
  1414. /* assign values */
  1415. v = &( argv[ 0 ] );
  1416. b = var_findsval( v, v->array_pos );
  1417. for ( i = 0; i < sizeof( double ); ++i )
  1418. {
  1419. a_double.the_chars[ i ] = b->sbuffer[ i ];
  1420. }
  1421. * var_findnval( &nvar, nvar.array_pos ) = (bnumber) a_double.the_double;
  1422. return &nvar;
  1423. }
  1424. /***************************************************************
  1425. FUNCTION: fnc_cvs()
  1426. DESCRIPTION: This C function implements the BASIC
  1427. predefined CVS() function.
  1428. NOTE: As implemented in bwBASIC, this is a
  1429. pseudo-function, since bwBASIC does
  1430. not recognize precision levels.
  1431. SYNTAX: CVS( string$ )
  1432. ***************************************************************/
  1433. #if ANSI_C
  1434. struct bwb_variable *
  1435. fnc_cvs( int argc, struct bwb_variable *argv, int unique_id )
  1436. #else
  1437. struct bwb_variable *
  1438. fnc_cvs( argc, argv, unique_id )
  1439. int argc;
  1440. struct bwb_variable *argv;
  1441. int unique_id;
  1442. #endif
  1443. {
  1444. register int i;
  1445. struct bwb_variable *v;
  1446. bstring *b;
  1447. static struct bwb_variable nvar;
  1448. static int init = FALSE;
  1449. /* initialize the variable if necessary */
  1450. if ( init == FALSE )
  1451. {
  1452. init = TRUE;
  1453. var_make( &nvar, NUMBER );
  1454. }
  1455. #if PROG_ERRORS
  1456. if ( argc < 1 )
  1457. {
  1458. sprintf( bwb_ebuf, "Not enough parameters (%d) to function CVS().",
  1459. argc );
  1460. bwb_error( bwb_ebuf );
  1461. return NULL;
  1462. }
  1463. else if ( argc > 1 )
  1464. {
  1465. sprintf( bwb_ebuf, "Too many parameters (%d) to function CVS().",
  1466. argc );
  1467. bwb_error( bwb_ebuf );
  1468. return NULL;
  1469. }
  1470. #else
  1471. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1472. {
  1473. return NULL;
  1474. }
  1475. #endif
  1476. /* assign values */
  1477. v = &( argv[ 0 ] );
  1478. b = var_findsval( v, v->array_pos );
  1479. for ( i = 0; i < sizeof( float ); ++i )
  1480. {
  1481. a_float.the_chars[ i ] = b->sbuffer[ i ];
  1482. }
  1483. #if INTENSIVE_DEBUG
  1484. sprintf( bwb_ebuf, "in fnc_cvs(): string <%s> hex vals <%X><%X><%X><%X>",
  1485. a_float.the_chars, a_float.the_chars[ 0 ], a_float.the_chars[ 1 ],
  1486. a_float.the_chars[ 2 ], a_float.the_chars[ 3 ] );
  1487. bwb_debug( bwb_ebuf );
  1488. #endif
  1489. * var_findnval( &nvar, nvar.array_pos ) = a_float.the_float;
  1490. return &nvar;
  1491. }
  1492. /***************************************************************
  1493. FUNCTION: fnc_csng()
  1494. DESCRIPTION: This C function implements the BASIC
  1495. function CSNG(). As implemented,
  1496. this is a pseudo-function, since
  1497. all bwBASIC numerical values have the
  1498. same precision.
  1499. SYNTAX: CSNG( number )
  1500. ***************************************************************/
  1501. #if ANSI_C
  1502. struct bwb_variable *
  1503. fnc_csng( int argc, struct bwb_variable *argv, int unique_id )
  1504. #else
  1505. struct bwb_variable *
  1506. fnc_csng( argc, argv, unique_id )
  1507. int argc;
  1508. struct bwb_variable *argv;
  1509. int unique_id;
  1510. #endif
  1511. {
  1512. static struct bwb_variable nvar;
  1513. static int init = FALSE;
  1514. /* initialize the variable if necessary */
  1515. if ( init == FALSE )
  1516. {
  1517. init = TRUE;
  1518. var_make( &nvar, NUMBER );
  1519. }
  1520. /* check parameters */
  1521. #if PROG_ERRORS
  1522. if ( argc < 1 )
  1523. {
  1524. sprintf( bwb_ebuf, "Not enough parameters (%d) to function CINT().",
  1525. argc );
  1526. bwb_error( bwb_ebuf );
  1527. return NULL;
  1528. }
  1529. else if ( argc > 1 )
  1530. {
  1531. sprintf( bwb_ebuf, "Too many parameters (%d) to function CINT().",
  1532. argc );
  1533. bwb_error( bwb_ebuf );
  1534. return NULL;
  1535. }
  1536. #else
  1537. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1538. {
  1539. return NULL;
  1540. }
  1541. #endif
  1542. /* get truncated integer value */
  1543. * var_findnval( &nvar, nvar.array_pos )
  1544. = (bnumber) var_getnval( &( argv[ 0 ] ) );
  1545. return &nvar;
  1546. }
  1547. /***************************************************************
  1548. FUNCTION: fnc_cint()
  1549. DESCRIPTION: This C function returns the truncated
  1550. rounded integer value of its numerical
  1551. argument.
  1552. SYNTAX: CINT( number )
  1553. ***************************************************************/
  1554. #if ANSI_C
  1555. struct bwb_variable *
  1556. fnc_cint( int argc, struct bwb_variable *argv, int unique_id )
  1557. #else
  1558. struct bwb_variable *
  1559. fnc_cint( argc, argv, unique_id )
  1560. int argc;
  1561. struct bwb_variable *argv;
  1562. int unique_id;
  1563. #endif
  1564. {
  1565. static struct bwb_variable nvar;
  1566. static int init = FALSE;
  1567. /* initialize the variable if necessary */
  1568. if ( init == FALSE )
  1569. {
  1570. init = TRUE;
  1571. var_make( &nvar, NUMBER );
  1572. }
  1573. /* check parameters */
  1574. #if PROG_ERRORS
  1575. if ( argc < 1 )
  1576. {
  1577. sprintf( bwb_ebuf, "Not enough parameters (%d) to function CINT().",
  1578. argc );
  1579. bwb_error( bwb_ebuf );
  1580. return NULL;
  1581. }
  1582. else if ( argc > 1 )
  1583. {
  1584. sprintf( bwb_ebuf, "Too many parameters (%d) to function CINT().",
  1585. argc );
  1586. bwb_error( bwb_ebuf );
  1587. return NULL;
  1588. }
  1589. #else
  1590. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1591. {
  1592. return NULL;
  1593. }
  1594. #endif
  1595. /* get rounded integer value */
  1596. * var_findnval( &nvar, nvar.array_pos )
  1597. = round_int( var_getnval( &( argv[ 0 ] ) ));
  1598. return &nvar;
  1599. }
  1600. #endif /* MS_FUNCS */
  1601. /***************************************************************
  1602. FUNCTION: trnc_int()
  1603. DESCRIPTION: This function returns the truncated
  1604. truncated integer value of its numerical
  1605. argument.
  1606. ***************************************************************/
  1607. #if ANSI_C
  1608. bnumber
  1609. trnc_int( bnumber x )
  1610. #else
  1611. bnumber
  1612. trnc_int( x )
  1613. bnumber x;
  1614. #endif
  1615. {
  1616. double sign; /* Was bnumber (JBV) */
  1617. if ( x < (bnumber) 0.0 )
  1618. {
  1619. sign = (double) -1.0; /* Was bnumber (JBV) */
  1620. }
  1621. else
  1622. {
  1623. sign = (double) 1.0; /* Was bnumber (JBV) */
  1624. }
  1625. /* Added double recast here (JBV) */
  1626. return (bnumber) ( floor( fabs( (double) x )) * sign );
  1627. }
  1628. /***************************************************************
  1629. FUNCTION: round_int()
  1630. DESCRIPTION: This function returns the truncated
  1631. rounded integer value of its numerical
  1632. argument.
  1633. ***************************************************************/
  1634. #if ANSI_C
  1635. bnumber
  1636. round_int( bnumber x )
  1637. #else
  1638. bnumber
  1639. round_int( x )
  1640. bnumber x;
  1641. #endif
  1642. {
  1643. if ( x < (bnumber) 0.00 )
  1644. {
  1645. /* Added double recasts here (JBV) */
  1646. if ( (bnumber) fabs( (bnumber) floor( (double) x ) - x ) < (bnumber) 0.500 )
  1647. {
  1648. return (bnumber) floor( (double) x );
  1649. }
  1650. else
  1651. {
  1652. return (bnumber) ceil( (double) x );
  1653. }
  1654. }
  1655. else
  1656. {
  1657. if ( ( x - (bnumber) floor( (double) x )) < (bnumber) 0.500 )
  1658. {
  1659. return (bnumber) floor( (double) x );
  1660. }
  1661. else
  1662. {
  1663. return (bnumber) ceil( (double) x );
  1664. }
  1665. }
  1666. }