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