Ruby 3.2.4p170 (2024-04-23 revision af471c0e0127eea0cafa6f308c0425bbfab0acf5)
math.c
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#include "ruby/internal/config.h"
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
31
32#define Get_Double(x) rb_num_to_dbl(x)
33
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)
40
41/*
42 * call-seq:
43 * Math.atan2(y, x) -> float
44 *
45 * Returns the {arc tangent}[https://en.wikipedia.org/wiki/Atan2] of +y+ and +x+
46 * in {radians}[https://en.wikipedia.org/wiki/Trigonometric_functions#Radians_versus_degrees].
47 *
48 * - Domain of +y+: <tt>[-INFINITY, INFINITY]</tt>.
49 * - Domain of +x+: <tt>[-INFINITY, INFINITY]</tt>.
50 * - Range: <tt>[-PI, PI]</tt>.
51 *
52 * Examples:
53 *
54 * atan2(-1.0, -1.0) # => -2.356194490192345 # -3*PI/4
55 * atan2(-1.0, 0.0) # => -1.5707963267948966 # -PI/2
56 * atan2(-1.0, 1.0) # => -0.7853981633974483 # -PI/4
57 * atan2(0.0, -1.0) # => 3.141592653589793 # PI
58 *
59 */
60
61static VALUE
62math_atan2(VALUE unused_obj, VALUE y, VALUE x)
63{
64 double dx, dy;
65 dx = Get_Double(x);
66 dy = Get_Double(y);
67 if (dx == 0.0 && dy == 0.0) {
68 if (!signbit(dx))
69 return DBL2NUM(dy);
70 if (!signbit(dy))
71 return DBL2NUM(M_PI);
72 return DBL2NUM(-M_PI);
73 }
74#ifndef ATAN2_INF_C99
75 if (isinf(dx) && isinf(dy)) {
76 /* optimization for FLONUM */
77 if (dx < 0.0) {
78 const double dz = (3.0 * M_PI / 4.0);
79 return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
80 }
81 else {
82 const double dz = (M_PI / 4.0);
83 return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
84 }
85 }
86#endif
87 return DBL2NUM(atan2(dy, dx));
88}
89
90
91/*
92 * call-seq:
93 * Math.cos(x) -> float
94 *
95 * Returns the
96 * {cosine}[https://en.wikipedia.org/wiki/Sine_and_cosine] of +x+
97 * in {radians}[https://en.wikipedia.org/wiki/Trigonometric_functions#Radians_versus_degrees].
98 *
99 * - Domain: <tt>(-INFINITY, INFINITY)</tt>.
100 * - Range: <tt>[-1.0, 1.0]</tt>.
101 *
102 * Examples:
103 *
104 * cos(-PI) # => -1.0
105 * cos(-PI/2) # => 6.123031769111886e-17 # 0.0000000000000001
106 * cos(0.0) # => 1.0
107 * cos(PI/2) # => 6.123031769111886e-17 # 0.0000000000000001
108 * cos(PI) # => -1.0
109 *
110 */
111
112static VALUE
113math_cos(VALUE unused_obj, VALUE x)
114{
115 return DBL2NUM(cos(Get_Double(x)));
116}
117
118/*
119 * call-seq:
120 * Math.sin(x) -> float
121 *
122 * Returns the
123 * {sine}[https://en.wikipedia.org/wiki/Sine_and_cosine] of +x+
124 * in {radians}[https://en.wikipedia.org/wiki/Trigonometric_functions#Radians_versus_degrees].
125 *
126 * - Domain: <tt>(-INFINITY, INFINITY)</tt>.
127 * - Range: <tt>[-1.0, 1.0]</tt>.
128 *
129 * Examples:
130 *
131 * sin(-PI) # => -1.2246063538223773e-16 # -0.0000000000000001
132 * sin(-PI/2) # => -1.0
133 * sin(0.0) # => 0.0
134 * sin(PI/2) # => 1.0
135 * sin(PI) # => 1.2246063538223773e-16 # 0.0000000000000001
136 *
137 */
138
139static VALUE
140math_sin(VALUE unused_obj, VALUE x)
141{
142 return DBL2NUM(sin(Get_Double(x)));
143}
144
145
146/*
147 * call-seq:
148 * Math.tan(x) -> float
149 *
150 * Returns the
151 * {tangent}[https://en.wikipedia.org/wiki/Trigonometric_functions] of +x+
152 * in {radians}[https://en.wikipedia.org/wiki/Trigonometric_functions#Radians_versus_degrees].
153 *
154 * - Domain: <tt>(-INFINITY, INFINITY)</tt>.
155 * - Range: <tt>(-INFINITY, INFINITY)</tt>.
156 *
157 * Examples:
158 *
159 * tan(-PI) # => 1.2246467991473532e-16 # -0.0000000000000001
160 * tan(-PI/2) # => -1.633123935319537e+16 # -16331239353195370.0
161 * tan(0.0) # => 0.0
162 * tan(PI/2) # => 1.633123935319537e+16 # 16331239353195370.0
163 * tan(PI) # => -1.2246467991473532e-16 # -0.0000000000000001
164 *
165 */
166
167static VALUE
168math_tan(VALUE unused_obj, VALUE x)
169{
170 return DBL2NUM(tan(Get_Double(x)));
171}
172
173/*
174 * call-seq:
175 * Math.acos(x) -> float
176 *
177 * Returns the {arc cosine}[https://en.wikipedia.org/wiki/Inverse_trigonometric_functions] of +x+.
178 *
179 * - Domain: <tt>[-1, 1]</tt>.
180 * - Range: <tt>[0, PI]</tt>.
181 *
182 * Examples:
183 *
184 * acos(-1.0) # => 3.141592653589793 # PI
185 * acos(0.0) # => 1.5707963267948966 # PI/2
186 * acos(1.0) # => 0.0
187 *
188 */
189
190static VALUE
191math_acos(VALUE unused_obj, VALUE x)
192{
193 double d;
194
195 d = Get_Double(x);
196 domain_check_range(d, -1.0, 1.0, "acos");
197 return DBL2NUM(acos(d));
198}
199
200/*
201 * call-seq:
202 * Math.asin(x) -> float
203 *
204 * Returns the {arc sine}[https://en.wikipedia.org/wiki/Inverse_trigonometric_functions] of +x+.
205 *
206 * - Domain: <tt>[-1, -1]</tt>.
207 * - Range: <tt>[-PI/2, PI/2]</tt>.
208 *
209 * Examples:
210 *
211 * asin(-1.0) # => -1.5707963267948966 # -PI/2
212 * asin(0.0) # => 0.0
213 * asin(1.0) # => 1.5707963267948966 # PI/2
214 *
215 */
216
217static VALUE
218math_asin(VALUE unused_obj, VALUE x)
219{
220 double d;
221
222 d = Get_Double(x);
223 domain_check_range(d, -1.0, 1.0, "asin");
224 return DBL2NUM(asin(d));
225}
226
227/*
228 * call-seq:
229 * Math.atan(x) -> Float
230 *
231 * Returns the {arc tangent}[https://en.wikipedia.org/wiki/Inverse_trigonometric_functions] of +x+.
232 *
233 * - Domain: <tt>[-INFINITY, INFINITY]</tt>.
234 * - Range: <tt>[-PI/2, PI/2] </tt>.
235 *
236 * Examples:
237 *
238 * atan(-INFINITY) # => -1.5707963267948966 # -PI2
239 * atan(-PI) # => -1.2626272556789115
240 * atan(-PI/2) # => -1.0038848218538872
241 * atan(0.0) # => 0.0
242 * atan(PI/2) # => 1.0038848218538872
243 * atan(PI) # => 1.2626272556789115
244 * atan(INFINITY) # => 1.5707963267948966 # PI/2
245 *
246 */
247
248static VALUE
249math_atan(VALUE unused_obj, VALUE x)
250{
251 return DBL2NUM(atan(Get_Double(x)));
252}
253
254#ifndef HAVE_COSH
255double
256cosh(double x)
257{
258 return (exp(x) + exp(-x)) / 2;
259}
260#endif
261
262/*
263 * call-seq:
264 * Math.cosh(x) -> float
265 *
266 * Returns the {hyperbolic cosine}[https://en.wikipedia.org/wiki/Hyperbolic_functions] of +x+
267 * in {radians}[https://en.wikipedia.org/wiki/Trigonometric_functions#Radians_versus_degrees].
268 *
269 * - Domain: <tt>[-INFINITY, INFINITY]</tt>.
270 * - Range: <tt>[1, INFINITY]</tt>.
271 *
272 * Examples:
273 *
274 * cosh(-INFINITY) # => Infinity
275 * cosh(0.0) # => 1.0
276 * cosh(INFINITY) # => Infinity
277 *
278 */
279
280static VALUE
281math_cosh(VALUE unused_obj, VALUE x)
282{
283 return DBL2NUM(cosh(Get_Double(x)));
284}
285
286#ifndef HAVE_SINH
287double
288sinh(double x)
289{
290 return (exp(x) - exp(-x)) / 2;
291}
292#endif
293
294/*
295 * call-seq:
296 * Math.sinh(x) -> float
297 *
298 * Returns the {hyperbolic sine}[https://en.wikipedia.org/wiki/Hyperbolic_functions] of +x+
299 * in {radians}[https://en.wikipedia.org/wiki/Trigonometric_functions#Radians_versus_degrees].
300 *
301 * - Domain: <tt>[-INFINITY, INFINITY]</tt>.
302 * - Range: <tt>[-INFINITY, INFINITY]</tt>.
303 *
304 * Examples:
305 *
306 * sinh(-INFINITY) # => -Infinity
307 * sinh(0.0) # => 0.0
308 * sinh(INFINITY) # => Infinity
309 *
310 */
311
312static VALUE
313math_sinh(VALUE unused_obj, VALUE x)
314{
315 return DBL2NUM(sinh(Get_Double(x)));
316}
317
318#ifndef HAVE_TANH
319double
320tanh(double x)
321{
322# if defined(HAVE_SINH) && defined(HAVE_COSH)
323 const double c = cosh(x);
324 if (!isinf(c)) return sinh(x) / c;
325# else
326 const double e = exp(x+x);
327 if (!isinf(e)) return (e - 1) / (e + 1);
328# endif
329 return x > 0 ? 1.0 : -1.0;
330}
331#endif
332
333/*
334 * call-seq:
335 * Math.tanh(x) -> float
336 *
337 * Returns the {hyperbolic tangent}[https://en.wikipedia.org/wiki/Hyperbolic_functions] of +x+
338 * in {radians}[https://en.wikipedia.org/wiki/Trigonometric_functions#Radians_versus_degrees].
339 *
340 * - Domain: <tt>[-INFINITY, INFINITY]</tt>.
341 * - Range: <tt>[-1, 1]</tt>.
342 *
343 * Examples:
344 *
345 * tanh(-INFINITY) # => -1.0
346 * tanh(0.0) # => 0.0
347 * tanh(INFINITY) # => 1.0
348 *
349 */
350
351static VALUE
352math_tanh(VALUE unused_obj, VALUE x)
353{
354 return DBL2NUM(tanh(Get_Double(x)));
355}
356
357/*
358 * call-seq:
359 * Math.acosh(x) -> float
360 *
361 * Returns the {inverse hyperbolic cosine}[https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions] of +x+.
362 *
363 * - Domain: <tt>[1, INFINITY]</tt>.
364 * - Range: <tt>[0, INFINITY]</tt>.
365 *
366 * Examples:
367 *
368 * acosh(1.0) # => 0.0
369 * acosh(INFINITY) # => Infinity
370 *
371 */
372
373static VALUE
374math_acosh(VALUE unused_obj, VALUE x)
375{
376 double d;
377
378 d = Get_Double(x);
379 domain_check_min(d, 1.0, "acosh");
380 return DBL2NUM(acosh(d));
381}
382
383/*
384 * call-seq:
385 * Math.asinh(x) -> float
386 *
387 * Returns the {inverse hyperbolic sine}[https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions] of +x+.
388 *
389 * - Domain: <tt>[-INFINITY, INFINITY]</tt>.
390 * - Range: <tt>[-INFINITY, INFINITY]</tt>.
391 *
392 * Examples:
393 *
394 * asinh(-INFINITY) # => -Infinity
395 * asinh(0.0) # => 0.0
396 * asinh(INFINITY) # => Infinity
397 *
398 */
399
400static VALUE
401math_asinh(VALUE unused_obj, VALUE x)
402{
403 return DBL2NUM(asinh(Get_Double(x)));
404}
405
406/*
407 * call-seq:
408 * Math.atanh(x) -> float
409 *
410 * Returns the {inverse hyperbolic tangent}[https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions] of +x+.
411 *
412 * - Domain: <tt>[-1, 1]</tt>.
413 * - Range: <tt>[-INFINITY, INFINITY]</tt>.
414 *
415 * Examples:
416 *
417 * atanh(-1.0) # => -Infinity
418 * atanh(0.0) # => 0.0
419 * atanh(1.0) # => Infinity
420 *
421 */
422
423static VALUE
424math_atanh(VALUE unused_obj, VALUE x)
425{
426 double d;
427
428 d = Get_Double(x);
429 domain_check_range(d, -1.0, +1.0, "atanh");
430 /* check for pole error */
431 if (d == -1.0) return DBL2NUM(-HUGE_VAL);
432 if (d == +1.0) return DBL2NUM(+HUGE_VAL);
433 return DBL2NUM(atanh(d));
434}
435
436/*
437 * call-seq:
438 * Math.exp(x) -> float
439 *
440 * Returns +e+ raised to the +x+ power.
441 *
442 * - Domain: <tt>[-INFINITY, INFINITY]</tt>.
443 * - Range: <tt>[0, INFINITY]</tt>.
444 *
445 * Examples:
446 *
447 * exp(-INFINITY) # => 0.0
448 * exp(-1.0) # => 0.36787944117144233 # 1.0/E
449 * exp(0.0) # => 1.0
450 * exp(0.5) # => 1.6487212707001282 # sqrt(E)
451 * exp(1.0) # => 2.718281828459045 # E
452 * exp(2.0) # => 7.38905609893065 # E**2
453 * exp(INFINITY) # => Infinity
454 *
455 */
456
457static VALUE
458math_exp(VALUE unused_obj, VALUE x)
459{
460 return DBL2NUM(exp(Get_Double(x)));
461}
462
463#if defined __CYGWIN__
464# include <cygwin/version.h>
465# if CYGWIN_VERSION_DLL_MAJOR < 1005
466# define nan(x) nan()
467# endif
468# define log(x) ((x) < 0.0 ? nan("") : log(x))
469# define log10(x) ((x) < 0.0 ? nan("") : log10(x))
470#endif
471
472#ifndef M_LN2
473# define M_LN2 0.693147180559945309417232121458176568
474#endif
475#ifndef M_LN10
476# define M_LN10 2.30258509299404568401799145468436421
477#endif
478
479static double math_log1(VALUE x);
480FUNC_MINIMIZED(static VALUE math_log(int, const VALUE *, VALUE));
481
482/*
483 * call-seq:
484 * Math.log(x, base = Math::E) -> Float
485 *
486 * Returns the base +base+ {logarithm}[https://en.wikipedia.org/wiki/Logarithm] of +x+.
487 *
488 * - Domain: <tt>[0, INFINITY]</tt>.
489 * - Range: <tt>[-INFINITY, INFINITY)]</tt>.
490 *
491 * Examples:
492 *
493 * log(0.0) # => -Infinity
494 * log(1.0) # => 0.0
495 * log(E) # => 1.0
496 * log(INFINITY) # => Infinity
497 *
498 * log(0.0, 2.0) # => -Infinity
499 * log(1.0, 2.0) # => 0.0
500 * log(2.0, 2.0) # => 1.0
501 *
502 * log(0.0, 10.0) # => -Infinity
503 * log(1.0, 10.0) # => 0.0
504 * log(10.0, 10.0) # => 1.0
505 *
506 */
507
508static VALUE
509math_log(int argc, const VALUE *argv, VALUE unused_obj)
510{
511 return rb_math_log(argc, argv);
512}
513
514VALUE
515rb_math_log(int argc, const VALUE *argv)
516{
517 VALUE x, base;
518 double d;
519
520 rb_scan_args(argc, argv, "11", &x, &base);
521 d = math_log1(x);
522 if (argc == 2) {
523 d /= math_log1(base);
524 }
525 return DBL2NUM(d);
526}
527
528static double
529get_double_rshift(VALUE x, size_t *pnumbits)
530{
531 size_t numbits;
532
533 if (RB_BIGNUM_TYPE_P(x) && BIGNUM_POSITIVE_P(x) &&
534 DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
535 numbits -= DBL_MANT_DIG;
536 x = rb_big_rshift(x, SIZET2NUM(numbits));
537 }
538 else {
539 numbits = 0;
540 }
541 *pnumbits = numbits;
542 return Get_Double(x);
543}
544
545static double
546math_log1(VALUE x)
547{
548 size_t numbits;
549 double d = get_double_rshift(x, &numbits);
550
551 domain_check_min(d, 0.0, "log");
552 /* check for pole error */
553 if (d == 0.0) return -HUGE_VAL;
554
555 return log(d) + numbits * M_LN2; /* log(d * 2 ** numbits) */
556}
557
558#ifndef log2
559#ifndef HAVE_LOG2
560double
561log2(double x)
562{
563 return log10(x)/log10(2.0);
564}
565#else
566extern double log2(double);
567#endif
568#endif
569
570/*
571 * call-seq:
572 * Math.log2(x) -> float
573 *
574 * Returns the base 2 {logarithm}[https://en.wikipedia.org/wiki/Logarithm] of +x+.
575 *
576 * - Domain: <tt>[0, INFINITY]</tt>.
577 * - Range: <tt>[-INFINITY, INFINITY]</tt>.
578 *
579 * Examples:
580 *
581 * log2(0.0) # => -Infinity
582 * log2(1.0) # => 0.0
583 * log2(2.0) # => 1.0
584 * log2(INFINITY) # => Infinity
585 *
586 */
587
588static VALUE
589math_log2(VALUE unused_obj, VALUE x)
590{
591 size_t numbits;
592 double d = get_double_rshift(x, &numbits);
593
594 domain_check_min(d, 0.0, "log2");
595 /* check for pole error */
596 if (d == 0.0) return DBL2NUM(-HUGE_VAL);
597
598 return DBL2NUM(log2(d) + numbits); /* log2(d * 2 ** numbits) */
599}
600
601/*
602 * call-seq:
603 * Math.log10(x) -> float
604 *
605 * Returns the base 10 {logarithm}[https://en.wikipedia.org/wiki/Logarithm] of +x+.
606 *
607 * - Domain: <tt>[0, INFINITY]</tt>.
608 * - Range: <tt>[-INFINITY, INFINITY]</tt>.
609 *
610 * Examples:
611 *
612 * log10(0.0) # => -Infinity
613 * log10(1.0) # => 0.0
614 * log10(10.0) # => 1.0
615 * log10(INFINITY) # => Infinity
616 *
617 */
618
619static VALUE
620math_log10(VALUE unused_obj, VALUE x)
621{
622 size_t numbits;
623 double d = get_double_rshift(x, &numbits);
624
625 domain_check_min(d, 0.0, "log10");
626 /* check for pole error */
627 if (d == 0.0) return DBL2NUM(-HUGE_VAL);
628
629 return DBL2NUM(log10(d) + numbits * log10(2)); /* log10(d * 2 ** numbits) */
630}
631
632static VALUE rb_math_sqrt(VALUE x);
633
634/*
635 * call-seq:
636 * Math.sqrt(x) -> float
637 *
638 * Returns the principal (non-negative) {square root}[https://en.wikipedia.org/wiki/Square_root] of +x+.
639 *
640 * - Domain: <tt>[0, INFINITY]</tt>.
641 * - Range: <tt>[0, INFINITY]</tt>.
642 *
643 * Examples:
644 *
645 * sqrt(0.0) # => 0.0
646 * sqrt(0.5) # => 0.7071067811865476
647 * sqrt(1.0) # => 1.0
648 * sqrt(2.0) # => 1.4142135623730951
649 * sqrt(4.0) # => 2.0
650 * sqrt(9.0) # => 3.0
651 * sqrt(INFINITY) # => Infinity
652 *
653 */
654
655static VALUE
656math_sqrt(VALUE unused_obj, VALUE x)
657{
658 return rb_math_sqrt(x);
659}
660
661inline static VALUE
662f_negative_p(VALUE x)
663{
664 if (FIXNUM_P(x))
665 return RBOOL(FIX2LONG(x) < 0);
666 return rb_funcall(x, '<', 1, INT2FIX(0));
667}
668inline static VALUE
669f_signbit(VALUE x)
670{
671 if (RB_FLOAT_TYPE_P(x)) {
672 double f = RFLOAT_VALUE(x);
673 return RBOOL(!isnan(f) && signbit(f));
674 }
675 return f_negative_p(x);
676}
677
678static VALUE
679rb_math_sqrt(VALUE x)
680{
681 double d;
682
683 if (RB_TYPE_P(x, T_COMPLEX)) {
684 VALUE neg = f_signbit(RCOMPLEX(x)->imag);
685 double re = Get_Double(RCOMPLEX(x)->real), im;
686 d = Get_Double(rb_complex_abs(x));
687 im = sqrt((d - re) / 2.0);
688 re = sqrt((d + re) / 2.0);
689 if (neg) im = -im;
690 return rb_complex_new(DBL2NUM(re), DBL2NUM(im));
691 }
692 d = Get_Double(x);
693 domain_check_min(d, 0.0, "sqrt");
694 if (d == 0.0) return DBL2NUM(0.0);
695 return DBL2NUM(sqrt(d));
696}
697
698/*
699 * call-seq:
700 * Math.cbrt(x) -> float
701 *
702 * Returns the {cube root}[https://en.wikipedia.org/wiki/Cube_root] of +x+.
703 *
704 * - Domain: <tt>[-INFINITY, INFINITY]</tt>.
705 * - Range: <tt>[-INFINITY, INFINITY]</tt>.
706 *
707 * Examples:
708 *
709 * cbrt(-INFINITY) # => -Infinity
710 * cbrt(-27.0) # => -3.0
711 * cbrt(-8.0) # => -2.0
712 * cbrt(-2.0) # => -1.2599210498948732
713 * cbrt(1.0) # => 1.0
714 * cbrt(0.0) # => 0.0
715 * cbrt(1.0) # => 1.0
716 cbrt(2.0) # => 1.2599210498948732
717 * cbrt(8.0) # => 2.0
718 * cbrt(27.0) # => 3.0
719 * cbrt(INFINITY) # => Infinity
720 *
721 */
722
723static VALUE
724math_cbrt(VALUE unused_obj, VALUE x)
725{
726 double f = Get_Double(x);
727 double r = cbrt(f);
728#if defined __GLIBC__
729 if (isfinite(r) && !(f == 0.0 && r == 0.0)) {
730 r = (2.0 * r + (f / r / r)) / 3.0;
731 }
732#endif
733 return DBL2NUM(r);
734}
735
736/*
737 * call-seq:
738 * Math.frexp(x) -> [fraction, exponent]
739 *
740 * Returns a 2-element array containing the normalized signed float +fraction+
741 * and integer +exponent+ of +x+ such that:
742 *
743 * x = fraction * 2**exponent
744 *
745 * See {IEEE 754 double-precision binary floating-point format: binary64}[https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64].
746 *
747 * - Domain: <tt>[-INFINITY, INFINITY]</tt>.
748 * - Range <tt>[-INFINITY, INFINITY]</tt>.
749 *
750 * Examples:
751 *
752 * frexp(-INFINITY) # => [-Infinity, -1]
753 * frexp(-2.0) # => [-0.5, 2]
754 * frexp(-1.0) # => [-0.5, 1]
755 * frexp(0.0) # => [0.0, 0]
756 * frexp(1.0) # => [0.5, 1]
757 * frexp(2.0) # => [0.5, 2]
758 * frexp(INFINITY) # => [Infinity, -1]
759 *
760 * Related: Math.ldexp (inverse of Math.frexp).
761 *
762 */
763
764static VALUE
765math_frexp(VALUE unused_obj, VALUE x)
766{
767 double d;
768 int exp;
769
770 d = frexp(Get_Double(x), &exp);
771 return rb_assoc_new(DBL2NUM(d), INT2NUM(exp));
772}
773
774/*
775 * call-seq:
776 * Math.ldexp(fraction, exponent) -> float
777 *
778 * Returns the value of <tt>fraction * 2**exponent</tt>.
779 *
780 * - Domain of +fraction+: <tt>[0.0, 1.0)</tt>.
781 * - Domain of +exponent+: <tt>[0, 1024]</tt>
782 * (larger values are equivalent to 1024).
783 *
784 * See {IEEE 754 double-precision binary floating-point format: binary64}[https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64].
785 *
786 * Examples:
787 *
788 * ldexp(-INFINITY, -1) # => -Infinity
789 * ldexp(-0.5, 2) # => -2.0
790 * ldexp(-0.5, 1) # => -1.0
791 * ldexp(0.0, 0) # => 0.0
792 * ldexp(-0.5, 1) # => 1.0
793 * ldexp(-0.5, 2) # => 2.0
794 * ldexp(INFINITY, -1) # => Infinity
795 *
796 * Related: Math.frexp (inverse of Math.ldexp).
797 *
798 */
799
800static VALUE
801math_ldexp(VALUE unused_obj, VALUE x, VALUE n)
802{
803 return DBL2NUM(ldexp(Get_Double(x), NUM2INT(n)));
804}
805
806/*
807 * call-seq:
808 * Math.hypot(a, b) -> float
809 *
810 * Returns <tt>sqrt(a**2 + b**2)</tt>,
811 * which is the length of the longest side +c+ (the hypotenuse)
812 * of the right triangle whose other sides have lengths +a+ and +b+.
813 *
814 * - Domain of +a+: <tt>[-INFINITY, INFINITY]</tt>.
815 * - Domain of +ab: <tt>[-INFINITY, INFINITY]</tt>.
816 * - Range: <tt>[0, INFINITY]</tt>.
817 *
818 * Examples:
819 *
820 * hypot(0.0, 1.0) # => 1.0
821 * hypot(1.0, 1.0) # => 1.4142135623730951 # sqrt(2.0)
822 * hypot(3.0, 4.0) # => 5.0
823 * hypot(5.0, 12.0) # => 13.0
824 * hypot(1.0, sqrt(3.0)) # => 1.9999999999999998 # Near 2.0
825 *
826 * Note that if either argument is +INFINITY+ or <tt>-INFINITY</tt>,
827 * the result is +Infinity+.
828 *
829 */
830
831static VALUE
832math_hypot(VALUE unused_obj, VALUE x, VALUE y)
833{
834 return DBL2NUM(hypot(Get_Double(x), Get_Double(y)));
835}
836
837/*
838 * call-seq:
839 * Math.erf(x) -> float
840 *
841 * Returns the value of the {Gauss error function}[https://en.wikipedia.org/wiki/Error_function] for +x+.
842 *
843 * - Domain: <tt>[-INFINITY, INFINITY]</tt>.
844 * - Range: <tt>[-1, 1]</tt>.
845 *
846 * Examples:
847 *
848 * erf(-INFINITY) # => -1.0
849 * erf(0.0) # => 0.0
850 * erf(INFINITY) # => 1.0
851 *
852 * Related: Math.erfc.
853 *
854 */
855
856static VALUE
857math_erf(VALUE unused_obj, VALUE x)
858{
859 return DBL2NUM(erf(Get_Double(x)));
860}
861
862/*
863 * call-seq:
864 * Math.erfc(x) -> Float
865 *
866 * Returns the value of the {complementary error function}[https://en.wikipedia.org/wiki/Error_function#Complementary_error_function] for +x+.
867 *
868 * - Domain: <tt>[-INFINITY, INFINITY]</tt>.
869 * - Range: <tt>[0, 2]</tt>.
870 *
871 * Examples:
872 *
873 * erfc(-INFINITY) # => 2.0
874 * erfc(0.0) # => 1.0
875 * erfc(INFINITY) # => 0.0
876 *
877 * Related: Math.erf.
878 *
879 */
880
881static VALUE
882math_erfc(VALUE unused_obj, VALUE x)
883{
884 return DBL2NUM(erfc(Get_Double(x)));
885}
886
887/*
888 * call-seq:
889 * Math.gamma(x) -> float
890 *
891 * Returns the value of the {gamma function}[https://en.wikipedia.org/wiki/Gamma_function] for +x+.
892 *
893 * - Domain: <tt>(-INFINITY, INFINITY]</tt> excluding negative integers.
894 * - Range: <tt>[-INFINITY, INFINITY]</tt>.
895 *
896 * Examples:
897 *
898 * gamma(-2.5) # => -0.9453087204829431
899 * gamma(-1.5) # => 2.3632718012073513
900 * gamma(-0.5) # => -3.5449077018110375
901 * gamma(0.0) # => Infinity
902 * gamma(1.0) # => 1.0
903 * gamma(2.0) # => 1.0
904 * gamma(3.0) # => 2.0
905 * gamma(4.0) # => 6.0
906 * gamma(5.0) # => 24.0
907 *
908 * Related: Math.lgamma.
909 *
910 */
911
912static VALUE
913math_gamma(VALUE unused_obj, VALUE x)
914{
915 static const double fact_table[] = {
916 /* fact(0) */ 1.0,
917 /* fact(1) */ 1.0,
918 /* fact(2) */ 2.0,
919 /* fact(3) */ 6.0,
920 /* fact(4) */ 24.0,
921 /* fact(5) */ 120.0,
922 /* fact(6) */ 720.0,
923 /* fact(7) */ 5040.0,
924 /* fact(8) */ 40320.0,
925 /* fact(9) */ 362880.0,
926 /* fact(10) */ 3628800.0,
927 /* fact(11) */ 39916800.0,
928 /* fact(12) */ 479001600.0,
929 /* fact(13) */ 6227020800.0,
930 /* fact(14) */ 87178291200.0,
931 /* fact(15) */ 1307674368000.0,
932 /* fact(16) */ 20922789888000.0,
933 /* fact(17) */ 355687428096000.0,
934 /* fact(18) */ 6402373705728000.0,
935 /* fact(19) */ 121645100408832000.0,
936 /* fact(20) */ 2432902008176640000.0,
937 /* fact(21) */ 51090942171709440000.0,
938 /* fact(22) */ 1124000727777607680000.0,
939 /* fact(23)=25852016738884976640000 needs 56bit mantissa which is
940 * impossible to represent exactly in IEEE 754 double which have
941 * 53bit mantissa. */
942 };
943 enum {NFACT_TABLE = numberof(fact_table)};
944 double d;
945 d = Get_Double(x);
946 /* check for domain error */
947 if (isinf(d)) {
948 if (signbit(d)) domain_error("gamma");
949 return DBL2NUM(HUGE_VAL);
950 }
951 if (d == 0.0) {
952 return signbit(d) ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
953 }
954 if (d == floor(d)) {
955 domain_check_min(d, 0.0, "gamma");
956 if (1.0 <= d && d <= (double)NFACT_TABLE) {
957 return DBL2NUM(fact_table[(int)d - 1]);
958 }
959 }
960 return DBL2NUM(tgamma(d));
961}
962
963/*
964 * call-seq:
965 * Math.lgamma(x) -> [float, -1 or 1]
966 *
967 * Returns a 2-element array equivalent to:
968 *
969 * [Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]
970 *
971 * See {logarithmic gamma function}[https://en.wikipedia.org/wiki/Gamma_function#The_log-gamma_function].
972 *
973 * - Domain: <tt>(-INFINITY, INFINITY]</tt>.
974 * - Range of first element: <tt>(-INFINITY, INFINITY]</tt>.
975 * - Second element is -1 or 1.
976 *
977 * Examples:
978 *
979 * lgamma(-4.0) # => [Infinity, -1]
980 * lgamma(-3.0) # => [Infinity, -1]
981 * lgamma(-2.0) # => [Infinity, -1]
982 * lgamma(-1.0) # => [Infinity, -1]
983 * lgamma(0.0) # => [Infinity, 1]
984 *
985 * lgamma(1.0) # => [0.0, 1]
986 * lgamma(2.0) # => [0.0, 1]
987 * lgamma(3.0) # => [0.6931471805599436, 1]
988 * lgamma(4.0) # => [1.7917594692280545, 1]
989 *
990 * lgamma(-2.5) # => [-0.05624371649767279, -1]
991 * lgamma(-1.5) # => [0.8600470153764797, 1]
992 * lgamma(-0.5) # => [1.265512123484647, -1]
993 * lgamma(0.5) # => [0.5723649429247004, 1]
994 * lgamma(1.5) # => [-0.12078223763524676, 1]
995 * lgamma(2.5) # => [0.2846828704729205, 1]
996 *
997 * Related: Math.gamma.
998 *
999 */
1000
1001static VALUE
1002math_lgamma(VALUE unused_obj, VALUE x)
1003{
1004 double d;
1005 int sign=1;
1006 VALUE v;
1007 d = Get_Double(x);
1008 /* check for domain error */
1009 if (isinf(d)) {
1010 if (signbit(d)) domain_error("lgamma");
1011 return rb_assoc_new(DBL2NUM(HUGE_VAL), INT2FIX(1));
1012 }
1013 if (d == 0.0) {
1014 VALUE vsign = signbit(d) ? INT2FIX(-1) : INT2FIX(+1);
1015 return rb_assoc_new(DBL2NUM(HUGE_VAL), vsign);
1016 }
1017 v = DBL2NUM(lgamma_r(d, &sign));
1018 return rb_assoc_new(v, INT2FIX(sign));
1019}
1020
1021
1022#define exp1(n) \
1023VALUE \
1024rb_math_##n(VALUE x)\
1025{\
1026 return math_##n(0, x);\
1027}
1028
1029#define exp2(n) \
1030VALUE \
1031rb_math_##n(VALUE x, VALUE y)\
1032{\
1033 return math_##n(0, x, y);\
1034}
1035
1036exp2(atan2)
1037exp1(cos)
1038exp1(cosh)
1039exp1(exp)
1040exp2(hypot)
1041exp1(sin)
1042exp1(sinh)
1043#if 0
1044exp1(sqrt)
1045#endif
1046
1047
1048/*
1049 * Document-class: Math::DomainError
1050 *
1051 * Raised when a mathematical function is evaluated outside of its
1052 * domain of definition.
1053 *
1054 * For example, since +cos+ returns values in the range -1..1,
1055 * its inverse function +acos+ is only defined on that interval:
1056 *
1057 * Math.acos(42)
1058 *
1059 * <em>produces:</em>
1060 *
1061 * Math::DomainError: Numerical argument is out of domain - "acos"
1062 */
1063
1064/*
1065 * Document-class: Math
1066 *
1067 * :include: doc/math/math.rdoc
1068 *
1069 */
1070
1071
1072void
1073InitVM_Math(void)
1074{
1075 rb_mMath = rb_define_module("Math");
1077
1078 /* Definition of the mathematical constant PI as a Float number. */
1079 rb_define_const(rb_mMath, "PI", DBL2NUM(M_PI));
1080
1081#ifdef M_E
1082 /* Definition of the mathematical constant E for Euler's number (e) as a Float number. */
1083 rb_define_const(rb_mMath, "E", DBL2NUM(M_E));
1084#else
1085 rb_define_const(rb_mMath, "E", DBL2NUM(exp(1.0)));
1086#endif
1087
1088 rb_define_module_function(rb_mMath, "atan2", math_atan2, 2);
1089 rb_define_module_function(rb_mMath, "cos", math_cos, 1);
1090 rb_define_module_function(rb_mMath, "sin", math_sin, 1);
1091 rb_define_module_function(rb_mMath, "tan", math_tan, 1);
1092
1093 rb_define_module_function(rb_mMath, "acos", math_acos, 1);
1094 rb_define_module_function(rb_mMath, "asin", math_asin, 1);
1095 rb_define_module_function(rb_mMath, "atan", math_atan, 1);
1096
1097 rb_define_module_function(rb_mMath, "cosh", math_cosh, 1);
1098 rb_define_module_function(rb_mMath, "sinh", math_sinh, 1);
1099 rb_define_module_function(rb_mMath, "tanh", math_tanh, 1);
1100
1101 rb_define_module_function(rb_mMath, "acosh", math_acosh, 1);
1102 rb_define_module_function(rb_mMath, "asinh", math_asinh, 1);
1103 rb_define_module_function(rb_mMath, "atanh", math_atanh, 1);
1104
1105 rb_define_module_function(rb_mMath, "exp", math_exp, 1);
1106 rb_define_module_function(rb_mMath, "log", math_log, -1);
1107 rb_define_module_function(rb_mMath, "log2", math_log2, 1);
1108 rb_define_module_function(rb_mMath, "log10", math_log10, 1);
1109 rb_define_module_function(rb_mMath, "sqrt", math_sqrt, 1);
1110 rb_define_module_function(rb_mMath, "cbrt", math_cbrt, 1);
1111
1112 rb_define_module_function(rb_mMath, "frexp", math_frexp, 1);
1113 rb_define_module_function(rb_mMath, "ldexp", math_ldexp, 2);
1114
1115 rb_define_module_function(rb_mMath, "hypot", math_hypot, 2);
1116
1117 rb_define_module_function(rb_mMath, "erf", math_erf, 1);
1118 rb_define_module_function(rb_mMath, "erfc", math_erfc, 1);
1119
1120 rb_define_module_function(rb_mMath, "gamma", math_gamma, 1);
1121 rb_define_module_function(rb_mMath, "lgamma", math_lgamma, 1);
1122}
1123
1124void
1125Init_Math(void)
1126{
1127 InitVM(Math);
1128}
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:955
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1033
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:2574
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition value_type.h:59
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
#define FIXNUM_P
Old name of RB_FIXNUM_P.
VALUE rb_eStandardError
StandardError exception.
Definition error.c:1088
VALUE rb_eMathDomainError
Math::DomainError exception.
Definition math.c:30
VALUE rb_mMath
Math module.
Definition math.c:29
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1102
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
Definition variable.c:3440
#define InitVM(ext)
This macro is for internal use.
Definition ruby.h:230
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
Definition value_type.h:263
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:375