Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-math / src / math.c
1 /*
2 ** math.c - Math module
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #include <mruby.h>
8 #include <mruby/array.h>
9
10 #include <math.h>
11
12 static void
13 domain_error(mrb_state *mrb, const char *func)
14 {
15   struct RClass *math = mrb_module_get(mrb, "Math");
16   struct RClass *domainerror = mrb_class_get_under(mrb, math, "DomainError");
17   mrb_value str = mrb_str_new_cstr(mrb, func);
18   mrb_raisef(mrb, domainerror, "Numerical argument is out of domain - %S", str);
19 }
20
21 /* math functions not provided by Microsoft Visual C++ 2012 or older */
22 #if defined _MSC_VER && _MSC_VER <= 1700
23
24 #include <float.h>
25
26 double
27 asinh(double x)
28 {
29   double xa, ya, y;
30
31   /* Basic formula loses precision for x < 0, but asinh is an odd function */
32   xa = fabs(x);
33   if (xa > 3.16227E+18) {
34     /* Prevent x*x from overflowing; basic formula reduces to log(2*x) */
35     ya = log(xa) + 0.69314718055994530942;
36   }
37   else {
38     /* Basic formula for asinh */
39     ya = log(xa + sqrt(xa*xa + 1.0));
40   }
41
42   y = _copysign(ya, x);
43   return y;
44 }
45
46 double
47 acosh(double x)
48 {
49   double y;
50
51   if (x > 3.16227E+18) {
52     /* Prevent x*x from overflowing; basic formula reduces to log(2*x) */
53     y = log(x) + 0.69314718055994530942;
54   }
55   else {
56     /* Basic formula for acosh */
57     y = log(x + sqrt(x*x - 1.0));
58   }
59
60   return y;
61 }
62
63 double
64 atanh(double x)
65 {
66   double y;
67
68   if (fabs(x) < 1E-2) {
69     /* The sums 1+x and 1-x lose precision for small x.  Use the polynomial
70        instead. */
71     double x2 = x * x;
72     y = x*(1.0 + x2*(1.0/3.0 + x2*(1.0/5.0 + x2*(1.0/7.0))));
73   }
74   else {
75     /* Basic formula for atanh */
76     y = 0.5 * (log(1.0+x) - log(1.0-x));
77   }
78
79   return y;
80 }
81
82 double
83 cbrt(double x)
84 {
85   double xa, ya, y;
86
87   /* pow(x, y) is undefined for x < 0 and y not an integer, but cbrt is an
88      odd function */
89   xa = fabs(x);
90   ya = pow(xa, 1.0/3.0);
91   y = _copysign(ya, x);
92   return y;
93 }
94
95 /* Declaration of complementary Error function */
96 double
97 erfc(double x);
98
99 /*
100 ** Implementations of error functions
101 ** credits to http://www.digitalmars.com/archives/cplusplus/3634.html
102 */
103
104 /* Implementation of Error function */
105 double
106 erf(double x)
107 {
108   static const double two_sqrtpi =  1.128379167095512574;
109   double sum  = x;
110   double term = x;
111   double xsqr = x*x;
112   int j= 1;
113   if (fabs(x) > 2.2) {
114     return 1.0 - erfc(x);
115   }
116   do {
117     term *= xsqr/j;
118     sum  -= term/(2*j+1);
119     ++j;
120     term *= xsqr/j;
121     sum  += term/(2*j+1);
122     ++j;
123     if (sum == 0) break;
124   } while (fabs(term/sum) > DBL_EPSILON);
125   return two_sqrtpi*sum;
126 }
127
128 /* Implementation of complementary Error function */
129 double
130 erfc(double x)
131 {
132   static const double one_sqrtpi=  0.564189583547756287;
133   double a = 1;
134   double b = x;
135   double c = x;
136   double d = x*x+0.5;
137   double q1;
138   double q2 = b/d;
139   double n = 1.0;
140   double t;
141   if (fabs(x) < 2.2) {
142     return 1.0 - erf(x);
143   }
144   if (x < 0.0) { /*signbit(x)*/
145     return 2.0 - erfc(-x);
146   }
147   do {
148     t  = a*n+b*x;
149     a  = b;
150     b  = t;
151     t  = c*n+d*x;
152     c  = d;
153     d  = t;
154     n += 0.5;
155     q1 = q2;
156     q2 = b/d;
157   } while (fabs(q1-q2)/q2 > DBL_EPSILON);
158   return one_sqrtpi*exp(-x*x)*q2;
159 }
160
161 #endif
162
163 #if defined __FreeBSD__ && !defined __FreeBSD_version
164 #include <osreldate.h> /* for __FreeBSD_version */
165 #endif
166
167 #if (defined _MSC_VER && _MSC_VER < 1800) || defined __ANDROID__ || (defined __FreeBSD__  &&  __FreeBSD_version < 803000)
168
169 double
170 log2(double x)
171 {
172     return log10(x)/log10(2.0);
173 }
174
175 #endif
176
177 /*
178   TRIGONOMETRIC FUNCTIONS
179 */
180
181 /*
182  *  call-seq:
183  *     Math.sin(x)    -> float
184  *
185  *  Computes the sine of <i>x</i> (expressed in radians). Returns
186  *  -1..1.
187  */
188 static mrb_value
189 math_sin(mrb_state *mrb, mrb_value obj)
190 {
191   mrb_float x;
192
193   mrb_get_args(mrb, "f", &x);
194   x = sin(x);
195
196   return mrb_float_value(mrb, x);
197 }
198
199 /*
200  *  call-seq:
201  *     Math.cos(x)    -> float
202  *
203  *  Computes the cosine of <i>x</i> (expressed in radians). Returns
204  *  -1..1.
205  */
206 static mrb_value
207 math_cos(mrb_state *mrb, mrb_value obj)
208 {
209   mrb_float x;
210
211   mrb_get_args(mrb, "f", &x);
212   x = cos(x);
213
214   return mrb_float_value(mrb, x);
215 }
216
217 /*
218  *  call-seq:
219  *     Math.tan(x)    -> float
220  *
221  *  Returns the tangent of <i>x</i> (expressed in radians).
222  */
223 static mrb_value
224 math_tan(mrb_state *mrb, mrb_value obj)
225 {
226   mrb_float x;
227
228   mrb_get_args(mrb, "f", &x);
229   x = tan(x);
230
231   return mrb_float_value(mrb, x);
232 }
233
234 /*
235   INVERSE TRIGONOMETRIC FUNCTIONS
236 */
237
238 /*
239  *  call-seq:
240  *     Math.asin(x)    -> float
241  *
242  *  Computes the arc sine of <i>x</i>.
243  *  @return computed value between `-(PI/2)` and `(PI/2)`.
244  */
245 static mrb_value
246 math_asin(mrb_state *mrb, mrb_value obj)
247 {
248   mrb_float x;
249
250   mrb_get_args(mrb, "f", &x);
251   if (x < -1.0 || x > 1.0) {
252     domain_error(mrb, "asin");
253   }
254   x = asin(x);
255
256   return mrb_float_value(mrb, x);
257 }
258
259 /*
260  *  call-seq:
261  *     Math.acos(x)    -> float
262  *
263  *  Computes the arc cosine of <i>x</i>. Returns 0..PI.
264  */
265 static mrb_value
266 math_acos(mrb_state *mrb, mrb_value obj)
267 {
268   mrb_float x;
269
270   mrb_get_args(mrb, "f", &x);
271   if (x < -1.0 || x > 1.0) {
272     domain_error(mrb, "acos");
273   }
274   x = acos(x);
275
276   return mrb_float_value(mrb, x);
277 }
278
279 /*
280  *  call-seq:
281  *     Math.atan(x)    -> float
282  *
283  *  Computes the arc tangent of <i>x</i>. Returns `-(PI/2) .. (PI/2)`.
284  */
285 static mrb_value
286 math_atan(mrb_state *mrb, mrb_value obj)
287 {
288   mrb_float x;
289
290   mrb_get_args(mrb, "f", &x);
291   x = atan(x);
292
293   return mrb_float_value(mrb, x);
294 }
295
296 /*
297  *  call-seq:
298  *     Math.atan2(y, x)  -> float
299  *
300  *  Computes the arc tangent given <i>y</i> and <i>x</i>. Returns
301  *  -PI..PI.
302  *
303  *    Math.atan2(-0.0, -1.0) #=> -3.141592653589793
304  *    Math.atan2(-1.0, -1.0) #=> -2.356194490192345
305  *    Math.atan2(-1.0, 0.0)  #=> -1.5707963267948966
306  *    Math.atan2(-1.0, 1.0)  #=> -0.7853981633974483
307  *    Math.atan2(-0.0, 1.0)  #=> -0.0
308  *    Math.atan2(0.0, 1.0)   #=> 0.0
309  *    Math.atan2(1.0, 1.0)   #=> 0.7853981633974483
310  *    Math.atan2(1.0, 0.0)   #=> 1.5707963267948966
311  *    Math.atan2(1.0, -1.0)  #=> 2.356194490192345
312  *    Math.atan2(0.0, -1.0)  #=> 3.141592653589793
313  *
314  */
315 static mrb_value
316 math_atan2(mrb_state *mrb, mrb_value obj)
317 {
318   mrb_float x, y;
319
320   mrb_get_args(mrb, "ff", &x, &y);
321   x = atan2(x, y);
322
323   return mrb_float_value(mrb, x);
324 }
325
326
327
328 /*
329   HYPERBOLIC TRIG FUNCTIONS
330 */
331 /*
332  *  call-seq:
333  *     Math.sinh(x)    -> float
334  *
335  *  Computes the hyperbolic sine of <i>x</i> (expressed in
336  *  radians).
337  */
338 static mrb_value
339 math_sinh(mrb_state *mrb, mrb_value obj)
340 {
341   mrb_float x;
342
343   mrb_get_args(mrb, "f", &x);
344   x = sinh(x);
345
346   return mrb_float_value(mrb, x);
347 }
348
349 /*
350  *  call-seq:
351  *     Math.cosh(x)    -> float
352  *
353  *  Computes the hyperbolic cosine of <i>x</i> (expressed in radians).
354  */
355 static mrb_value
356 math_cosh(mrb_state *mrb, mrb_value obj)
357 {
358   mrb_float x;
359
360   mrb_get_args(mrb, "f", &x);
361   x = cosh(x);
362
363   return mrb_float_value(mrb, x);
364 }
365
366 /*
367  *  call-seq:
368  *     Math.tanh()    -> float
369  *
370  *  Computes the hyperbolic tangent of <i>x</i> (expressed in
371  *  radians).
372  */
373 static mrb_value
374 math_tanh(mrb_state *mrb, mrb_value obj)
375 {
376   mrb_float x;
377
378   mrb_get_args(mrb, "f", &x);
379   x = tanh(x);
380
381   return mrb_float_value(mrb, x);
382 }
383
384
385 /*
386   INVERSE HYPERBOLIC TRIG FUNCTIONS
387 */
388
389 /*
390  *  call-seq:
391  *     Math.asinh(x)    -> float
392  *
393  *  Computes the inverse hyperbolic sine of <i>x</i>.
394  */
395 static mrb_value
396 math_asinh(mrb_state *mrb, mrb_value obj)
397 {
398   mrb_float x;
399
400   mrb_get_args(mrb, "f", &x);
401
402   x = asinh(x);
403
404   return mrb_float_value(mrb, x);
405 }
406
407 /*
408  *  call-seq:
409  *     Math.acosh(x)    -> float
410  *
411  *  Computes the inverse hyperbolic cosine of <i>x</i>.
412  */
413 static mrb_value
414 math_acosh(mrb_state *mrb, mrb_value obj)
415 {
416   mrb_float x;
417
418   mrb_get_args(mrb, "f", &x);
419   if (x < 1.0) {
420     domain_error(mrb, "acosh");
421   }
422   x = acosh(x);
423
424   return mrb_float_value(mrb, x);
425 }
426
427 /*
428  *  call-seq:
429  *     Math.atanh(x)    -> float
430  *
431  *  Computes the inverse hyperbolic tangent of <i>x</i>.
432  */
433 static mrb_value
434 math_atanh(mrb_state *mrb, mrb_value obj)
435 {
436   mrb_float x;
437
438   mrb_get_args(mrb, "f", &x);
439   if (x < -1.0 || x > 1.0) {
440     domain_error(mrb, "atanh");
441   }
442   x = atanh(x);
443
444   return mrb_float_value(mrb, x);
445 }
446
447 /*
448   EXPONENTIALS AND LOGARITHMS
449 */
450
451 /*
452  *  call-seq:
453  *     Math.exp(x)    -> float
454  *
455  *  Returns e**x.
456  *
457  *    Math.exp(0)       #=> 1.0
458  *    Math.exp(1)       #=> 2.718281828459045
459  *    Math.exp(1.5)     #=> 4.4816890703380645
460  *
461  */
462 static mrb_value
463 math_exp(mrb_state *mrb, mrb_value obj)
464 {
465   mrb_float x;
466
467   mrb_get_args(mrb, "f", &x);
468   x = exp(x);
469
470   return mrb_float_value(mrb, x);
471 }
472
473 /*
474  *  call-seq:
475  *     Math.log(numeric)    -> float
476  *     Math.log(num,base)   -> float
477  *
478  *  Returns the natural logarithm of <i>numeric</i>.
479  *  If additional second argument is given, it will be the base
480  *  of logarithm.
481  *
482  *    Math.log(1)          #=> 0.0
483  *    Math.log(Math::E)    #=> 1.0
484  *    Math.log(Math::E**3) #=> 3.0
485  *    Math.log(12,3)       #=> 2.2618595071429146
486  *
487  */
488 static mrb_value
489 math_log(mrb_state *mrb, mrb_value obj)
490 {
491   mrb_float x, base;
492   mrb_int argc;
493
494   argc = mrb_get_args(mrb, "f|f", &x, &base);
495   if (x < 0.0) {
496     domain_error(mrb, "log");
497   }
498   x = log(x);
499   if (argc == 2) {
500     if (base < 0.0) {
501       domain_error(mrb, "log");
502     }
503     x /= log(base);
504   }
505   return mrb_float_value(mrb, x);
506 }
507
508 /*
509  *  call-seq:
510  *     Math.log2(numeric)    -> float
511  *
512  *  Returns the base 2 logarithm of <i>numeric</i>.
513  *
514  *    Math.log2(1)      #=> 0.0
515  *    Math.log2(2)      #=> 1.0
516  *    Math.log2(32768)  #=> 15.0
517  *    Math.log2(65536)  #=> 16.0
518  *
519  */
520 static mrb_value
521 math_log2(mrb_state *mrb, mrb_value obj)
522 {
523   mrb_float x;
524
525   mrb_get_args(mrb, "f", &x);
526   if (x < 0.0) {
527     domain_error(mrb, "log2");
528   }
529   x = log2(x);
530
531   return mrb_float_value(mrb, x);
532 }
533
534 /*
535  *  call-seq:
536  *     Math.log10(numeric)    -> float
537  *
538  *  Returns the base 10 logarithm of <i>numeric</i>.
539  *
540  *    Math.log10(1)       #=> 0.0
541  *    Math.log10(10)      #=> 1.0
542  *    Math.log10(10**100) #=> 100.0
543  *
544  */
545 static mrb_value
546 math_log10(mrb_state *mrb, mrb_value obj)
547 {
548   mrb_float x;
549
550   mrb_get_args(mrb, "f", &x);
551   if (x < 0.0) {
552     domain_error(mrb, "log10");
553   }
554   x = log10(x);
555
556   return mrb_float_value(mrb, x);
557 }
558
559 /*
560  *  call-seq:
561  *     Math.sqrt(numeric)    -> float
562  *
563  *  Returns the square root of <i>numeric</i>.
564  *
565  */
566 static mrb_value
567 math_sqrt(mrb_state *mrb, mrb_value obj)
568 {
569   mrb_float x;
570
571   mrb_get_args(mrb, "f", &x);
572   if (x < 0.0) {
573     domain_error(mrb, "sqrt");
574   }
575   x = sqrt(x);
576
577   return mrb_float_value(mrb, x);
578 }
579
580
581 /*
582  *  call-seq:
583  *     Math.cbrt(numeric)    -> float
584  *
585  *  Returns the cube root of <i>numeric</i>.
586  *
587  *    -9.upto(9) {|x|
588  *      p [x, Math.cbrt(x), Math.cbrt(x)**3]
589  *    }
590  *    #=>
591  *    [-9, -2.0800838230519, -9.0]
592  *    [-8, -2.0, -8.0]
593  *    [-7, -1.91293118277239, -7.0]
594  *    [-6, -1.81712059283214, -6.0]
595  *    [-5, -1.7099759466767, -5.0]
596  *    [-4, -1.5874010519682, -4.0]
597  *    [-3, -1.44224957030741, -3.0]
598  *    [-2, -1.25992104989487, -2.0]
599  *    [-1, -1.0, -1.0]
600  *    [0, 0.0, 0.0]
601  *    [1, 1.0, 1.0]
602  *    [2, 1.25992104989487, 2.0]
603  *    [3, 1.44224957030741, 3.0]
604  *    [4, 1.5874010519682, 4.0]
605  *    [5, 1.7099759466767, 5.0]
606  *    [6, 1.81712059283214, 6.0]
607  *    [7, 1.91293118277239, 7.0]
608  *    [8, 2.0, 8.0]
609  *    [9, 2.0800838230519, 9.0]
610  *
611  */
612 static mrb_value
613 math_cbrt(mrb_state *mrb, mrb_value obj)
614 {
615   mrb_float x;
616
617   mrb_get_args(mrb, "f", &x);
618   x = cbrt(x);
619
620   return mrb_float_value(mrb, x);
621 }
622
623
624 /*
625  *  call-seq:
626  *     Math.frexp(numeric)    -> [ fraction, exponent ]
627  *
628  *  Returns a two-element array containing the normalized fraction (a
629  *  <code>Float</code>) and exponent (a <code>Fixnum</code>) of
630  *  <i>numeric</i>.
631  *
632  *     fraction, exponent = Math.frexp(1234)   #=> [0.6025390625, 11]
633  *     fraction * 2**exponent                  #=> 1234.0
634  */
635 static mrb_value
636 math_frexp(mrb_state *mrb, mrb_value obj)
637 {
638   mrb_float x;
639   int exp;
640
641   mrb_get_args(mrb, "f", &x);
642   x = frexp(x, &exp);
643
644   return mrb_assoc_new(mrb, mrb_float_value(mrb, x), mrb_fixnum_value(exp));
645 }
646
647 /*
648  *  call-seq:
649  *     Math.ldexp(flt, int) -> float
650  *
651  *  Returns the value of <i>flt</i>*(2**<i>int</i>).
652  *
653  *     fraction, exponent = Math.frexp(1234)
654  *     Math.ldexp(fraction, exponent)   #=> 1234.0
655  */
656 static mrb_value
657 math_ldexp(mrb_state *mrb, mrb_value obj)
658 {
659   mrb_float x;
660   mrb_int   i;
661
662   mrb_get_args(mrb, "fi", &x, &i);
663   x = ldexp(x, (int)i);
664
665   return mrb_float_value(mrb, x);
666 }
667
668 /*
669  *  call-seq:
670  *     Math.hypot(x, y)    -> float
671  *
672  *  Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle
673  *  with sides <i>x</i> and <i>y</i>.
674  *
675  *     Math.hypot(3, 4)   #=> 5.0
676  */
677 static mrb_value
678 math_hypot(mrb_state *mrb, mrb_value obj)
679 {
680   mrb_float x, y;
681
682   mrb_get_args(mrb, "ff", &x, &y);
683   x = hypot(x, y);
684
685   return mrb_float_value(mrb, x);
686 }
687
688 /*
689  * call-seq:
690  *    Math.erf(x)  -> float
691  *
692  *  Calculates the error function of x.
693  */
694 static mrb_value
695 math_erf(mrb_state *mrb, mrb_value obj)
696 {
697   mrb_float x;
698
699   mrb_get_args(mrb, "f", &x);
700   x = erf(x);
701
702   return mrb_float_value(mrb, x);
703 }
704
705
706 /*
707  * call-seq:
708  *    Math.erfc(x)  -> float
709  *
710  *  Calculates the complementary error function of x.
711  */
712 static mrb_value
713 math_erfc(mrb_state *mrb, mrb_value obj)
714 {
715   mrb_float x;
716
717   mrb_get_args(mrb, "f", &x);
718   x = erfc(x);
719
720   return mrb_float_value(mrb, x);
721 }
722
723 /* ------------------------------------------------------------------------*/
724 void
725 mrb_mruby_math_gem_init(mrb_state* mrb)
726 {
727   struct RClass *mrb_math;
728   mrb_math = mrb_define_module(mrb, "Math");
729
730   mrb_define_class_under(mrb, mrb_math, "DomainError", mrb->eStandardError_class);
731
732 #ifdef M_PI
733   mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(mrb, M_PI));
734 #else
735   mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(mrb, atan(1.0)*4.0));
736 #endif
737
738 #ifdef M_E
739   mrb_define_const(mrb, mrb_math, "E", mrb_float_value(mrb, M_E));
740 #else
741   mrb_define_const(mrb, mrb_math, "E", mrb_float_value(mrb, exp(1.0)));
742 #endif
743
744   mrb_define_module_function(mrb, mrb_math, "sin", math_sin, MRB_ARGS_REQ(1));
745   mrb_define_module_function(mrb, mrb_math, "cos", math_cos, MRB_ARGS_REQ(1));
746   mrb_define_module_function(mrb, mrb_math, "tan", math_tan, MRB_ARGS_REQ(1));
747
748   mrb_define_module_function(mrb, mrb_math, "asin", math_asin, MRB_ARGS_REQ(1));
749   mrb_define_module_function(mrb, mrb_math, "acos", math_acos, MRB_ARGS_REQ(1));
750   mrb_define_module_function(mrb, mrb_math, "atan", math_atan, MRB_ARGS_REQ(1));
751   mrb_define_module_function(mrb, mrb_math, "atan2", math_atan2, MRB_ARGS_REQ(2));
752
753   mrb_define_module_function(mrb, mrb_math, "sinh", math_sinh, MRB_ARGS_REQ(1));
754   mrb_define_module_function(mrb, mrb_math, "cosh", math_cosh, MRB_ARGS_REQ(1));
755   mrb_define_module_function(mrb, mrb_math, "tanh", math_tanh, MRB_ARGS_REQ(1));
756
757   mrb_define_module_function(mrb, mrb_math, "asinh", math_asinh, MRB_ARGS_REQ(1));
758   mrb_define_module_function(mrb, mrb_math, "acosh", math_acosh, MRB_ARGS_REQ(1));
759   mrb_define_module_function(mrb, mrb_math, "atanh", math_atanh, MRB_ARGS_REQ(1));
760
761   mrb_define_module_function(mrb, mrb_math, "exp", math_exp, MRB_ARGS_REQ(1));
762   mrb_define_module_function(mrb, mrb_math, "log", math_log, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
763   mrb_define_module_function(mrb, mrb_math, "log2", math_log2, MRB_ARGS_REQ(1));
764   mrb_define_module_function(mrb, mrb_math, "log10", math_log10, MRB_ARGS_REQ(1));
765   mrb_define_module_function(mrb, mrb_math, "sqrt", math_sqrt, MRB_ARGS_REQ(1));
766   mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, MRB_ARGS_REQ(1));
767
768   mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, MRB_ARGS_REQ(1));
769   mrb_define_module_function(mrb, mrb_math, "ldexp", math_ldexp, MRB_ARGS_REQ(2));
770
771   mrb_define_module_function(mrb, mrb_math, "hypot", math_hypot, MRB_ARGS_REQ(2));
772
773   mrb_define_module_function(mrb, mrb_math, "erf",  math_erf,  MRB_ARGS_REQ(1));
774   mrb_define_module_function(mrb, mrb_math, "erfc", math_erfc, MRB_ARGS_REQ(1));
775 }
776
777 void
778 mrb_mruby_math_gem_final(mrb_state* mrb)
779 {
780 }