1 /**********************************************************************
6 created at: Tue Jan 25 14:12:56 JST 1994
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
21 if (!rb_obj_is_kind_of(x
, rb_cNumeric
)) {
22 rb_raise(rb_eTypeError
, "can't convert %s into Float",
25 x
== Qfalse
? "false" :
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 {\
38 domain_check(double x
, const char *msg
)
57 infinity_check(VALUE arg
, double res
, const char *msg
)
63 if (isinf(res
) && !isinf(RFLOAT_VALUE(arg
))) {
77 * Math.atan2(y, x) => float
79 * Computes the arc tangent given <i>y</i> and <i>x</i>. Returns
85 math_atan2(VALUE obj
, VALUE y
, VALUE x
)
88 return DOUBLE2NUM(atan2(RFLOAT_VALUE(y
), RFLOAT_VALUE(x
)));
94 * Math.cos(x) => float
96 * Computes the cosine of <i>x</i> (expressed in radians). Returns
101 math_cos(VALUE obj
, VALUE x
)
104 return DOUBLE2NUM(cos(RFLOAT_VALUE(x
)));
109 * Math.sin(x) => float
111 * Computes the sine of <i>x</i> (expressed in radians). Returns
116 math_sin(VALUE obj
, VALUE x
)
120 return DOUBLE2NUM(sin(RFLOAT_VALUE(x
)));
126 * Math.tan(x) => float
128 * Returns the tangent of <i>x</i> (expressed in radians).
132 math_tan(VALUE obj
, VALUE x
)
136 return DOUBLE2NUM(tan(RFLOAT_VALUE(x
)));
141 * Math.acos(x) => float
143 * Computes the arc cosine of <i>x</i>. Returns 0..PI.
147 math_acos(VALUE obj
, VALUE x
)
153 d
= acos(RFLOAT_VALUE(x
));
154 domain_check(d
, "acos");
155 return DOUBLE2NUM(d
);
160 * Math.asin(x) => float
162 * Computes the arc sine of <i>x</i>. Returns -{PI/2} .. {PI/2}.
166 math_asin(VALUE obj
, VALUE x
)
172 d
= asin(RFLOAT_VALUE(x
));
173 domain_check(d
, "asin");
174 return DOUBLE2NUM(d
);
179 * Math.atan(x) => float
181 * Computes the arc tangent of <i>x</i>. Returns -{PI/2} .. {PI/2}.
185 math_atan(VALUE obj
, VALUE x
)
188 return DOUBLE2NUM(atan(RFLOAT_VALUE(x
)));
195 return (exp(x
) + exp(-x
)) / 2;
201 * Math.cosh(x) => float
203 * Computes the hyperbolic cosine of <i>x</i> (expressed in radians).
207 math_cosh(VALUE obj
, VALUE x
)
211 return DOUBLE2NUM(cosh(RFLOAT_VALUE(x
)));
218 return (exp(x
) - exp(-x
)) / 2;
224 * Math.sinh(x) => float
226 * Computes the hyperbolic sine of <i>x</i> (expressed in
231 math_sinh(VALUE obj
, VALUE x
)
234 return DOUBLE2NUM(sinh(RFLOAT_VALUE(x
)));
241 return sinh(x
) / cosh(x
);
247 * Math.tanh() => float
249 * Computes the hyperbolic tangent of <i>x</i> (expressed in
254 math_tanh(VALUE obj
, VALUE x
)
257 return DOUBLE2NUM(tanh(RFLOAT_VALUE(x
)));
262 * Math.acosh(x) => float
264 * Computes the inverse hyperbolic cosine of <i>x</i>.
268 math_acosh(VALUE obj
, VALUE x
)
274 d
= acosh(RFLOAT_VALUE(x
));
275 domain_check(d
, "acosh");
276 return DOUBLE2NUM(d
);
281 * Math.asinh(x) => float
283 * Computes the inverse hyperbolic sine of <i>x</i>.
287 math_asinh(VALUE obj
, VALUE x
)
290 return DOUBLE2NUM(asinh(RFLOAT_VALUE(x
)));
295 * Math.atanh(x) => float
297 * Computes the inverse hyperbolic tangent of <i>x</i>.
301 math_atanh(VALUE obj
, VALUE x
)
307 d
= atanh(RFLOAT_VALUE(x
));
308 domain_check(d
, "atanh");
309 infinity_check(x
, d
, "atanh");
310 return DOUBLE2NUM(d
);
315 * Math.exp(x) => float
321 math_exp(VALUE obj
, VALUE 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()
332 # define log(x) ((x) < 0.0 ? nan("") : log(x))
333 # define log10(x) ((x) < 0.0 ? nan("") : log10(x))
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
347 math_log(int argc
, VALUE
*argv
)
352 rb_scan_args(argc
, argv
, "11", &x
, &base
);
355 d
= log(RFLOAT_VALUE(x
));
358 d
/= log(RFLOAT_VALUE(base
));
360 domain_check(d
, "log");
361 infinity_check(x
, d
, "log");
362 return DOUBLE2NUM(d
);
370 return log10(x
)/log10(2.0);
373 extern double log2(double);
379 * Math.log2(numeric) => float
381 * Returns the base 2 logarithm of <i>numeric</i>.
385 math_log2(VALUE obj
, VALUE x
)
391 d
= log2(RFLOAT_VALUE(x
));
392 domain_check(d
, "log2");
393 infinity_check(x
, d
, "log2");
394 return DOUBLE2NUM(d
);
399 * Math.log10(numeric) => float
401 * Returns the base 10 logarithm of <i>numeric</i>.
405 math_log10(VALUE obj
, VALUE x
)
411 d
= log10(RFLOAT_VALUE(x
));
412 domain_check(d
, "log10");
413 infinity_check(x
, d
, "log10");
414 return DOUBLE2NUM(d
);
419 * Math.sqrt(numeric) => float
421 * Returns the non-negative square root of <i>numeric</i>.
424 * p [x, Math.sqrt(x), Math.sqrt(x)**2]
429 * [2, 1.4142135623731, 2.0]
430 * [3, 1.73205080756888, 3.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]
437 * [10, 3.16227766016838, 10.0]
442 math_sqrt(VALUE obj
, VALUE x
)
448 d
= sqrt(RFLOAT_VALUE(x
));
449 domain_check(d
, "sqrt");
450 return DOUBLE2NUM(d
);
455 * Math.cbrt(numeric) => float
457 * Returns the cube root of <i>numeric</i>.
460 * p [x, Math.cbrt(x), Math.cbrt(x)**3]
463 * [-9, -2.0800838230519, -9.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]
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]
481 * [9, 2.0800838230519, 9.0]
486 math_cbrt(VALUE obj
, VALUE x
)
489 return DOUBLE2NUM(cbrt(RFLOAT_VALUE(x
)));
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
500 * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11]
501 * fraction * 2**exponent #=> 1234.0
505 math_frexp(VALUE obj
, VALUE x
)
512 d
= frexp(RFLOAT_VALUE(x
), &exp
);
513 return rb_assoc_new(DOUBLE2NUM(d
), INT2NUM(exp
));
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
527 math_ldexp(VALUE obj
, VALUE x
, VALUE n
)
530 return DOUBLE2NUM(ldexp(RFLOAT_VALUE(x
), NUM2INT(n
)));
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
544 math_hypot(VALUE obj
, VALUE x
, VALUE y
)
547 return DOUBLE2NUM(hypot(RFLOAT_VALUE(x
), RFLOAT_VALUE(y
)));
552 * Math.erf(x) => float
554 * Calculates the error function of x.
558 math_erf(VALUE obj
, VALUE x
)
561 return DOUBLE2NUM(erf(RFLOAT_VALUE(x
)));
566 * Math.erfc(x) => float
568 * Calculates the complementary error function of x.
572 math_erfc(VALUE obj
, VALUE x
)
575 return DOUBLE2NUM(erfc(RFLOAT_VALUE(x
)));
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)] }
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]
620 math_gamma(VALUE obj
, VALUE x
)
625 d
= tgamma(RFLOAT_VALUE(x
));
626 domain_check(d
, "gamma");
627 return DOUBLE2NUM(d
);
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.
643 math_lgamma(VALUE obj
, VALUE x
)
650 d
= lgamma_r(RFLOAT_VALUE(x
), &sign
);
651 domain_check(d
, "lgamma");
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.
667 rb_mMath
= rb_define_module("Math");
670 rb_define_const(rb_mMath
, "PI", DOUBLE2NUM(M_PI
));
672 rb_define_const(rb_mMath
, "PI", DOUBLE2NUM(atan(1.0)*4.0));
676 rb_define_const(rb_mMath
, "E", DOUBLE2NUM(M_E
));
678 rb_define_const(rb_mMath
, "E", DOUBLE2NUM(exp(1.0)));
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);