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