home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZMATH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  5.7 KB  |  237 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zmath.c */
  20. /* Mathematical operators */
  21. #include "math_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "store.h"
  26.  
  27. /* Current state of random number generator. */
  28. /* We have to implement this ourselves because */
  29. /* the Unix rand doesn't provide anything equivalent to rrand. */
  30. /* Note that the value always lies in the range [0..0x7ffffffe], */
  31. /* even if longs are longer than 32 bits. */
  32. private long rand_state;
  33.  
  34. /* Initialize the random number generator. */
  35. private void
  36. zmath_init(void)
  37. {    rand_state = 1;
  38. }
  39.  
  40. /****** NOTE: none of these operators currently ******/
  41. /****** check for floating over- or underflow.    ******/
  42.  
  43. /* <num> sqrt <real> */
  44. int
  45. zsqrt(register os_ptr op)
  46. {    float num;
  47.     int code = num_params(op, 1, &num);
  48.     if ( code < 0 )
  49.         return code;
  50.     if ( num < 0.0 )
  51.         return_error(e_rangecheck);
  52.     make_real(op, sqrt(num));
  53.     return 0;
  54. }
  55.  
  56. /* <num> arccos <real> */
  57. int
  58. zarccos(register os_ptr op)
  59. {    float num, result;
  60.     int code = num_params(op, 1, &num);
  61.     if ( code < 0 ) return code;
  62.     result = acos(num) * radians_to_degrees;
  63.     make_real(op, result);
  64.     return 0;
  65. }
  66.  
  67. /* <num> arcsin <real> */
  68. int
  69. zarcsin(register os_ptr op)
  70. {    float num, result;
  71.     int code = num_params(op, 1, &num);
  72.     if ( code < 0 ) return code;
  73.     result = asin(num) * radians_to_degrees;
  74.     make_real(op, result);
  75.     return 0;
  76. }
  77.  
  78. /* <num> <denom> atan <real> */
  79. int
  80. zatan(register os_ptr op)
  81. {    float args[2];
  82.     float result;
  83.     int code = num_params(op, 2, args);
  84.     if ( code < 0 ) return code;
  85.     if ( args[0] == 0 )        /* on X-axis, special case */
  86.        {    if ( args[1] == 0 )
  87.             return_error(e_undefinedresult);
  88.         result = (args[1] < 0 ? 180 : 0);
  89.        }
  90.     else
  91.        {    result = atan2(args[0], args[1]) * radians_to_degrees;
  92.         if ( result < 0 ) result += 360;
  93.        }
  94.     make_real(op - 1, result);
  95.     pop(1);
  96.     return 0;
  97. }
  98.  
  99. /* <num> cos <real> */
  100. int
  101. zcos(register os_ptr op)
  102. {    float angle;
  103.     int code = num_params(op, 1, &angle);
  104.     if ( code < 0 ) return code;
  105.     make_real(op, cos(angle * degrees_to_radians));
  106.     return 0;
  107. }
  108.  
  109. /* <num> sin <real> */
  110. int
  111. zsin(register os_ptr op)
  112. {    float angle;
  113.     int code = num_params(op, 1, &angle);
  114.     if ( code < 0 ) return code;
  115.     make_real(op, sin(angle * degrees_to_radians));
  116.     return 0;
  117. }
  118.  
  119. /* <base> <exponent> exp <real> */
  120. int
  121. zexp(register os_ptr op)
  122. {    float args[2];
  123.     float result;
  124.     double ipart;
  125.     int code = num_params(op, 2, args);
  126.     if ( code < 0 ) return code;
  127.     if ( args[0] == 0.0 && args[1] == 0.0 )
  128.         return_error(e_undefinedresult);
  129.     if ( args[0] < 0.0 && modf(args[1], &ipart) != 0.0 )
  130.         return_error(e_undefinedresult);
  131.     result = pow(args[0], args[1]);
  132.     make_real(op - 1, result);
  133.     pop(1);
  134.     return 0;
  135. }
  136.  
  137. /* <posnum> ln <real> */
  138. int
  139. zln(register os_ptr op)
  140. {    float num;
  141.     int code = num_params(op, 1, &num);
  142.     if ( code < 0 )
  143.         return code;
  144.     if ( num <= 0.0 )
  145.         return_error(e_rangecheck);
  146.     make_real(op, log(num));
  147.     return 0;
  148. }
  149.  
  150. /* <posnum> log <real> */
  151. int
  152. zlog(register os_ptr op)
  153. {    float num;
  154.     int code = num_params(op, 1, &num);
  155.     if ( code < 0 )
  156.         return code;
  157.     if ( num <= 0.0 )
  158.         return_error(e_rangecheck);
  159.     make_real(op, log10(num));
  160.     return 0;
  161. }
  162.  
  163. /* - rand <int> */
  164. int
  165. zrand(register os_ptr op)
  166. {    /*
  167.      * We use an algorithm from CACM 31 no. 10, pp. 1192-1201,
  168.      * October 1988.  According to a posting by Ed Taft on
  169.      * comp.lang.postscript, Level 2 (Adobe) PostScript interpreters
  170.      * use this algorithm too:
  171.      *    x[n+1] = (16807 * x[n]) mod (2^31 - 1)
  172.      */
  173. #define A 16807
  174. #define M 0x7fffffff
  175. #define Q 127773            /* M / A */
  176. #define R 2836                /* M % A */
  177.     rand_state = A * (rand_state % Q) - R * (rand_state / Q);
  178.     /* Note that rand_state cannot be 0 here. */
  179.     if ( rand_state <= 0 ) rand_state += M;
  180. #undef A
  181. #undef M
  182. #undef Q
  183. #undef R
  184.     push(1);
  185.     make_int(op, rand_state);
  186.     return 0;
  187. }
  188.  
  189. /* <int> srand - */
  190. int
  191. zsrand(register os_ptr op)
  192. {    long state;
  193.     check_type(*op, t_integer);
  194.     state = op->value.intval;
  195. #if arch_sizeof_long > 4
  196.     /* Trim the state back to 32 bits. */
  197.     state = (int)state;
  198. #endif
  199.     /*
  200.      * The following somewhat bizarre adjustments are according to
  201.      * public information from Adobe describing their implementation.
  202.      */
  203.     if ( state < 1 )
  204.         state = -(state % 0x7ffffffe) + 1;
  205.     else if ( state > 0x7ffffffe )
  206.         state = 0x7ffffffe;
  207.     rand_state = state;
  208.     pop(1);
  209.     return 0;
  210. }
  211.  
  212. /* - rrand <int> */
  213. int
  214. zrrand(register os_ptr op)
  215. {    push(1);
  216.     make_int(op, rand_state);
  217.     return 0;
  218. }
  219.  
  220. /* ------ Initialization procedure ------ */
  221.  
  222. op_def zmath_op_defs[] = {
  223.     {"1arccos", zarccos},        /* extension */
  224.     {"1arcsin", zarcsin},        /* extension */
  225.     {"2atan", zatan},
  226.     {"1cos", zcos},
  227.     {"2exp", zexp},
  228.     {"1ln", zln},
  229.     {"1log", zlog},
  230.     {"0rand", zrand},
  231.     {"0rrand", zrrand},
  232.     {"1sin", zsin},
  233.     {"1sqrt", zsqrt},
  234.     {"1srand", zsrand},
  235.     op_def_end(zmath_init)
  236. };
  237.