[ruby/win32ole] Undefine allocator of WIN32OLE_VARIABLE to get rid of warning
[ruby-80x24.org.git] / math.c
blobcce29b526ba196046e13c2e624705784732c3128
1 /**********************************************************************
3 math.c -
5 $Author$
6 created at: Tue Jan 25 14:12:56 JST 1994
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/internal/config.h"
14 #ifdef _MSC_VER
15 # define _USE_MATH_DEFINES 1
16 #endif
18 #include <errno.h>
19 #include <float.h>
20 #include <math.h>
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"
29 VALUE rb_mMath;
30 VALUE rb_eMathDomainError;
32 #define Get_Double(x) rb_num_to_dbl(x)
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)
42 * call-seq:
43 * Math.atan2(y, x) -> Float
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.
50 * Domain: (-INFINITY, INFINITY)
52 * Codomain: [-PI, PI]
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
71 static VALUE
72 math_atan2(VALUE unused_obj, VALUE y, VALUE x)
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);
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);
91 else {
92 const double dz = (M_PI / 4.0);
93 return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
96 #endif
97 return DBL2NUM(atan2(dy, dx));
102 * call-seq:
103 * Math.cos(x) -> Float
105 * Computes the cosine of +x+ (expressed in radians).
106 * Returns a Float in the range -1.0..1.0.
108 * Domain: (-INFINITY, INFINITY)
110 * Codomain: [-1, 1]
112 * Math.cos(Math::PI) #=> -1.0
116 static VALUE
117 math_cos(VALUE unused_obj, VALUE x)
119 return DBL2NUM(cos(Get_Double(x)));
123 * call-seq:
124 * Math.sin(x) -> Float
126 * Computes the sine of +x+ (expressed in radians).
127 * Returns a Float in the range -1.0..1.0.
129 * Domain: (-INFINITY, INFINITY)
131 * Codomain: [-1, 1]
133 * Math.sin(Math::PI/2) #=> 1.0
137 static VALUE
138 math_sin(VALUE unused_obj, VALUE x)
140 return DBL2NUM(sin(Get_Double(x)));
145 * call-seq:
146 * Math.tan(x) -> Float
148 * Computes the tangent of +x+ (expressed in radians).
150 * Domain: (-INFINITY, INFINITY)
152 * Codomain: (-INFINITY, INFINITY)
154 * Math.tan(0) #=> 0.0
158 static VALUE
159 math_tan(VALUE unused_obj, VALUE x)
161 return DBL2NUM(tan(Get_Double(x)));
165 * call-seq:
166 * Math.acos(x) -> Float
168 * Computes the arc cosine of +x+. Returns 0..PI.
170 * Domain: [-1, 1]
172 * Codomain: [0, PI]
174 * Math.acos(0) == Math::PI/2 #=> true
178 static VALUE
179 math_acos(VALUE unused_obj, VALUE x)
181 double d;
183 d = Get_Double(x);
184 domain_check_range(d, -1.0, 1.0, "acos");
185 return DBL2NUM(acos(d));
189 * call-seq:
190 * Math.asin(x) -> Float
192 * Computes the arc sine of +x+. Returns -PI/2..PI/2.
194 * Domain: [-1, -1]
196 * Codomain: [-PI/2, PI/2]
198 * Math.asin(1) == Math::PI/2 #=> true
201 static VALUE
202 math_asin(VALUE unused_obj, VALUE x)
204 double d;
206 d = Get_Double(x);
207 domain_check_range(d, -1.0, 1.0, "asin");
208 return DBL2NUM(asin(d));
212 * call-seq:
213 * Math.atan(x) -> Float
215 * Computes the arc tangent of +x+. Returns -PI/2..PI/2.
217 * Domain: (-INFINITY, INFINITY)
219 * Codomain: (-PI/2, PI/2)
221 * Math.atan(0) #=> 0.0
224 static VALUE
225 math_atan(VALUE unused_obj, VALUE x)
227 return DBL2NUM(atan(Get_Double(x)));
230 #ifndef HAVE_COSH
231 double
232 cosh(double x)
234 return (exp(x) + exp(-x)) / 2;
236 #endif
239 * call-seq:
240 * Math.cosh(x) -> Float
242 * Computes the hyperbolic cosine of +x+ (expressed in radians).
244 * Domain: (-INFINITY, INFINITY)
246 * Codomain: [1, INFINITY)
248 * Math.cosh(0) #=> 1.0
252 static VALUE
253 math_cosh(VALUE unused_obj, VALUE x)
255 return DBL2NUM(cosh(Get_Double(x)));
258 #ifndef HAVE_SINH
259 double
260 sinh(double x)
262 return (exp(x) - exp(-x)) / 2;
264 #endif
267 * call-seq:
268 * Math.sinh(x) -> Float
270 * Computes the hyperbolic sine of +x+ (expressed in radians).
272 * Domain: (-INFINITY, INFINITY)
274 * Codomain: (-INFINITY, INFINITY)
276 * Math.sinh(0) #=> 0.0
280 static VALUE
281 math_sinh(VALUE unused_obj, VALUE x)
283 return DBL2NUM(sinh(Get_Double(x)));
286 #ifndef HAVE_TANH
287 double
288 tanh(double x)
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;
299 #endif
302 * call-seq:
303 * Math.tanh(x) -> Float
305 * Computes the hyperbolic tangent of +x+ (expressed in radians).
307 * Domain: (-INFINITY, INFINITY)
309 * Codomain: (-1, 1)
311 * Math.tanh(0) #=> 0.0
315 static VALUE
316 math_tanh(VALUE unused_obj, VALUE x)
318 return DBL2NUM(tanh(Get_Double(x)));
322 * call-seq:
323 * Math.acosh(x) -> Float
325 * Computes the inverse hyperbolic cosine of +x+.
327 * Domain: [1, INFINITY)
329 * Codomain: [0, INFINITY)
331 * Math.acosh(1) #=> 0.0
335 static VALUE
336 math_acosh(VALUE unused_obj, VALUE x)
338 double d;
340 d = Get_Double(x);
341 domain_check_min(d, 1.0, "acosh");
342 return DBL2NUM(acosh(d));
346 * call-seq:
347 * Math.asinh(x) -> Float
349 * Computes the inverse hyperbolic sine of +x+.
351 * Domain: (-INFINITY, INFINITY)
353 * Codomain: (-INFINITY, INFINITY)
355 * Math.asinh(1) #=> 0.881373587019543
359 static VALUE
360 math_asinh(VALUE unused_obj, VALUE x)
362 return DBL2NUM(asinh(Get_Double(x)));
366 * call-seq:
367 * Math.atanh(x) -> Float
369 * Computes the inverse hyperbolic tangent of +x+.
371 * Domain: (-1, 1)
373 * Codomain: (-INFINITY, INFINITY)
375 * Math.atanh(1) #=> Infinity
379 static VALUE
380 math_atanh(VALUE unused_obj, VALUE x)
382 double d;
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));
393 * call-seq:
394 * Math.exp(x) -> Float
396 * Returns e**x.
398 * Domain: (-INFINITY, INFINITY)
400 * Codomain: (0, INFINITY)
402 * Math.exp(0) #=> 1.0
403 * Math.exp(1) #=> 2.718281828459045
404 * Math.exp(1.5) #=> 4.4816890703380645
408 static VALUE
409 math_exp(VALUE unused_obj, VALUE x)
411 return DBL2NUM(exp(Get_Double(x)));
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
423 #ifndef M_LN2
424 # define M_LN2 0.693147180559945309417232121458176568
425 #endif
426 #ifndef M_LN10
427 # define M_LN10 2.30258509299404568401799145468436421
428 #endif
430 static double math_log1(VALUE x);
431 FUNC_MINIMIZED(static VALUE math_log(int, const VALUE *, VALUE));
434 * call-seq:
435 * Math.log(x) -> Float
436 * Math.log(x, base) -> Float
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).
442 * Domain: (0, INFINITY)
444 * Codomain: (-INFINITY, INFINITY)
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
454 static VALUE
455 math_log(int argc, const VALUE *argv, VALUE unused_obj)
457 return rb_math_log(argc, argv);
460 VALUE
461 rb_math_log(int argc, const VALUE *argv)
463 VALUE x, base;
464 double d;
466 rb_scan_args(argc, argv, "11", &x, &base);
467 d = math_log1(x);
468 if (argc == 2) {
469 d /= math_log1(base);
471 return DBL2NUM(d);
474 static double
475 get_double_rshift(VALUE x, size_t *pnumbits)
477 size_t numbits;
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));
484 else {
485 numbits = 0;
487 *pnumbits = numbits;
488 return Get_Double(x);
491 static double
492 math_log1(VALUE x)
494 size_t numbits;
495 double d = get_double_rshift(x, &numbits);
497 domain_check_min(d, 0.0, "log");
498 /* check for pole error */
499 if (d == 0.0) return -HUGE_VAL;
501 return log(d) + numbits * M_LN2; /* log(d * 2 ** numbits) */
504 #ifndef log2
505 #ifndef HAVE_LOG2
506 double
507 log2(double x)
509 return log10(x)/log10(2.0);
511 #else
512 extern double log2(double);
513 #endif
514 #endif
517 * call-seq:
518 * Math.log2(x) -> Float
520 * Returns the base 2 logarithm of +x+.
522 * Domain: (0, INFINITY)
524 * Codomain: (-INFINITY, INFINITY)
526 * Math.log2(1) #=> 0.0
527 * Math.log2(2) #=> 1.0
528 * Math.log2(32768) #=> 15.0
529 * Math.log2(65536) #=> 16.0
533 static VALUE
534 math_log2(VALUE unused_obj, VALUE x)
536 size_t numbits;
537 double d = get_double_rshift(x, &numbits);
539 domain_check_min(d, 0.0, "log2");
540 /* check for pole error */
541 if (d == 0.0) return DBL2NUM(-HUGE_VAL);
543 return DBL2NUM(log2(d) + numbits); /* log2(d * 2 ** numbits) */
547 * call-seq:
548 * Math.log10(x) -> Float
550 * Returns the base 10 logarithm of +x+.
552 * Domain: (0, INFINITY)
554 * Codomain: (-INFINITY, INFINITY)
556 * Math.log10(1) #=> 0.0
557 * Math.log10(10) #=> 1.0
558 * Math.log10(10**100) #=> 100.0
562 static VALUE
563 math_log10(VALUE unused_obj, VALUE x)
565 size_t numbits;
566 double d = get_double_rshift(x, &numbits);
568 domain_check_min(d, 0.0, "log10");
569 /* check for pole error */
570 if (d == 0.0) return DBL2NUM(-HUGE_VAL);
572 return DBL2NUM(log10(d) + numbits * log10(2)); /* log10(d * 2 ** numbits) */
575 static VALUE rb_math_sqrt(VALUE x);
578 * call-seq:
579 * Math.sqrt(x) -> Float
581 * Returns the non-negative square root of +x+.
583 * Domain: [0, INFINITY)
585 * Codomain:[0, INFINITY)
587 * 0.upto(10) {|x|
588 * p [x, Math.sqrt(x), Math.sqrt(x)**2]
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]
602 * Note that the limited precision of floating point arithmetic
603 * might lead to surprising results:
605 * Math.sqrt(10**46).to_i #=> 99999999999999991611392 (!)
607 * See also BigDecimal#sqrt and Integer.sqrt.
610 static VALUE
611 math_sqrt(VALUE unused_obj, VALUE x)
613 return rb_math_sqrt(x);
616 inline static VALUE
617 f_negative_p(VALUE x)
619 if (FIXNUM_P(x))
620 return RBOOL(FIX2LONG(x) < 0);
621 return rb_funcall(x, '<', 1, INT2FIX(0));
623 inline static VALUE
624 f_signbit(VALUE x)
626 if (RB_FLOAT_TYPE_P(x)) {
627 double f = RFLOAT_VALUE(x);
628 return RBOOL(!isnan(f) && signbit(f));
630 return f_negative_p(x);
633 static VALUE
634 rb_math_sqrt(VALUE x)
636 double d;
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));
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));
654 * call-seq:
655 * Math.cbrt(x) -> Float
657 * Returns the cube root of +x+.
659 * Domain: (-INFINITY, INFINITY)
661 * Codomain: (-INFINITY, INFINITY)
663 * -9.upto(9) {|x|
664 * p [x, Math.cbrt(x), Math.cbrt(x)**3]
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]
688 static VALUE
689 math_cbrt(VALUE unused_obj, VALUE x)
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;
697 #endif
698 return DBL2NUM(r);
702 * call-seq:
703 * Math.frexp(x) -> [fraction, exponent]
705 * Returns a two-element array containing the normalized fraction (a Float)
706 * and exponent (an Integer) of +x+.
708 * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11]
709 * fraction * 2**exponent #=> 1234.0
712 static VALUE
713 math_frexp(VALUE unused_obj, VALUE x)
715 double d;
716 int exp;
718 d = frexp(Get_Double(x), &exp);
719 return rb_assoc_new(DBL2NUM(d), INT2NUM(exp));
723 * call-seq:
724 * Math.ldexp(fraction, exponent) -> float
726 * Returns the value of +fraction+*(2**+exponent+).
728 * fraction, exponent = Math.frexp(1234)
729 * Math.ldexp(fraction, exponent) #=> 1234.0
732 static VALUE
733 math_ldexp(VALUE unused_obj, VALUE x, VALUE n)
735 return DBL2NUM(ldexp(Get_Double(x), NUM2INT(n)));
739 * call-seq:
740 * Math.hypot(x, y) -> Float
742 * Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle with
743 * sides +x+ and +y+.
745 * Math.hypot(3, 4) #=> 5.0
748 static VALUE
749 math_hypot(VALUE unused_obj, VALUE x, VALUE y)
751 return DBL2NUM(hypot(Get_Double(x), Get_Double(y)));
755 * call-seq:
756 * Math.erf(x) -> Float
758 * Calculates the error function of +x+.
760 * Domain: (-INFINITY, INFINITY)
762 * Codomain: (-1, 1)
764 * Math.erf(0) #=> 0.0
768 static VALUE
769 math_erf(VALUE unused_obj, VALUE x)
771 return DBL2NUM(erf(Get_Double(x)));
775 * call-seq:
776 * Math.erfc(x) -> Float
778 * Calculates the complementary error function of x.
780 * Domain: (-INFINITY, INFINITY)
782 * Codomain: (0, 2)
784 * Math.erfc(0) #=> 1.0
788 static VALUE
789 math_erfc(VALUE unused_obj, VALUE x)
791 return DBL2NUM(erfc(Get_Double(x)));
795 * call-seq:
796 * Math.gamma(x) -> Float
798 * Calculates the gamma function of x.
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.
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]
834 static VALUE
835 math_gamma(VALUE unused_obj, VALUE x)
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. */
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);
873 if (d == 0.0) {
874 return signbit(d) ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
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]);
882 return DBL2NUM(tgamma(d));
886 * call-seq:
887 * Math.lgamma(x) -> [float, -1 or 1]
889 * Calculates the logarithmic gamma of +x+ and the sign of gamma of +x+.
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.
895 * Math.lgamma(0) #=> [Infinity, 1]
899 static VALUE
900 math_lgamma(VALUE unused_obj, VALUE x)
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));
911 if (d == 0.0) {
912 VALUE vsign = signbit(d) ? INT2FIX(-1) : INT2FIX(+1);
913 return rb_assoc_new(DBL2NUM(HUGE_VAL), vsign);
915 v = DBL2NUM(lgamma_r(d, &sign));
916 return rb_assoc_new(v, INT2FIX(sign));
920 #define exp1(n) \
921 VALUE \
922 rb_math_##n(VALUE x)\
924 return math_##n(0, x);\
927 #define exp2(n) \
928 VALUE \
929 rb_math_##n(VALUE x, VALUE y)\
931 return math_##n(0, x, y);\
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
947 * Document-class: Math::DomainError
949 * Raised when a mathematical function is evaluated outside of its
950 * domain of definition.
952 * For example, since +cos+ returns values in the range -1..1,
953 * its inverse function +acos+ is only defined on that interval:
955 * Math.acos(42)
957 * <em>produces:</em>
959 * Math::DomainError: Numerical argument is out of domain - "acos"
963 * Document-class: Math
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.
970 * Domains and codomains are given only for real (not complex) numbers.
974 void
975 InitVM_Math(void)
977 rb_mMath = rb_define_module("Math");
978 rb_eMathDomainError = rb_define_class_under(rb_mMath, "DomainError", rb_eStandardError);
980 /* Definition of the mathematical constant PI as a Float number. */
981 rb_define_const(rb_mMath, "PI", DBL2NUM(M_PI));
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
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);
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);
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);
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);
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);
1014 rb_define_module_function(rb_mMath, "frexp", math_frexp, 1);
1015 rb_define_module_function(rb_mMath, "ldexp", math_ldexp, 2);
1017 rb_define_module_function(rb_mMath, "hypot", math_hypot, 2);
1019 rb_define_module_function(rb_mMath, "erf", math_erf, 1);
1020 rb_define_module_function(rb_mMath, "erfc", math_erfc, 1);
1022 rb_define_module_function(rb_mMath, "gamma", math_gamma, 1);
1023 rb_define_module_function(rb_mMath, "lgamma", math_lgamma, 1);
1026 void
1027 Init_Math(void)
1029 InitVM(Math);