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.
 
 
 
 
 
 

2047 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. if ( strlen( tbuf ) == 0 ) /* JBV */
  894. *var_findnval( &nvar, nvar.array_pos ) = (bnumber) 0;
  895. else
  896. #if NUMBER_DOUBLE
  897. sscanf( tbuf, "%lf",
  898. var_findnval( &nvar, nvar.array_pos ) );
  899. #else
  900. sscanf( tbuf, "%f",
  901. var_findnval( &nvar, nvar.array_pos ) );
  902. #endif
  903. return &nvar;
  904. }
  905. /***************************************************************
  906. FUNCTION: fnc_str()
  907. DESCRIPTION: This C function implements the BASIC
  908. STR$() function, returning an ASCII string
  909. with the decimal value of the numerical argument.
  910. SYNTAX: STR$( number )
  911. ***************************************************************/
  912. #if ANSI_C
  913. struct bwb_variable *
  914. fnc_str( int argc, struct bwb_variable *argv, int unique_id )
  915. #else
  916. struct bwb_variable *
  917. fnc_str( argc, argv, unique_id )
  918. int argc;
  919. struct bwb_variable *argv;
  920. int unique_id;
  921. #endif
  922. {
  923. static struct bwb_variable nvar;
  924. static char *tbuf;
  925. static int init = FALSE;
  926. /* initialize the variable if necessary */
  927. if ( init == FALSE )
  928. {
  929. init = TRUE;
  930. var_make( &nvar, STRING );
  931. /* Revised to CALLOC pass-thru call by JBV */
  932. if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_str" )) == NULL )
  933. {
  934. #if PROG_ERRORS
  935. bwb_error( "in fnc_str(): failed to get memory for tbuf" );
  936. #else
  937. bwb_error( err_getmem );
  938. #endif
  939. }
  940. }
  941. /* check parameters */
  942. #if PROG_ERRORS
  943. if ( argc < 1 )
  944. {
  945. sprintf( bwb_ebuf, "Not enough parameters (%d) to function STR$().",
  946. argc );
  947. bwb_error( bwb_ebuf );
  948. return NULL;
  949. }
  950. else if ( argc > 1 )
  951. {
  952. sprintf( bwb_ebuf, "Too many parameters (%d) to function STR$().",
  953. argc );
  954. bwb_error( bwb_ebuf );
  955. return NULL;
  956. }
  957. #else
  958. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  959. {
  960. return NULL;
  961. }
  962. #endif
  963. /* format as decimal number */
  964. sprintf( tbuf, " %.*f", prn_precision( &( argv[ 0 ] ) ),
  965. var_getnval( &( argv[ 0 ] ) ) );
  966. str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );
  967. return &nvar;
  968. }
  969. #endif /* COMMON_FUNCS */
  970. #if MS_FUNCS
  971. /***************************************************************
  972. FUNCTION: fnc_hex()
  973. DESCRIPTION: This C function implements the BASIC
  974. HEX$() function, returning a string
  975. containing the hexadecimal value of
  976. the numerical argument.
  977. SYNTAX: HEX$( number )
  978. ***************************************************************/
  979. #if ANSI_C
  980. struct bwb_variable *
  981. fnc_hex( int argc, struct bwb_variable *argv, int unique_id )
  982. #else
  983. struct bwb_variable *
  984. fnc_hex( argc, argv, unique_id )
  985. int argc;
  986. struct bwb_variable *argv;
  987. int unique_id;
  988. #endif
  989. {
  990. static struct bwb_variable nvar;
  991. static char *tbuf;
  992. static int init = FALSE;
  993. /* initialize the variable if necessary */
  994. if ( init == FALSE )
  995. {
  996. init = TRUE;
  997. var_make( &nvar, STRING );
  998. /* Revised to CALLOC pass-thru call by JBV */
  999. if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_hex" )) == NULL )
  1000. {
  1001. #if PROG_ERRORS
  1002. bwb_error( "in fnc_hex(): failed to get memory for tbuf" );
  1003. #else
  1004. bwb_error( err_getmem );
  1005. #endif
  1006. }
  1007. }
  1008. /* check parameters */
  1009. #if PROG_ERRORS
  1010. if ( argc < 1 )
  1011. {
  1012. sprintf( bwb_ebuf, "Not enough parameters (%d) to function HEX$().",
  1013. argc );
  1014. bwb_error( bwb_ebuf );
  1015. return NULL;
  1016. }
  1017. else if ( argc > 1 )
  1018. {
  1019. sprintf( bwb_ebuf, "Too many parameters (%d) to function HEX$().",
  1020. argc );
  1021. bwb_error( bwb_ebuf );
  1022. return NULL;
  1023. }
  1024. #else
  1025. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1026. {
  1027. return NULL;
  1028. }
  1029. #endif
  1030. /* format as hex integer */
  1031. sprintf( tbuf, "%X", (int) trnc_int( (bnumber) var_getnval( &( argv[ 0 ] )) ) );
  1032. str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );
  1033. return &nvar;
  1034. }
  1035. /***************************************************************
  1036. FUNCTION: fnc_oct()
  1037. DESCRIPTION: This C function implements the BASIC
  1038. OCT$() function, returning a string
  1039. with the octal value of the numerical
  1040. argument.
  1041. SYNTAX: OCT$( number )
  1042. ***************************************************************/
  1043. #if ANSI_C
  1044. struct bwb_variable *
  1045. fnc_oct( int argc, struct bwb_variable *argv, int unique_id )
  1046. #else
  1047. struct bwb_variable *
  1048. fnc_oct( argc, argv, unique_id )
  1049. int argc;
  1050. struct bwb_variable *argv;
  1051. int unique_id;
  1052. #endif
  1053. {
  1054. static struct bwb_variable nvar;
  1055. static char *tbuf;
  1056. static int init = FALSE;
  1057. /* initialize the variable if necessary */
  1058. if ( init == FALSE )
  1059. {
  1060. init = TRUE;
  1061. var_make( &nvar, STRING );
  1062. /* Revised to CALLOC pass-thru call by JBV */
  1063. if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_oct" )) == NULL )
  1064. {
  1065. #if PROG_ERRORS
  1066. bwb_error( "in fnc_oct(): failed to get memory for tbuf" );
  1067. #else
  1068. bwb_error( err_getmem );
  1069. #endif
  1070. }
  1071. }
  1072. /* check parameters */
  1073. #if PROG_ERRORS
  1074. if ( argc < 1 )
  1075. {
  1076. sprintf( bwb_ebuf, "Not enough parameters (%d) to function OCT$().",
  1077. argc );
  1078. bwb_error( bwb_ebuf );
  1079. return NULL;
  1080. }
  1081. else if ( argc > 1 )
  1082. {
  1083. sprintf( bwb_ebuf, "Too many parameters (%d) to function OCT$().",
  1084. argc );
  1085. bwb_error( bwb_ebuf );
  1086. return NULL;
  1087. }
  1088. #else
  1089. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1090. {
  1091. return NULL;
  1092. }
  1093. #endif
  1094. /* format as octal integer */
  1095. /* Revised by JBV */
  1096. /* sprintf( tbuf, "%o", (int) var_getnval( &( argv[ 0 ] ) ) ); */
  1097. sprintf( tbuf, "%o", (int) trnc_int( (bnumber) var_getnval( &( argv[ 0 ] )) ) );
  1098. str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );
  1099. return &nvar;
  1100. }
  1101. /***************************************************************
  1102. FUNCTION: fnc_mki()
  1103. DESCRIPTION: This C function implements the BASIC
  1104. predefined MKI$() function.
  1105. NOTE: As implemented in bwBASIC, this is a
  1106. pseudo-function, since bwBASIC does
  1107. not recognize precision levels.
  1108. SYNTAX: MKI$( number )
  1109. ***************************************************************/
  1110. #if ANSI_C
  1111. struct bwb_variable *
  1112. fnc_mki( int argc, struct bwb_variable *argv, int unique_id )
  1113. #else
  1114. struct bwb_variable *
  1115. fnc_mki( argc, argv, unique_id )
  1116. int argc;
  1117. struct bwb_variable *argv;
  1118. int unique_id;
  1119. #endif
  1120. {
  1121. register int i;
  1122. static struct bwb_variable nvar;
  1123. bstring *b;
  1124. static char tbuf[ sizeof( int ) ];
  1125. static int init = FALSE;
  1126. /* initialize the variable if necessary */
  1127. if ( init == FALSE )
  1128. {
  1129. init = TRUE;
  1130. var_make( &nvar, STRING );
  1131. }
  1132. #if PROG_ERRORS
  1133. if ( argc < 1 )
  1134. {
  1135. sprintf( bwb_ebuf, "Not enough parameters (%d) to function MKI$().",
  1136. argc );
  1137. bwb_error( bwb_ebuf );
  1138. return NULL;
  1139. }
  1140. else if ( argc > 1 )
  1141. {
  1142. sprintf( bwb_ebuf, "Too many parameters (%d) to function MKI$().",
  1143. argc );
  1144. bwb_error( bwb_ebuf );
  1145. return NULL;
  1146. }
  1147. #else
  1148. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1149. {
  1150. return NULL;
  1151. }
  1152. #endif
  1153. /* assign values */
  1154. an_integer.the_integer = (int) var_getnval( &( argv[ 0 ] ) );
  1155. for ( i = 0; i < sizeof( int ); ++i )
  1156. {
  1157. tbuf[ i ] = an_integer.the_chars[ i ];
  1158. tbuf[ i + 1 ] = '\0';
  1159. }
  1160. b = var_getsval( &nvar );
  1161. b->length = sizeof( int );
  1162. b->sbuffer = tbuf;
  1163. b->rab = FALSE;
  1164. return &nvar;
  1165. }
  1166. /***************************************************************
  1167. FUNCTION: fnc_mkd()
  1168. DESCRIPTION: This C function implements the BASIC
  1169. predefined MKD$() function.
  1170. NOTE: As implemented in bwBASIC, this is a
  1171. pseudo-function, since bwBASIC does
  1172. not recognize precision levels.
  1173. SYNTAX: MKD$( number )
  1174. ***************************************************************/
  1175. #if ANSI_C
  1176. struct bwb_variable *
  1177. fnc_mkd( int argc, struct bwb_variable *argv, int unique_id )
  1178. #else
  1179. struct bwb_variable *
  1180. fnc_mkd( argc, argv, unique_id )
  1181. int argc;
  1182. struct bwb_variable *argv;
  1183. int unique_id;
  1184. #endif
  1185. {
  1186. register int i;
  1187. static struct bwb_variable nvar;
  1188. bstring *b;
  1189. static char tbuf[ sizeof ( double ) ];
  1190. static int init = FALSE;
  1191. /* initialize the variable if necessary */
  1192. if ( init == FALSE )
  1193. {
  1194. init = TRUE;
  1195. var_make( &nvar, STRING );
  1196. }
  1197. #if PROG_ERRORS
  1198. if ( argc < 1 )
  1199. {
  1200. sprintf( bwb_ebuf, "Not enough parameters (%d) to function MKD$().",
  1201. argc );
  1202. bwb_error( bwb_ebuf );
  1203. return NULL;
  1204. }
  1205. else if ( argc > 1 )
  1206. {
  1207. sprintf( bwb_ebuf, "Too many parameters (%d) to function MKD$().",
  1208. argc );
  1209. bwb_error( bwb_ebuf );
  1210. return NULL;
  1211. }
  1212. #else
  1213. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1214. {
  1215. return NULL;
  1216. }
  1217. #endif
  1218. /* assign values */
  1219. a_double.the_double = var_getnval( &( argv[ 0 ] ) );
  1220. for ( i = 0; i < sizeof ( double ); ++i )
  1221. {
  1222. tbuf[ i ] = a_double.the_chars[ i ];
  1223. tbuf[ i + 1 ] = '\0';
  1224. }
  1225. b = var_getsval( &nvar );
  1226. b->length = sizeof( double );
  1227. b->sbuffer = tbuf;
  1228. b->rab = FALSE;
  1229. return &nvar;
  1230. }
  1231. /***************************************************************
  1232. FUNCTION: fnc_mks()
  1233. DESCRIPTION: This C function implements the BASIC
  1234. predefined MKS$() function.
  1235. NOTE: As implemented in bwBASIC, this is a
  1236. pseudo-function, since bwBASIC does
  1237. not recognize precision levels.
  1238. SYNTAX: MKS$( number )
  1239. ***************************************************************/
  1240. #if ANSI_C
  1241. struct bwb_variable *
  1242. fnc_mks( int argc, struct bwb_variable *argv, int unique_id )
  1243. #else
  1244. struct bwb_variable *
  1245. fnc_mks( argc, argv, unique_id )
  1246. int argc;
  1247. struct bwb_variable *argv;
  1248. int unique_id;
  1249. #endif
  1250. {
  1251. register int i;
  1252. static struct bwb_variable nvar;
  1253. static char tbuf[ 5 ];
  1254. bstring *b;
  1255. static int init = FALSE;
  1256. /* initialize the variable if necessary */
  1257. if ( init == FALSE )
  1258. {
  1259. init = TRUE;
  1260. var_make( &nvar, STRING );
  1261. }
  1262. #if PROG_ERRORS
  1263. if ( argc < 1 )
  1264. {
  1265. sprintf( bwb_ebuf, "Not enough parameters (%d) to function MKS$().",
  1266. argc );
  1267. bwb_error( bwb_ebuf );
  1268. return NULL;
  1269. }
  1270. else if ( argc > 1 )
  1271. {
  1272. sprintf( bwb_ebuf, "Too many parameters (%d) to function MKS$().",
  1273. argc );
  1274. bwb_error( bwb_ebuf );
  1275. return NULL;
  1276. }
  1277. #else
  1278. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1279. {
  1280. return NULL;
  1281. }
  1282. #endif
  1283. /* assign values */
  1284. a_float.the_float = var_getnval( &( argv[ 0 ] ) );
  1285. for ( i = 0; i < sizeof( float ); ++i )
  1286. {
  1287. tbuf[ i ] = a_float.the_chars[ i ];
  1288. tbuf[ i + 1 ] = '\0';
  1289. }
  1290. b = var_getsval( &nvar );
  1291. b->length = sizeof( float );
  1292. b->sbuffer = tbuf;
  1293. b->rab = FALSE;
  1294. #if INTENSIVE_DEBUG
  1295. sprintf( bwb_ebuf, "in fnc_mks(): string <%s> hex vals <%X><%X><%X><%X>",
  1296. tbuf, tbuf[ 0 ], tbuf[ 1 ], tbuf[ 2 ], tbuf[ 3 ] );
  1297. bwb_debug( bwb_ebuf );
  1298. #endif
  1299. return &nvar;
  1300. }
  1301. /***************************************************************
  1302. FUNCTION: fnc_cvi()
  1303. DESCRIPTION: This C function implements the BASIC
  1304. predefined CVI() function.
  1305. NOTE: As implemented in bwBASIC, this is a
  1306. pseudo-function, since bwBASIC does
  1307. not recognize precision levels.
  1308. SYNTAX: CVI( string$ )
  1309. ***************************************************************/
  1310. #if ANSI_C
  1311. struct bwb_variable *
  1312. fnc_cvi( int argc, struct bwb_variable *argv, int unique_id )
  1313. #else
  1314. struct bwb_variable *
  1315. fnc_cvi( argc, argv, unique_id )
  1316. int argc;
  1317. struct bwb_variable *argv;
  1318. int unique_id;
  1319. #endif
  1320. {
  1321. register int i;
  1322. struct bwb_variable *v;
  1323. bstring *b;
  1324. static struct bwb_variable nvar;
  1325. static int init = FALSE;
  1326. /* initialize the variable if necessary */
  1327. if ( init == FALSE )
  1328. {
  1329. init = TRUE;
  1330. var_make( &nvar, NUMBER );
  1331. }
  1332. #if PROG_ERRORS
  1333. if ( argc < 1 )
  1334. {
  1335. sprintf( bwb_ebuf, "Not enough parameters (%d) to function CVI().",
  1336. argc );
  1337. bwb_error( bwb_ebuf );
  1338. return NULL;
  1339. }
  1340. else if ( argc > 1 )
  1341. {
  1342. sprintf( bwb_ebuf, "Too many parameters (%d) to function CVI().",
  1343. argc );
  1344. bwb_error( bwb_ebuf );
  1345. return NULL;
  1346. }
  1347. #else
  1348. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1349. {
  1350. return NULL;
  1351. }
  1352. #endif
  1353. /* assign values */
  1354. v = &( argv[ 0 ] );
  1355. b = var_findsval( v, v->array_pos );
  1356. for ( i = 0; i < sizeof( int ); ++i )
  1357. {
  1358. an_integer.the_chars[ i ] = b->sbuffer[ i ];
  1359. }
  1360. * var_findnval( &nvar, nvar.array_pos ) = (bnumber) an_integer.the_integer;
  1361. return &nvar;
  1362. }
  1363. /***************************************************************
  1364. FUNCTION: fnc_cvd()
  1365. DESCRIPTION: This C function implements the BASIC
  1366. predefined CVD() function.
  1367. NOTE: As implemented in bwBASIC, this is a
  1368. pseudo-function, since bwBASIC does
  1369. not recognize precision levels.
  1370. SYNTAX: CVD( string$ )
  1371. ***************************************************************/
  1372. #if ANSI_C
  1373. struct bwb_variable *
  1374. fnc_cvd( int argc, struct bwb_variable *argv, int unique_id )
  1375. #else
  1376. struct bwb_variable *
  1377. fnc_cvd( argc, argv, unique_id )
  1378. int argc;
  1379. struct bwb_variable *argv;
  1380. int unique_id;
  1381. #endif
  1382. {
  1383. register int i;
  1384. struct bwb_variable *v;
  1385. bstring *b;
  1386. static struct bwb_variable nvar;
  1387. static int init = FALSE;
  1388. /* initialize the variable if necessary */
  1389. if ( init == FALSE )
  1390. {
  1391. init = TRUE;
  1392. var_make( &nvar, NUMBER );
  1393. }
  1394. #if PROG_ERRORS
  1395. if ( argc < 1 )
  1396. {
  1397. sprintf( bwb_ebuf, "Not enough parameters (%d) to function CVD().",
  1398. argc );
  1399. bwb_error( bwb_ebuf );
  1400. return NULL;
  1401. }
  1402. else if ( argc > 1 )
  1403. {
  1404. sprintf( bwb_ebuf, "Too many parameters (%d) to function CVD().",
  1405. argc );
  1406. bwb_error( bwb_ebuf );
  1407. return NULL;
  1408. }
  1409. #else
  1410. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1411. {
  1412. return NULL;
  1413. }
  1414. #endif
  1415. /* assign values */
  1416. v = &( argv[ 0 ] );
  1417. b = var_findsval( v, v->array_pos );
  1418. for ( i = 0; i < sizeof( double ); ++i )
  1419. {
  1420. a_double.the_chars[ i ] = b->sbuffer[ i ];
  1421. }
  1422. * var_findnval( &nvar, nvar.array_pos ) = (bnumber) a_double.the_double;
  1423. return &nvar;
  1424. }
  1425. /***************************************************************
  1426. FUNCTION: fnc_cvs()
  1427. DESCRIPTION: This C function implements the BASIC
  1428. predefined CVS() function.
  1429. NOTE: As implemented in bwBASIC, this is a
  1430. pseudo-function, since bwBASIC does
  1431. not recognize precision levels.
  1432. SYNTAX: CVS( string$ )
  1433. ***************************************************************/
  1434. #if ANSI_C
  1435. struct bwb_variable *
  1436. fnc_cvs( int argc, struct bwb_variable *argv, int unique_id )
  1437. #else
  1438. struct bwb_variable *
  1439. fnc_cvs( argc, argv, unique_id )
  1440. int argc;
  1441. struct bwb_variable *argv;
  1442. int unique_id;
  1443. #endif
  1444. {
  1445. register int i;
  1446. struct bwb_variable *v;
  1447. bstring *b;
  1448. static struct bwb_variable nvar;
  1449. static int init = FALSE;
  1450. /* initialize the variable if necessary */
  1451. if ( init == FALSE )
  1452. {
  1453. init = TRUE;
  1454. var_make( &nvar, NUMBER );
  1455. }
  1456. #if PROG_ERRORS
  1457. if ( argc < 1 )
  1458. {
  1459. sprintf( bwb_ebuf, "Not enough parameters (%d) to function CVS().",
  1460. argc );
  1461. bwb_error( bwb_ebuf );
  1462. return NULL;
  1463. }
  1464. else if ( argc > 1 )
  1465. {
  1466. sprintf( bwb_ebuf, "Too many parameters (%d) to function CVS().",
  1467. argc );
  1468. bwb_error( bwb_ebuf );
  1469. return NULL;
  1470. }
  1471. #else
  1472. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1473. {
  1474. return NULL;
  1475. }
  1476. #endif
  1477. /* assign values */
  1478. v = &( argv[ 0 ] );
  1479. b = var_findsval( v, v->array_pos );
  1480. for ( i = 0; i < sizeof( float ); ++i )
  1481. {
  1482. a_float.the_chars[ i ] = b->sbuffer[ i ];
  1483. }
  1484. #if INTENSIVE_DEBUG
  1485. sprintf( bwb_ebuf, "in fnc_cvs(): string <%s> hex vals <%X><%X><%X><%X>",
  1486. a_float.the_chars, a_float.the_chars[ 0 ], a_float.the_chars[ 1 ],
  1487. a_float.the_chars[ 2 ], a_float.the_chars[ 3 ] );
  1488. bwb_debug( bwb_ebuf );
  1489. #endif
  1490. * var_findnval( &nvar, nvar.array_pos ) = a_float.the_float;
  1491. return &nvar;
  1492. }
  1493. /***************************************************************
  1494. FUNCTION: fnc_csng()
  1495. DESCRIPTION: This C function implements the BASIC
  1496. function CSNG(). As implemented,
  1497. this is a pseudo-function, since
  1498. all bwBASIC numerical values have the
  1499. same precision.
  1500. SYNTAX: CSNG( number )
  1501. ***************************************************************/
  1502. #if ANSI_C
  1503. struct bwb_variable *
  1504. fnc_csng( int argc, struct bwb_variable *argv, int unique_id )
  1505. #else
  1506. struct bwb_variable *
  1507. fnc_csng( argc, argv, unique_id )
  1508. int argc;
  1509. struct bwb_variable *argv;
  1510. int unique_id;
  1511. #endif
  1512. {
  1513. static struct bwb_variable nvar;
  1514. static int init = FALSE;
  1515. /* initialize the variable if necessary */
  1516. if ( init == FALSE )
  1517. {
  1518. init = TRUE;
  1519. var_make( &nvar, NUMBER );
  1520. }
  1521. /* check parameters */
  1522. #if PROG_ERRORS
  1523. if ( argc < 1 )
  1524. {
  1525. sprintf( bwb_ebuf, "Not enough parameters (%d) to function CINT().",
  1526. argc );
  1527. bwb_error( bwb_ebuf );
  1528. return NULL;
  1529. }
  1530. else if ( argc > 1 )
  1531. {
  1532. sprintf( bwb_ebuf, "Too many parameters (%d) to function CINT().",
  1533. argc );
  1534. bwb_error( bwb_ebuf );
  1535. return NULL;
  1536. }
  1537. #else
  1538. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1539. {
  1540. return NULL;
  1541. }
  1542. #endif
  1543. /* get truncated integer value */
  1544. * var_findnval( &nvar, nvar.array_pos )
  1545. = (bnumber) var_getnval( &( argv[ 0 ] ) );
  1546. return &nvar;
  1547. }
  1548. /***************************************************************
  1549. FUNCTION: fnc_cint()
  1550. DESCRIPTION: This C function returns the truncated
  1551. rounded integer value of its numerical
  1552. argument.
  1553. SYNTAX: CINT( number )
  1554. ***************************************************************/
  1555. #if ANSI_C
  1556. struct bwb_variable *
  1557. fnc_cint( int argc, struct bwb_variable *argv, int unique_id )
  1558. #else
  1559. struct bwb_variable *
  1560. fnc_cint( argc, argv, unique_id )
  1561. int argc;
  1562. struct bwb_variable *argv;
  1563. int unique_id;
  1564. #endif
  1565. {
  1566. static struct bwb_variable nvar;
  1567. static int init = FALSE;
  1568. /* initialize the variable if necessary */
  1569. if ( init == FALSE )
  1570. {
  1571. init = TRUE;
  1572. var_make( &nvar, NUMBER );
  1573. }
  1574. /* check parameters */
  1575. #if PROG_ERRORS
  1576. if ( argc < 1 )
  1577. {
  1578. sprintf( bwb_ebuf, "Not enough parameters (%d) to function CINT().",
  1579. argc );
  1580. bwb_error( bwb_ebuf );
  1581. return NULL;
  1582. }
  1583. else if ( argc > 1 )
  1584. {
  1585. sprintf( bwb_ebuf, "Too many parameters (%d) to function CINT().",
  1586. argc );
  1587. bwb_error( bwb_ebuf );
  1588. return NULL;
  1589. }
  1590. #else
  1591. if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )
  1592. {
  1593. return NULL;
  1594. }
  1595. #endif
  1596. /* get rounded integer value */
  1597. * var_findnval( &nvar, nvar.array_pos )
  1598. = round_int( var_getnval( &( argv[ 0 ] ) ));
  1599. return &nvar;
  1600. }
  1601. #endif /* MS_FUNCS */
  1602. /***************************************************************
  1603. FUNCTION: trnc_int()
  1604. DESCRIPTION: This function returns the truncated
  1605. truncated integer value of its numerical
  1606. argument.
  1607. ***************************************************************/
  1608. #if ANSI_C
  1609. bnumber
  1610. trnc_int( bnumber x )
  1611. #else
  1612. bnumber
  1613. trnc_int( x )
  1614. bnumber x;
  1615. #endif
  1616. {
  1617. double sign; /* Was bnumber (JBV) */
  1618. if ( x < (bnumber) 0.0 )
  1619. {
  1620. sign = (double) -1.0; /* Was bnumber (JBV) */
  1621. }
  1622. else
  1623. {
  1624. sign = (double) 1.0; /* Was bnumber (JBV) */
  1625. }
  1626. /* Added double recast here (JBV) */
  1627. return (bnumber) ( floor( fabs( (double) x )) * sign );
  1628. }
  1629. /***************************************************************
  1630. FUNCTION: round_int()
  1631. DESCRIPTION: This function returns the truncated
  1632. rounded integer value of its numerical
  1633. argument.
  1634. ***************************************************************/
  1635. #if ANSI_C
  1636. bnumber
  1637. round_int( bnumber x )
  1638. #else
  1639. bnumber
  1640. round_int( x )
  1641. bnumber x;
  1642. #endif
  1643. {
  1644. if ( x < (bnumber) 0.00 )
  1645. {
  1646. /* Added double recasts here (JBV) */
  1647. if ( (bnumber) fabs( (bnumber) floor( (double) x ) - x ) < (bnumber) 0.500 )
  1648. {
  1649. return (bnumber) floor( (double) x );
  1650. }
  1651. else
  1652. {
  1653. return (bnumber) ceil( (double) x );
  1654. }
  1655. }
  1656. else
  1657. {
  1658. if ( ( x - (bnumber) floor( (double) x )) < (bnumber) 0.500 )
  1659. {
  1660. return (bnumber) floor( (double) x );
  1661. }
  1662. else
  1663. {
  1664. return (bnumber) ceil( (double) x );
  1665. }
  1666. }
  1667. }