Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-math/src/math.c

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