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.
 
 
 
 
 
 

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