Ruby  3.1.4p223 (2023-03-30 revision HEAD)
math.c
1 /**********************************************************************
2 
3  math.c -
4 
5  $Author$
6  created at: Tue Jan 25 14:12:56 JST 1994
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9 
10 **********************************************************************/
11 
12 #include "ruby/internal/config.h"
13 
14 #ifdef _MSC_VER
15 # define _USE_MATH_DEFINES 1
16 #endif
17 
18 #include <errno.h>
19 #include <float.h>
20 #include <math.h>
21 
22 #include "internal.h"
23 #include "internal/bignum.h"
24 #include "internal/complex.h"
25 #include "internal/math.h"
26 #include "internal/object.h"
27 #include "internal/vm.h"
28 
31 
32 #define Get_Double(x) rb_num_to_dbl(x)
33 
34 #define domain_error(msg) \
35  rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " msg)
36 #define domain_check_min(val, min, msg) \
37  ((val) < (min) ? domain_error(msg) : (void)0)
38 #define domain_check_range(val, min, max, msg) \
39  ((val) < (min) || (max) < (val) ? domain_error(msg) : (void)0)
40 
41 /*
42  * call-seq:
43  * Math.atan2(y, x) -> Float
44  *
45  * Computes the arc tangent given +y+ and +x+.
46  * Returns a Float in the range -PI..PI. Return value is a angle
47  * in radians between the positive x-axis of cartesian plane
48  * and the point given by the coordinates (+x+, +y+) on it.
49  *
50  * Domain: (-INFINITY, INFINITY)
51  *
52  * Codomain: [-PI, PI]
53  *
54  * Math.atan2(-0.0, -1.0) #=> -3.141592653589793
55  * Math.atan2(-1.0, -1.0) #=> -2.356194490192345
56  * Math.atan2(-1.0, 0.0) #=> -1.5707963267948966
57  * Math.atan2(-1.0, 1.0) #=> -0.7853981633974483
58  * Math.atan2(-0.0, 1.0) #=> -0.0
59  * Math.atan2(0.0, 1.0) #=> 0.0
60  * Math.atan2(1.0, 1.0) #=> 0.7853981633974483
61  * Math.atan2(1.0, 0.0) #=> 1.5707963267948966
62  * Math.atan2(1.0, -1.0) #=> 2.356194490192345
63  * Math.atan2(0.0, -1.0) #=> 3.141592653589793
64  * Math.atan2(INFINITY, INFINITY) #=> 0.7853981633974483
65  * Math.atan2(INFINITY, -INFINITY) #=> 2.356194490192345
66  * Math.atan2(-INFINITY, INFINITY) #=> -0.7853981633974483
67  * Math.atan2(-INFINITY, -INFINITY) #=> -2.356194490192345
68  *
69  */
70 
71 static VALUE
72 math_atan2(VALUE unused_obj, VALUE y, VALUE x)
73 {
74  double dx, dy;
75  dx = Get_Double(x);
76  dy = Get_Double(y);
77  if (dx == 0.0 && dy == 0.0) {
78  if (!signbit(dx))
79  return DBL2NUM(dy);
80  if (!signbit(dy))
81  return DBL2NUM(M_PI);
82  return DBL2NUM(-M_PI);
83  }
84 #ifndef ATAN2_INF_C99
85  if (isinf(dx) && isinf(dy)) {
86  /* optimization for FLONUM */
87  if (dx < 0.0) {
88  const double dz = (3.0 * M_PI / 4.0);
89  return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
90  }
91  else {
92  const double dz = (M_PI / 4.0);
93  return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
94  }
95  }
96 #endif
97  return DBL2NUM(atan2(dy, dx));
98 }
99 
100 
101 /*
102  * call-seq:
103  * Math.cos(x) -> Float
104  *
105  * Computes the cosine of +x+ (expressed in radians).
106  * Returns a Float in the range -1.0..1.0.
107  *
108  * Domain: (-INFINITY, INFINITY)
109  *
110  * Codomain: [-1, 1]
111  *
112  * Math.cos(Math::PI) #=> -1.0
113  *
114  */
115 
116 static VALUE
117 math_cos(VALUE unused_obj, VALUE x)
118 {
119  return DBL2NUM(cos(Get_Double(x)));
120 }
121 
122 /*
123  * call-seq:
124  * Math.sin(x) -> Float
125  *
126  * Computes the sine of +x+ (expressed in radians).
127  * Returns a Float in the range -1.0..1.0.
128  *
129  * Domain: (-INFINITY, INFINITY)
130  *
131  * Codomain: [-1, 1]
132  *
133  * Math.sin(Math::PI/2) #=> 1.0
134  *
135  */
136 
137 static VALUE
138 math_sin(VALUE unused_obj, VALUE x)
139 {
140  return DBL2NUM(sin(Get_Double(x)));
141 }
142 
143 
144 /*
145  * call-seq:
146  * Math.tan(x) -> Float
147  *
148  * Computes the tangent of +x+ (expressed in radians).
149  *
150  * Domain: (-INFINITY, INFINITY)
151  *
152  * Codomain: (-INFINITY, INFINITY)
153  *
154  * Math.tan(0) #=> 0.0
155  *
156  */
157 
158 static VALUE
159 math_tan(VALUE unused_obj, VALUE x)
160 {
161  return DBL2NUM(tan(Get_Double(x)));
162 }
163 
164 /*
165  * call-seq:
166  * Math.acos(x) -> Float
167  *
168  * Computes the arc cosine of +x+. Returns 0..PI.
169  *
170  * Domain: [-1, 1]
171  *
172  * Codomain: [0, PI]
173  *
174  * Math.acos(0) == Math::PI/2 #=> true
175  *
176  */
177 
178 static VALUE
179 math_acos(VALUE unused_obj, VALUE x)
180 {
181  double d;
182 
183  d = Get_Double(x);
184  domain_check_range(d, -1.0, 1.0, "acos");
185  return DBL2NUM(acos(d));
186 }
187 
188 /*
189  * call-seq:
190  * Math.asin(x) -> Float
191  *
192  * Computes the arc sine of +x+. Returns -PI/2..PI/2.
193  *
194  * Domain: [-1, -1]
195  *
196  * Codomain: [-PI/2, PI/2]
197  *
198  * Math.asin(1) == Math::PI/2 #=> true
199  */
200 
201 static VALUE
202 math_asin(VALUE unused_obj, VALUE x)
203 {
204  double d;
205 
206  d = Get_Double(x);
207  domain_check_range(d, -1.0, 1.0, "asin");
208  return DBL2NUM(asin(d));
209 }
210 
211 /*
212  * call-seq:
213  * Math.atan(x) -> Float
214  *
215  * Computes the arc tangent of +x+. Returns -PI/2..PI/2.
216  *
217  * Domain: (-INFINITY, INFINITY)
218  *
219  * Codomain: (-PI/2, PI/2)
220  *
221  * Math.atan(0) #=> 0.0
222  */
223 
224 static VALUE
225 math_atan(VALUE unused_obj, VALUE x)
226 {
227  return DBL2NUM(atan(Get_Double(x)));
228 }
229 
230 #ifndef HAVE_COSH
231 double
232 cosh(double x)
233 {
234  return (exp(x) + exp(-x)) / 2;
235 }
236 #endif
237 
238 /*
239  * call-seq:
240  * Math.cosh(x) -> Float
241  *
242  * Computes the hyperbolic cosine of +x+ (expressed in radians).
243  *
244  * Domain: (-INFINITY, INFINITY)
245  *
246  * Codomain: [1, INFINITY)
247  *
248  * Math.cosh(0) #=> 1.0
249  *
250  */
251 
252 static VALUE
253 math_cosh(VALUE unused_obj, VALUE x)
254 {
255  return DBL2NUM(cosh(Get_Double(x)));
256 }
257 
258 #ifndef HAVE_SINH
259 double
260 sinh(double x)
261 {
262  return (exp(x) - exp(-x)) / 2;
263 }
264 #endif
265 
266 /*
267  * call-seq:
268  * Math.sinh(x) -> Float
269  *
270  * Computes the hyperbolic sine of +x+ (expressed in radians).
271  *
272  * Domain: (-INFINITY, INFINITY)
273  *
274  * Codomain: (-INFINITY, INFINITY)
275  *
276  * Math.sinh(0) #=> 0.0
277  *
278  */
279 
280 static VALUE
281 math_sinh(VALUE unused_obj, VALUE x)
282 {
283  return DBL2NUM(sinh(Get_Double(x)));
284 }
285 
286 #ifndef HAVE_TANH
287 double
288 tanh(double x)
289 {
290 # if defined(HAVE_SINH) && defined(HAVE_COSH)
291  const double c = cosh(x);
292  if (!isinf(c)) return sinh(x) / c;
293 # else
294  const double e = exp(x+x);
295  if (!isinf(e)) return (e - 1) / (e + 1);
296 # endif
297  return x > 0 ? 1.0 : -1.0;
298 }
299 #endif
300 
301 /*
302  * call-seq:
303  * Math.tanh(x) -> Float
304  *
305  * Computes the hyperbolic tangent of +x+ (expressed in radians).
306  *
307  * Domain: (-INFINITY, INFINITY)
308  *
309  * Codomain: (-1, 1)
310  *
311  * Math.tanh(0) #=> 0.0
312  *
313  */
314 
315 static VALUE
316 math_tanh(VALUE unused_obj, VALUE x)
317 {
318  return DBL2NUM(tanh(Get_Double(x)));
319 }
320 
321 /*
322  * call-seq:
323  * Math.acosh(x) -> Float
324  *
325  * Computes the inverse hyperbolic cosine of +x+.
326  *
327  * Domain: [1, INFINITY)
328  *
329  * Codomain: [0, INFINITY)
330  *
331  * Math.acosh(1) #=> 0.0
332  *
333  */
334 
335 static VALUE
336 math_acosh(VALUE unused_obj, VALUE x)
337 {
338  double d;
339 
340  d = Get_Double(x);
341  domain_check_min(d, 1.0, "acosh");
342  return DBL2NUM(acosh(d));
343 }
344 
345 /*
346  * call-seq:
347  * Math.asinh(x) -> Float
348  *
349  * Computes the inverse hyperbolic sine of +x+.
350  *
351  * Domain: (-INFINITY, INFINITY)
352  *
353  * Codomain: (-INFINITY, INFINITY)
354  *
355  * Math.asinh(1) #=> 0.881373587019543
356  *
357  */
358 
359 static VALUE
360 math_asinh(VALUE unused_obj, VALUE x)
361 {
362  return DBL2NUM(asinh(Get_Double(x)));
363 }
364 
365 /*
366  * call-seq:
367  * Math.atanh(x) -> Float
368  *
369  * Computes the inverse hyperbolic tangent of +x+.
370  *
371  * Domain: (-1, 1)
372  *
373  * Codomain: (-INFINITY, INFINITY)
374  *
375  * Math.atanh(1) #=> Infinity
376  *
377  */
378 
379 static VALUE
380 math_atanh(VALUE unused_obj, VALUE x)
381 {
382  double d;
383 
384  d = Get_Double(x);
385  domain_check_range(d, -1.0, +1.0, "atanh");
386  /* check for pole error */
387  if (d == -1.0) return DBL2NUM(-HUGE_VAL);
388  if (d == +1.0) return DBL2NUM(+HUGE_VAL);
389  return DBL2NUM(atanh(d));
390 }
391 
392 /*
393  * call-seq:
394  * Math.exp(x) -> Float
395  *
396  * Returns e**x.
397  *
398  * Domain: (-INFINITY, INFINITY)
399  *
400  * Codomain: (0, INFINITY)
401  *
402  * Math.exp(0) #=> 1.0
403  * Math.exp(1) #=> 2.718281828459045
404  * Math.exp(1.5) #=> 4.4816890703380645
405  *
406  */
407 
408 static VALUE
409 math_exp(VALUE unused_obj, VALUE x)
410 {
411  return DBL2NUM(exp(Get_Double(x)));
412 }
413 
414 #if defined __CYGWIN__
415 # include <cygwin/version.h>
416 # if CYGWIN_VERSION_DLL_MAJOR < 1005
417 # define nan(x) nan()
418 # endif
419 # define log(x) ((x) < 0.0 ? nan("") : log(x))
420 # define log10(x) ((x) < 0.0 ? nan("") : log10(x))
421 #endif
422 
423 #ifndef M_LN2
424 # define M_LN2 0.693147180559945309417232121458176568
425 #endif
426 #ifndef M_LN10
427 # define M_LN10 2.30258509299404568401799145468436421
428 #endif
429 
430 static double math_log1(VALUE x);
431 FUNC_MINIMIZED(static VALUE math_log(int, const VALUE *, VALUE));
432 
433 /*
434  * call-seq:
435  * Math.log(x) -> Float
436  * Math.log(x, base) -> Float
437  *
438  * Returns the logarithm of +x+.
439  * If additional second argument is given, it will be the base
440  * of logarithm. Otherwise it is +e+ (for the natural logarithm).
441  *
442  * Domain: (0, INFINITY)
443  *
444  * Codomain: (-INFINITY, INFINITY)
445  *
446  * Math.log(0) #=> -Infinity
447  * Math.log(1) #=> 0.0
448  * Math.log(Math::E) #=> 1.0
449  * Math.log(Math::E**3) #=> 3.0
450  * Math.log(12, 3) #=> 2.2618595071429146
451  *
452  */
453 
454 static VALUE
455 math_log(int argc, const VALUE *argv, VALUE unused_obj)
456 {
457  return rb_math_log(argc, argv);
458 }
459 
460 VALUE
461 rb_math_log(int argc, const VALUE *argv)
462 {
463  VALUE x, base;
464  double d;
465 
466  rb_scan_args(argc, argv, "11", &x, &base);
467  d = math_log1(x);
468  if (argc == 2) {
469  d /= math_log1(base);
470  }
471  return DBL2NUM(d);
472 }
473 
474 static double
475 get_double_rshift(VALUE x, size_t *pnumbits)
476 {
477  size_t numbits;
478 
479  if (RB_BIGNUM_TYPE_P(x) && BIGNUM_POSITIVE_P(x) &&
480  DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
481  numbits -= DBL_MANT_DIG;
482  x = rb_big_rshift(x, SIZET2NUM(numbits));
483  }
484  else {
485  numbits = 0;
486  }
487  *pnumbits = numbits;
488  return Get_Double(x);
489 }
490 
491 static double
492 math_log1(VALUE x)
493 {
494  size_t numbits;
495  double d = get_double_rshift(x, &numbits);
496 
497  domain_check_min(d, 0.0, "log");
498  /* check for pole error */
499  if (d == 0.0) return -HUGE_VAL;
500 
501  return log(d) + numbits * M_LN2; /* log(d * 2 ** numbits) */
502 }
503 
504 #ifndef log2
505 #ifndef HAVE_LOG2
506 double
507 log2(double x)
508 {
509  return log10(x)/log10(2.0);
510 }
511 #else
512 extern double log2(double);
513 #endif
514 #endif
515 
516 /*
517  * call-seq:
518  * Math.log2(x) -> Float
519  *
520  * Returns the base 2 logarithm of +x+.
521  *
522  * Domain: (0, INFINITY)
523  *
524  * Codomain: (-INFINITY, INFINITY)
525  *
526  * Math.log2(1) #=> 0.0
527  * Math.log2(2) #=> 1.0
528  * Math.log2(32768) #=> 15.0
529  * Math.log2(65536) #=> 16.0
530  *
531  */
532 
533 static VALUE
534 math_log2(VALUE unused_obj, VALUE x)
535 {
536  size_t numbits;
537  double d = get_double_rshift(x, &numbits);
538 
539  domain_check_min(d, 0.0, "log2");
540  /* check for pole error */
541  if (d == 0.0) return DBL2NUM(-HUGE_VAL);
542 
543  return DBL2NUM(log2(d) + numbits); /* log2(d * 2 ** numbits) */
544 }
545 
546 /*
547  * call-seq:
548  * Math.log10(x) -> Float
549  *
550  * Returns the base 10 logarithm of +x+.
551  *
552  * Domain: (0, INFINITY)
553  *
554  * Codomain: (-INFINITY, INFINITY)
555  *
556  * Math.log10(1) #=> 0.0
557  * Math.log10(10) #=> 1.0
558  * Math.log10(10**100) #=> 100.0
559  *
560  */
561 
562 static VALUE
563 math_log10(VALUE unused_obj, VALUE x)
564 {
565  size_t numbits;
566  double d = get_double_rshift(x, &numbits);
567 
568  domain_check_min(d, 0.0, "log10");
569  /* check for pole error */
570  if (d == 0.0) return DBL2NUM(-HUGE_VAL);
571 
572  return DBL2NUM(log10(d) + numbits * log10(2)); /* log10(d * 2 ** numbits) */
573 }
574 
575 static VALUE rb_math_sqrt(VALUE x);
576 
577 /*
578  * call-seq:
579  * Math.sqrt(x) -> Float
580  *
581  * Returns the non-negative square root of +x+.
582  *
583  * Domain: [0, INFINITY)
584  *
585  * Codomain:[0, INFINITY)
586  *
587  * 0.upto(10) {|x|
588  * p [x, Math.sqrt(x), Math.sqrt(x)**2]
589  * }
590  * #=> [0, 0.0, 0.0]
591  * # [1, 1.0, 1.0]
592  * # [2, 1.4142135623731, 2.0]
593  * # [3, 1.73205080756888, 3.0]
594  * # [4, 2.0, 4.0]
595  * # [5, 2.23606797749979, 5.0]
596  * # [6, 2.44948974278318, 6.0]
597  * # [7, 2.64575131106459, 7.0]
598  * # [8, 2.82842712474619, 8.0]
599  * # [9, 3.0, 9.0]
600  * # [10, 3.16227766016838, 10.0]
601  *
602  * Note that the limited precision of floating point arithmetic
603  * might lead to surprising results:
604  *
605  * Math.sqrt(10**46).to_i #=> 99999999999999991611392 (!)
606  *
607  * See also BigDecimal#sqrt and Integer.sqrt.
608  */
609 
610 static VALUE
611 math_sqrt(VALUE unused_obj, VALUE x)
612 {
613  return rb_math_sqrt(x);
614 }
615 
616 inline static VALUE
617 f_negative_p(VALUE x)
618 {
619  if (FIXNUM_P(x))
620  return RBOOL(FIX2LONG(x) < 0);
621  return rb_funcall(x, '<', 1, INT2FIX(0));
622 }
623 inline static VALUE
624 f_signbit(VALUE x)
625 {
626  if (RB_FLOAT_TYPE_P(x)) {
627  double f = RFLOAT_VALUE(x);
628  return RBOOL(!isnan(f) && signbit(f));
629  }
630  return f_negative_p(x);
631 }
632 
633 static VALUE
634 rb_math_sqrt(VALUE x)
635 {
636  double d;
637 
638  if (RB_TYPE_P(x, T_COMPLEX)) {
639  VALUE neg = f_signbit(RCOMPLEX(x)->imag);
640  double re = Get_Double(RCOMPLEX(x)->real), im;
641  d = Get_Double(rb_complex_abs(x));
642  im = sqrt((d - re) / 2.0);
643  re = sqrt((d + re) / 2.0);
644  if (neg) im = -im;
645  return rb_complex_new(DBL2NUM(re), DBL2NUM(im));
646  }
647  d = Get_Double(x);
648  domain_check_min(d, 0.0, "sqrt");
649  if (d == 0.0) return DBL2NUM(0.0);
650  return DBL2NUM(sqrt(d));
651 }
652 
653 /*
654  * call-seq:
655  * Math.cbrt(x) -> Float
656  *
657  * Returns the cube root of +x+.
658  *
659  * Domain: (-INFINITY, INFINITY)
660  *
661  * Codomain: (-INFINITY, INFINITY)
662  *
663  * -9.upto(9) {|x|
664  * p [x, Math.cbrt(x), Math.cbrt(x)**3]
665  * }
666  * #=> [-9, -2.0800838230519, -9.0]
667  * # [-8, -2.0, -8.0]
668  * # [-7, -1.91293118277239, -7.0]
669  * # [-6, -1.81712059283214, -6.0]
670  * # [-5, -1.7099759466767, -5.0]
671  * # [-4, -1.5874010519682, -4.0]
672  * # [-3, -1.44224957030741, -3.0]
673  * # [-2, -1.25992104989487, -2.0]
674  * # [-1, -1.0, -1.0]
675  * # [0, 0.0, 0.0]
676  * # [1, 1.0, 1.0]
677  * # [2, 1.25992104989487, 2.0]
678  * # [3, 1.44224957030741, 3.0]
679  * # [4, 1.5874010519682, 4.0]
680  * # [5, 1.7099759466767, 5.0]
681  * # [6, 1.81712059283214, 6.0]
682  * # [7, 1.91293118277239, 7.0]
683  * # [8, 2.0, 8.0]
684  * # [9, 2.0800838230519, 9.0]
685  *
686  */
687 
688 static VALUE
689 math_cbrt(VALUE unused_obj, VALUE x)
690 {
691  double f = Get_Double(x);
692  double r = cbrt(f);
693 #if defined __GLIBC__
694  if (isfinite(r) && !(f == 0.0 && r == 0.0)) {
695  r = (2.0 * r + (f / r / r)) / 3.0;
696  }
697 #endif
698  return DBL2NUM(r);
699 }
700 
701 /*
702  * call-seq:
703  * Math.frexp(x) -> [fraction, exponent]
704  *
705  * Returns a two-element array containing the normalized fraction (a Float)
706  * and exponent (an Integer) of +x+.
707  *
708  * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11]
709  * fraction * 2**exponent #=> 1234.0
710  */
711 
712 static VALUE
713 math_frexp(VALUE unused_obj, VALUE x)
714 {
715  double d;
716  int exp;
717 
718  d = frexp(Get_Double(x), &exp);
719  return rb_assoc_new(DBL2NUM(d), INT2NUM(exp));
720 }
721 
722 /*
723  * call-seq:
724  * Math.ldexp(fraction, exponent) -> float
725  *
726  * Returns the value of +fraction+*(2**+exponent+).
727  *
728  * fraction, exponent = Math.frexp(1234)
729  * Math.ldexp(fraction, exponent) #=> 1234.0
730  */
731 
732 static VALUE
733 math_ldexp(VALUE unused_obj, VALUE x, VALUE n)
734 {
735  return DBL2NUM(ldexp(Get_Double(x), NUM2INT(n)));
736 }
737 
738 /*
739  * call-seq:
740  * Math.hypot(x, y) -> Float
741  *
742  * Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle with
743  * sides +x+ and +y+.
744  *
745  * Math.hypot(3, 4) #=> 5.0
746  */
747 
748 static VALUE
749 math_hypot(VALUE unused_obj, VALUE x, VALUE y)
750 {
751  return DBL2NUM(hypot(Get_Double(x), Get_Double(y)));
752 }
753 
754 /*
755  * call-seq:
756  * Math.erf(x) -> Float
757  *
758  * Calculates the error function of +x+.
759  *
760  * Domain: (-INFINITY, INFINITY)
761  *
762  * Codomain: (-1, 1)
763  *
764  * Math.erf(0) #=> 0.0
765  *
766  */
767 
768 static VALUE
769 math_erf(VALUE unused_obj, VALUE x)
770 {
771  return DBL2NUM(erf(Get_Double(x)));
772 }
773 
774 /*
775  * call-seq:
776  * Math.erfc(x) -> Float
777  *
778  * Calculates the complementary error function of x.
779  *
780  * Domain: (-INFINITY, INFINITY)
781  *
782  * Codomain: (0, 2)
783  *
784  * Math.erfc(0) #=> 1.0
785  *
786  */
787 
788 static VALUE
789 math_erfc(VALUE unused_obj, VALUE x)
790 {
791  return DBL2NUM(erfc(Get_Double(x)));
792 }
793 
794 /*
795  * call-seq:
796  * Math.gamma(x) -> Float
797  *
798  * Calculates the gamma function of x.
799  *
800  * Note that gamma(n) is the same as fact(n-1) for integer n > 0.
801  * However gamma(n) returns float and can be an approximation.
802  *
803  * def fact(n) (1..n).inject(1) {|r,i| r*i } end
804  * 1.upto(26) {|i| p [i, Math.gamma(i), fact(i-1)] }
805  * #=> [1, 1.0, 1]
806  * # [2, 1.0, 1]
807  * # [3, 2.0, 2]
808  * # [4, 6.0, 6]
809  * # [5, 24.0, 24]
810  * # [6, 120.0, 120]
811  * # [7, 720.0, 720]
812  * # [8, 5040.0, 5040]
813  * # [9, 40320.0, 40320]
814  * # [10, 362880.0, 362880]
815  * # [11, 3628800.0, 3628800]
816  * # [12, 39916800.0, 39916800]
817  * # [13, 479001600.0, 479001600]
818  * # [14, 6227020800.0, 6227020800]
819  * # [15, 87178291200.0, 87178291200]
820  * # [16, 1307674368000.0, 1307674368000]
821  * # [17, 20922789888000.0, 20922789888000]
822  * # [18, 355687428096000.0, 355687428096000]
823  * # [19, 6.402373705728e+15, 6402373705728000]
824  * # [20, 1.21645100408832e+17, 121645100408832000]
825  * # [21, 2.43290200817664e+18, 2432902008176640000]
826  * # [22, 5.109094217170944e+19, 51090942171709440000]
827  * # [23, 1.1240007277776077e+21, 1124000727777607680000]
828  * # [24, 2.5852016738885062e+22, 25852016738884976640000]
829  * # [25, 6.204484017332391e+23, 620448401733239439360000]
830  * # [26, 1.5511210043330954e+25, 15511210043330985984000000]
831  *
832  */
833 
834 static VALUE
835 math_gamma(VALUE unused_obj, VALUE x)
836 {
837  static const double fact_table[] = {
838  /* fact(0) */ 1.0,
839  /* fact(1) */ 1.0,
840  /* fact(2) */ 2.0,
841  /* fact(3) */ 6.0,
842  /* fact(4) */ 24.0,
843  /* fact(5) */ 120.0,
844  /* fact(6) */ 720.0,
845  /* fact(7) */ 5040.0,
846  /* fact(8) */ 40320.0,
847  /* fact(9) */ 362880.0,
848  /* fact(10) */ 3628800.0,
849  /* fact(11) */ 39916800.0,
850  /* fact(12) */ 479001600.0,
851  /* fact(13) */ 6227020800.0,
852  /* fact(14) */ 87178291200.0,
853  /* fact(15) */ 1307674368000.0,
854  /* fact(16) */ 20922789888000.0,
855  /* fact(17) */ 355687428096000.0,
856  /* fact(18) */ 6402373705728000.0,
857  /* fact(19) */ 121645100408832000.0,
858  /* fact(20) */ 2432902008176640000.0,
859  /* fact(21) */ 51090942171709440000.0,
860  /* fact(22) */ 1124000727777607680000.0,
861  /* fact(23)=25852016738884976640000 needs 56bit mantissa which is
862  * impossible to represent exactly in IEEE 754 double which have
863  * 53bit mantissa. */
864  };
865  enum {NFACT_TABLE = numberof(fact_table)};
866  double d;
867  d = Get_Double(x);
868  /* check for domain error */
869  if (isinf(d)) {
870  if (signbit(d)) domain_error("gamma");
871  return DBL2NUM(HUGE_VAL);
872  }
873  if (d == 0.0) {
874  return signbit(d) ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
875  }
876  if (d == floor(d)) {
877  domain_check_min(d, 0.0, "gamma");
878  if (1.0 <= d && d <= (double)NFACT_TABLE) {
879  return DBL2NUM(fact_table[(int)d - 1]);
880  }
881  }
882  return DBL2NUM(tgamma(d));
883 }
884 
885 /*
886  * call-seq:
887  * Math.lgamma(x) -> [float, -1 or 1]
888  *
889  * Calculates the logarithmic gamma of +x+ and the sign of gamma of +x+.
890  *
891  * Math.lgamma(x) is the same as
892  * [Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]
893  * but avoids overflow by Math.gamma(x) for large x.
894  *
895  * Math.lgamma(0) #=> [Infinity, 1]
896  *
897  */
898 
899 static VALUE
900 math_lgamma(VALUE unused_obj, VALUE x)
901 {
902  double d;
903  int sign=1;
904  VALUE v;
905  d = Get_Double(x);
906  /* check for domain error */
907  if (isinf(d)) {
908  if (signbit(d)) domain_error("lgamma");
909  return rb_assoc_new(DBL2NUM(HUGE_VAL), INT2FIX(1));
910  }
911  if (d == 0.0) {
912  VALUE vsign = signbit(d) ? INT2FIX(-1) : INT2FIX(+1);
913  return rb_assoc_new(DBL2NUM(HUGE_VAL), vsign);
914  }
915  v = DBL2NUM(lgamma_r(d, &sign));
916  return rb_assoc_new(v, INT2FIX(sign));
917 }
918 
919 
920 #define exp1(n) \
921 VALUE \
922 rb_math_##n(VALUE x)\
923 {\
924  return math_##n(0, x);\
925 }
926 
927 #define exp2(n) \
928 VALUE \
929 rb_math_##n(VALUE x, VALUE y)\
930 {\
931  return math_##n(0, x, y);\
932 }
933 
934 exp2(atan2)
935 exp1(cos)
936 exp1(cosh)
937 exp1(exp)
938 exp2(hypot)
939 exp1(sin)
940 exp1(sinh)
941 #if 0
942 exp1(sqrt)
943 #endif
944 
945 
946 /*
947  * Document-class: Math::DomainError
948  *
949  * Raised when a mathematical function is evaluated outside of its
950  * domain of definition.
951  *
952  * For example, since +cos+ returns values in the range -1..1,
953  * its inverse function +acos+ is only defined on that interval:
954  *
955  * Math.acos(42)
956  *
957  * <em>produces:</em>
958  *
959  * Math::DomainError: Numerical argument is out of domain - "acos"
960  */
961 
962 /*
963  * Document-class: Math
964  *
965  * The Math module contains module functions for basic
966  * trigonometric and transcendental functions. See class
967  * Float for a list of constants that
968  * define Ruby's floating point accuracy.
969  *
970  * Domains and codomains are given only for real (not complex) numbers.
971  */
972 
973 
974 void
975 InitVM_Math(void)
976 {
977  rb_mMath = rb_define_module("Math");
979 
980  /* Definition of the mathematical constant PI as a Float number. */
981  rb_define_const(rb_mMath, "PI", DBL2NUM(M_PI));
982 
983 #ifdef M_E
984  /* Definition of the mathematical constant E for Euler's number (e) as a Float number. */
985  rb_define_const(rb_mMath, "E", DBL2NUM(M_E));
986 #else
987  rb_define_const(rb_mMath, "E", DBL2NUM(exp(1.0)));
988 #endif
989 
990  rb_define_module_function(rb_mMath, "atan2", math_atan2, 2);
991  rb_define_module_function(rb_mMath, "cos", math_cos, 1);
992  rb_define_module_function(rb_mMath, "sin", math_sin, 1);
993  rb_define_module_function(rb_mMath, "tan", math_tan, 1);
994 
995  rb_define_module_function(rb_mMath, "acos", math_acos, 1);
996  rb_define_module_function(rb_mMath, "asin", math_asin, 1);
997  rb_define_module_function(rb_mMath, "atan", math_atan, 1);
998 
999  rb_define_module_function(rb_mMath, "cosh", math_cosh, 1);
1000  rb_define_module_function(rb_mMath, "sinh", math_sinh, 1);
1001  rb_define_module_function(rb_mMath, "tanh", math_tanh, 1);
1002 
1003  rb_define_module_function(rb_mMath, "acosh", math_acosh, 1);
1004  rb_define_module_function(rb_mMath, "asinh", math_asinh, 1);
1005  rb_define_module_function(rb_mMath, "atanh", math_atanh, 1);
1006 
1007  rb_define_module_function(rb_mMath, "exp", math_exp, 1);
1008  rb_define_module_function(rb_mMath, "log", math_log, -1);
1009  rb_define_module_function(rb_mMath, "log2", math_log2, 1);
1010  rb_define_module_function(rb_mMath, "log10", math_log10, 1);
1011  rb_define_module_function(rb_mMath, "sqrt", math_sqrt, 1);
1012  rb_define_module_function(rb_mMath, "cbrt", math_cbrt, 1);
1013 
1014  rb_define_module_function(rb_mMath, "frexp", math_frexp, 1);
1015  rb_define_module_function(rb_mMath, "ldexp", math_ldexp, 2);
1016 
1017  rb_define_module_function(rb_mMath, "hypot", math_hypot, 2);
1018 
1019  rb_define_module_function(rb_mMath, "erf", math_erf, 1);
1020  rb_define_module_function(rb_mMath, "erfc", math_erfc, 1);
1021 
1022  rb_define_module_function(rb_mMath, "gamma", math_gamma, 1);
1023  rb_define_module_function(rb_mMath, "lgamma", math_lgamma, 1);
1024 }
1025 
1026 void
1027 Init_Math(void)
1028 {
1029  InitVM(Math);
1030 }
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:869
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition: class.c:948
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for a module.
Definition: class.c:2100
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition: class.c:2406
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition: value_type.h:59
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition: double.h:28
#define INT2FIX
Old name of RB_INT2FIX.
Definition: long.h:48
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition: size_t.h:62
#define NUM2INT
Old name of RB_NUM2INT.
Definition: int.h:44
#define INT2NUM
Old name of RB_INT2NUM.
Definition: int.h:43
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition: long.h:46
#define DBL2NUM
Old name of rb_float_new.
Definition: double.h:29
#define FIXNUM_P
Old name of RB_FIXNUM_P.
VALUE rb_eStandardError
StandardError exception.
Definition: error.c:1096
VALUE rb_eMathDomainError
Math::DomainError exception.
Definition: math.c:30
VALUE rb_mMath
Math module.
Definition: math.c:29
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition: vm_eval.c:1102
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
Definition: array.c:976
size_t rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret)
Calculates the number of words needed represent the absolute value of the passed integer.
Definition: bignum.c:3393
VALUE rb_big_rshift(VALUE x, VALUE y)
Performs shift right.
Definition: bignum.c:6648
VALUE rb_complex_new(VALUE real, VALUE imag)
Constructs a Complex, by first multiplying the imaginary part with 1i then adds it to the real part.
Definition: complex.c:1528
VALUE rb_complex_abs(VALUE z)
Queries the absolute (or the magnitude) of the passed object.
Definition: complex.c:1161
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
Definition: variable.c:3253
#define InitVM(ext)
This macro is for internal use.
Definition: ruby.h:229
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
Definition: value_type.h:263
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition: value_type.h:375