home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / ZMATH.C < prev    next >
C/C++ Source or Header  |  1992-04-25  |  5KB  |  213 lines

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