* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / math.c
blobe23734a812feb55e5b95dd12585ae8c96015b1a0
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/ruby.h"
13 #include <math.h>
14 #include <errno.h>
16 VALUE rb_mMath;
18 static VALUE
19 to_flo(VALUE x)
21 if (!rb_obj_is_kind_of(x, rb_cNumeric)) {
22 rb_raise(rb_eTypeError, "can't convert %s into Float",
23 NIL_P(x) ? "nil" :
24 x == Qtrue ? "true" :
25 x == Qfalse ? "false" :
26 rb_obj_classname(x));
28 return rb_convert_type(x, T_FLOAT, "Float", "to_f");
31 #define Need_Float(x) (x) = to_flo(x)
32 #define Need_Float2(x,y) do {\
33 Need_Float(x);\
34 Need_Float(y);\
35 } while (0)
37 static void
38 domain_check(double x, const char *msg)
40 while(1) {
41 if (errno) {
42 rb_sys_fail(msg);
44 if (isnan(x)) {
45 #if defined(EDOM)
46 errno = EDOM;
47 #elif defined(ERANGE)
48 errno = ERANGE;
49 #endif
50 continue;
52 break;
56 static void
57 infinity_check(VALUE arg, double res, const char *msg)
59 while(1) {
60 if (errno) {
61 rb_sys_fail(msg);
63 if (isinf(res) && !isinf(RFLOAT_VALUE(arg))) {
64 #if defined(EDOM)
65 errno = EDOM;
66 #elif defined(ERANGE)
67 errno = ERANGE;
68 #endif
69 continue;
71 break;
76 * call-seq:
77 * Math.atan2(y, x) => float
79 * Computes the arc tangent given <i>y</i> and <i>x</i>. Returns
80 * -PI..PI.
84 VALUE
85 math_atan2(VALUE obj, VALUE y, VALUE x)
87 Need_Float2(y, x);
88 return DOUBLE2NUM(atan2(RFLOAT_VALUE(y), RFLOAT_VALUE(x)));
93 * call-seq:
94 * Math.cos(x) => float
96 * Computes the cosine of <i>x</i> (expressed in radians). Returns
97 * -1..1.
100 VALUE
101 math_cos(VALUE obj, VALUE x)
103 Need_Float(x);
104 return DOUBLE2NUM(cos(RFLOAT_VALUE(x)));
108 * call-seq:
109 * Math.sin(x) => float
111 * Computes the sine of <i>x</i> (expressed in radians). Returns
112 * -1..1.
115 VALUE
116 math_sin(VALUE obj, VALUE x)
118 Need_Float(x);
120 return DOUBLE2NUM(sin(RFLOAT_VALUE(x)));
125 * call-seq:
126 * Math.tan(x) => float
128 * Returns the tangent of <i>x</i> (expressed in radians).
131 static VALUE
132 math_tan(VALUE obj, VALUE x)
134 Need_Float(x);
136 return DOUBLE2NUM(tan(RFLOAT_VALUE(x)));
140 * call-seq:
141 * Math.acos(x) => float
143 * Computes the arc cosine of <i>x</i>. Returns 0..PI.
146 static VALUE
147 math_acos(VALUE obj, VALUE x)
149 double d;
151 Need_Float(x);
152 errno = 0;
153 d = acos(RFLOAT_VALUE(x));
154 domain_check(d, "acos");
155 return DOUBLE2NUM(d);
159 * call-seq:
160 * Math.asin(x) => float
162 * Computes the arc sine of <i>x</i>. Returns -{PI/2} .. {PI/2}.
165 static VALUE
166 math_asin(VALUE obj, VALUE x)
168 double d;
170 Need_Float(x);
171 errno = 0;
172 d = asin(RFLOAT_VALUE(x));
173 domain_check(d, "asin");
174 return DOUBLE2NUM(d);
178 * call-seq:
179 * Math.atan(x) => float
181 * Computes the arc tangent of <i>x</i>. Returns -{PI/2} .. {PI/2}.
184 static VALUE
185 math_atan(VALUE obj, VALUE x)
187 Need_Float(x);
188 return DOUBLE2NUM(atan(RFLOAT_VALUE(x)));
191 #ifndef HAVE_COSH
192 double
193 cosh(double x)
195 return (exp(x) + exp(-x)) / 2;
197 #endif
200 * call-seq:
201 * Math.cosh(x) => float
203 * Computes the hyperbolic cosine of <i>x</i> (expressed in radians).
206 VALUE
207 math_cosh(VALUE obj, VALUE x)
209 Need_Float(x);
211 return DOUBLE2NUM(cosh(RFLOAT_VALUE(x)));
214 #ifndef HAVE_SINH
215 double
216 sinh(double x)
218 return (exp(x) - exp(-x)) / 2;
220 #endif
223 * call-seq:
224 * Math.sinh(x) => float
226 * Computes the hyperbolic sine of <i>x</i> (expressed in
227 * radians).
230 VALUE
231 math_sinh(VALUE obj, VALUE x)
233 Need_Float(x);
234 return DOUBLE2NUM(sinh(RFLOAT_VALUE(x)));
237 #ifndef HAVE_TANH
238 double
239 tanh(double x)
241 return sinh(x) / cosh(x);
243 #endif
246 * call-seq:
247 * Math.tanh() => float
249 * Computes the hyperbolic tangent of <i>x</i> (expressed in
250 * radians).
253 static VALUE
254 math_tanh(VALUE obj, VALUE x)
256 Need_Float(x);
257 return DOUBLE2NUM(tanh(RFLOAT_VALUE(x)));
261 * call-seq:
262 * Math.acosh(x) => float
264 * Computes the inverse hyperbolic cosine of <i>x</i>.
267 static VALUE
268 math_acosh(VALUE obj, VALUE x)
270 double d;
272 Need_Float(x);
273 errno = 0;
274 d = acosh(RFLOAT_VALUE(x));
275 domain_check(d, "acosh");
276 return DOUBLE2NUM(d);
280 * call-seq:
281 * Math.asinh(x) => float
283 * Computes the inverse hyperbolic sine of <i>x</i>.
286 static VALUE
287 math_asinh(VALUE obj, VALUE x)
289 Need_Float(x);
290 return DOUBLE2NUM(asinh(RFLOAT_VALUE(x)));
294 * call-seq:
295 * Math.atanh(x) => float
297 * Computes the inverse hyperbolic tangent of <i>x</i>.
300 static VALUE
301 math_atanh(VALUE obj, VALUE x)
303 double d;
305 Need_Float(x);
306 errno = 0;
307 d = atanh(RFLOAT_VALUE(x));
308 domain_check(d, "atanh");
309 infinity_check(x, d, "atanh");
310 return DOUBLE2NUM(d);
314 * call-seq:
315 * Math.exp(x) => float
317 * Returns e**x.
320 VALUE
321 math_exp(VALUE obj, VALUE x)
323 Need_Float(x);
324 return DOUBLE2NUM(exp(RFLOAT_VALUE(x)));
327 #if defined __CYGWIN__
328 # include <cygwin/version.h>
329 # if CYGWIN_VERSION_DLL_MAJOR < 1005
330 # define nan(x) nan()
331 # endif
332 # define log(x) ((x) < 0.0 ? nan("") : log(x))
333 # define log10(x) ((x) < 0.0 ? nan("") : log10(x))
334 #endif
337 * call-seq:
338 * Math.log(numeric) => float
339 * Math.log(num,base) => float
341 * Returns the natural logarithm of <i>numeric</i>.
342 * If additional second argument is given, it will be the base
343 * of logarithm.
346 VALUE
347 math_log(int argc, VALUE *argv)
349 VALUE x, base;
350 double d;
352 rb_scan_args(argc, argv, "11", &x, &base);
353 Need_Float(x);
354 errno = 0;
355 d = log(RFLOAT_VALUE(x));
356 if (!NIL_P(base)) {
357 Need_Float(base);
358 d /= log(RFLOAT_VALUE(base));
360 domain_check(d, "log");
361 infinity_check(x, d, "log");
362 return DOUBLE2NUM(d);
365 #ifndef log2
366 #ifndef HAVE_LOG2
367 double
368 log2(double x)
370 return log10(x)/log10(2.0);
372 #else
373 extern double log2(double);
374 #endif
375 #endif
378 * call-seq:
379 * Math.log2(numeric) => float
381 * Returns the base 2 logarithm of <i>numeric</i>.
384 static VALUE
385 math_log2(VALUE obj, VALUE x)
387 double d;
389 Need_Float(x);
390 errno = 0;
391 d = log2(RFLOAT_VALUE(x));
392 domain_check(d, "log2");
393 infinity_check(x, d, "log2");
394 return DOUBLE2NUM(d);
398 * call-seq:
399 * Math.log10(numeric) => float
401 * Returns the base 10 logarithm of <i>numeric</i>.
404 static VALUE
405 math_log10(VALUE obj, VALUE x)
407 double d;
409 Need_Float(x);
410 errno = 0;
411 d = log10(RFLOAT_VALUE(x));
412 domain_check(d, "log10");
413 infinity_check(x, d, "log10");
414 return DOUBLE2NUM(d);
418 * call-seq:
419 * Math.sqrt(numeric) => float
421 * Returns the non-negative square root of <i>numeric</i>.
423 * 0.upto(10) {|x|
424 * p [x, Math.sqrt(x), Math.sqrt(x)**2]
426 * #=>
427 * [0, 0.0, 0.0]
428 * [1, 1.0, 1.0]
429 * [2, 1.4142135623731, 2.0]
430 * [3, 1.73205080756888, 3.0]
431 * [4, 2.0, 4.0]
432 * [5, 2.23606797749979, 5.0]
433 * [6, 2.44948974278318, 6.0]
434 * [7, 2.64575131106459, 7.0]
435 * [8, 2.82842712474619, 8.0]
436 * [9, 3.0, 9.0]
437 * [10, 3.16227766016838, 10.0]
441 VALUE
442 math_sqrt(VALUE obj, VALUE x)
444 double d;
446 Need_Float(x);
447 errno = 0;
448 d = sqrt(RFLOAT_VALUE(x));
449 domain_check(d, "sqrt");
450 return DOUBLE2NUM(d);
454 * call-seq:
455 * Math.cbrt(numeric) => float
457 * Returns the cube root of <i>numeric</i>.
459 * -9.upto(9) {|x|
460 * p [x, Math.cbrt(x), Math.cbrt(x)**3]
462 * #=>
463 * [-9, -2.0800838230519, -9.0]
464 * [-8, -2.0, -8.0]
465 * [-7, -1.91293118277239, -7.0]
466 * [-6, -1.81712059283214, -6.0]
467 * [-5, -1.7099759466767, -5.0]
468 * [-4, -1.5874010519682, -4.0]
469 * [-3, -1.44224957030741, -3.0]
470 * [-2, -1.25992104989487, -2.0]
471 * [-1, -1.0, -1.0]
472 * [0, 0.0, 0.0]
473 * [1, 1.0, 1.0]
474 * [2, 1.25992104989487, 2.0]
475 * [3, 1.44224957030741, 3.0]
476 * [4, 1.5874010519682, 4.0]
477 * [5, 1.7099759466767, 5.0]
478 * [6, 1.81712059283214, 6.0]
479 * [7, 1.91293118277239, 7.0]
480 * [8, 2.0, 8.0]
481 * [9, 2.0800838230519, 9.0]
485 static VALUE
486 math_cbrt(VALUE obj, VALUE x)
488 Need_Float(x);
489 return DOUBLE2NUM(cbrt(RFLOAT_VALUE(x)));
493 * call-seq:
494 * Math.frexp(numeric) => [ fraction, exponent ]
496 * Returns a two-element array containing the normalized fraction (a
497 * <code>Float</code>) and exponent (a <code>Fixnum</code>) of
498 * <i>numeric</i>.
500 * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11]
501 * fraction * 2**exponent #=> 1234.0
504 static VALUE
505 math_frexp(VALUE obj, VALUE x)
507 double d;
508 int exp;
510 Need_Float(x);
512 d = frexp(RFLOAT_VALUE(x), &exp);
513 return rb_assoc_new(DOUBLE2NUM(d), INT2NUM(exp));
517 * call-seq:
518 * Math.ldexp(flt, int) -> float
520 * Returns the value of <i>flt</i>*(2**<i>int</i>).
522 * fraction, exponent = Math.frexp(1234)
523 * Math.ldexp(fraction, exponent) #=> 1234.0
526 static VALUE
527 math_ldexp(VALUE obj, VALUE x, VALUE n)
529 Need_Float(x);
530 return DOUBLE2NUM(ldexp(RFLOAT_VALUE(x), NUM2INT(n)));
534 * call-seq:
535 * Math.hypot(x, y) => float
537 * Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle
538 * with sides <i>x</i> and <i>y</i>.
540 * Math.hypot(3, 4) #=> 5.0
543 VALUE
544 math_hypot(VALUE obj, VALUE x, VALUE y)
546 Need_Float2(x, y);
547 return DOUBLE2NUM(hypot(RFLOAT_VALUE(x), RFLOAT_VALUE(y)));
551 * call-seq:
552 * Math.erf(x) => float
554 * Calculates the error function of x.
557 static VALUE
558 math_erf(VALUE obj, VALUE x)
560 Need_Float(x);
561 return DOUBLE2NUM(erf(RFLOAT_VALUE(x)));
565 * call-seq:
566 * Math.erfc(x) => float
568 * Calculates the complementary error function of x.
571 static VALUE
572 math_erfc(VALUE obj, VALUE x)
574 Need_Float(x);
575 return DOUBLE2NUM(erfc(RFLOAT_VALUE(x)));
579 * call-seq:
580 * Math.gamma(x) => float
582 * Calculates the gamma function of x.
584 * Note that gamma(n) is same as fact(n-1) for integer n >= 0.
585 * However gamma(n) returns float and possibly has error in calculation.
587 * def fact(n) (1..n).inject(1) {|r,i| r*i } end
588 * 0.upto(25) {|i| p [i, Math.gamma(i+1), fact(i)] }
589 * #=>
590 * [0, 1.0, 1]
591 * [1, 1.0, 1]
592 * [2, 2.0, 2]
593 * [3, 6.0, 6]
594 * [4, 24.0, 24]
595 * [5, 120.0, 120]
596 * [6, 720.0, 720]
597 * [7, 5040.0, 5040]
598 * [8, 40320.0, 40320]
599 * [9, 362880.0, 362880]
600 * [10, 3628800.0, 3628800]
601 * [11, 39916800.0, 39916800]
602 * [12, 479001599.999999, 479001600]
603 * [13, 6227020800.00001, 6227020800]
604 * [14, 87178291199.9998, 87178291200]
605 * [15, 1307674368000.0, 1307674368000]
606 * [16, 20922789888000.0, 20922789888000]
607 * [17, 3.55687428096001e+14, 355687428096000]
608 * [18, 6.40237370572799e+15, 6402373705728000]
609 * [19, 1.21645100408832e+17, 121645100408832000]
610 * [20, 2.43290200817664e+18, 2432902008176640000]
611 * [21, 5.10909421717094e+19, 51090942171709440000]
612 * [22, 1.12400072777761e+21, 1124000727777607680000]
613 * [23, 2.58520167388851e+22, 25852016738884976640000]
614 * [24, 6.20448401733239e+23, 620448401733239439360000]
615 * [25, 1.5511210043331e+25, 15511210043330985984000000]
619 static VALUE
620 math_gamma(VALUE obj, VALUE x)
622 double d;
623 Need_Float(x);
624 errno = 0;
625 d = tgamma(RFLOAT_VALUE(x));
626 domain_check(d, "gamma");
627 return DOUBLE2NUM(d);
631 * call-seq:
632 * Math.lgamma(x) => [float, -1 or 1]
634 * Calculates the logarithmic gamma of x and
635 * the sign of gamma of x.
637 * Math.lgamma(x) is same as
638 * [Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]
639 * but avoid overflow by Math.gamma(x) for large x.
642 static VALUE
643 math_lgamma(VALUE obj, VALUE x)
645 double d;
646 int sign;
647 VALUE v;
648 Need_Float(x);
649 errno = 0;
650 d = lgamma_r(RFLOAT_VALUE(x), &sign);
651 domain_check(d, "lgamma");
652 v = DOUBLE2NUM(d);
653 return rb_assoc_new(v, INT2FIX(sign));
657 * The <code>Math</code> module contains module functions for basic
658 * trigonometric and transcendental functions. See class
659 * <code>Float</code> for a list of constants that
660 * define Ruby's floating point accuracy.
664 void
665 Init_Math(void)
667 rb_mMath = rb_define_module("Math");
669 #ifdef M_PI
670 rb_define_const(rb_mMath, "PI", DOUBLE2NUM(M_PI));
671 #else
672 rb_define_const(rb_mMath, "PI", DOUBLE2NUM(atan(1.0)*4.0));
673 #endif
675 #ifdef M_E
676 rb_define_const(rb_mMath, "E", DOUBLE2NUM(M_E));
677 #else
678 rb_define_const(rb_mMath, "E", DOUBLE2NUM(exp(1.0)));
679 #endif
681 rb_define_module_function(rb_mMath, "atan2", math_atan2, 2);
682 rb_define_module_function(rb_mMath, "cos", math_cos, 1);
683 rb_define_module_function(rb_mMath, "sin", math_sin, 1);
684 rb_define_module_function(rb_mMath, "tan", math_tan, 1);
686 rb_define_module_function(rb_mMath, "acos", math_acos, 1);
687 rb_define_module_function(rb_mMath, "asin", math_asin, 1);
688 rb_define_module_function(rb_mMath, "atan", math_atan, 1);
690 rb_define_module_function(rb_mMath, "cosh", math_cosh, 1);
691 rb_define_module_function(rb_mMath, "sinh", math_sinh, 1);
692 rb_define_module_function(rb_mMath, "tanh", math_tanh, 1);
694 rb_define_module_function(rb_mMath, "acosh", math_acosh, 1);
695 rb_define_module_function(rb_mMath, "asinh", math_asinh, 1);
696 rb_define_module_function(rb_mMath, "atanh", math_atanh, 1);
698 rb_define_module_function(rb_mMath, "exp", math_exp, 1);
699 rb_define_module_function(rb_mMath, "log", math_log, -1);
700 rb_define_module_function(rb_mMath, "log2", math_log2, 1);
701 rb_define_module_function(rb_mMath, "log10", math_log10, 1);
702 rb_define_module_function(rb_mMath, "sqrt", math_sqrt, 1);
703 rb_define_module_function(rb_mMath, "cbrt", math_cbrt, 1);
705 rb_define_module_function(rb_mMath, "frexp", math_frexp, 1);
706 rb_define_module_function(rb_mMath, "ldexp", math_ldexp, 2);
708 rb_define_module_function(rb_mMath, "hypot", math_hypot, 2);
710 rb_define_module_function(rb_mMath, "erf", math_erf, 1);
711 rb_define_module_function(rb_mMath, "erfc", math_erfc, 1);
713 rb_define_module_function(rb_mMath, "gamma", math_gamma, 1);
714 rb_define_module_function(rb_mMath, "lgamma", math_lgamma, 1);