Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
math.c
Go to the documentation of this file.
1 /*
2 ** math.c - Math module
3 **
4 ** See Copyright Notice in mruby.h
5 */
6 
7 #include "mruby.h"
8 #include "mruby/array.h"
9 
10 #include <math.h>
11 
12 #define domain_error(msg) \
13  mrb_raise(mrb, E_RANGE_ERROR, "Numerical argument is out of domain - " #msg)
14 
15 /* math functions not provided by Microsoft Visual C++ 2012 or older */
16 #if defined _MSC_VER && _MSC_VER < 1800
17 
18 #define MATH_TOLERANCE 1E-12
19 
20 #define asinh(x) log(x + sqrt(pow(x,2.0) + 1))
21 #define acosh(x) log(x + sqrt(pow(x,2.0) - 1))
22 #define atanh(x) (log(1+x) - log(1-x))/2.0
23 #define cbrt(x) pow(x,1.0/3.0)
24 
25 /* Declaration of complementary Error function */
26 double
27 erfc(double x);
28 
29 /*
30 ** Implementations of error functions
31 ** credits to http://www.digitalmars.com/archives/cplusplus/3634.html
32 */
33 
34 /* Implementation of Error function */
35 double
36 erf(double x)
37 {
38  static const double two_sqrtpi = 1.128379167095512574;
39  double sum = x;
40  double term = x;
41  double xsqr = x*x;
42  int j= 1;
43  if (fabs(x) > 2.2) {
44  return 1.0 - erfc(x);
45  }
46  do {
47  term *= xsqr/j;
48  sum -= term/(2*j+1);
49  ++j;
50  term *= xsqr/j;
51  sum += term/(2*j+1);
52  ++j;
53  } while (fabs(term/sum) > MATH_TOLERANCE);
54  return two_sqrtpi*sum;
55 }
56 
57 /* Implementation of complementary Error function */
58 double
59 erfc(double x)
60 {
61  static const double one_sqrtpi= 0.564189583547756287;
62  double a = 1;
63  double b = x;
64  double c = x;
65  double d = x*x+0.5;
66  double q1;
67  double q2 = b/d;
68  double n = 1.0;
69  double t;
70  if (fabs(x) < 2.2) {
71  return 1.0 - erf(x);
72  }
73  if (x < 0.0) { /*signbit(x)*/
74  return 2.0 - erfc(-x);
75  }
76  do {
77  t = a*n+b*x;
78  a = b;
79  b = t;
80  t = c*n+d*x;
81  c = d;
82  d = t;
83  n += 0.5;
84  q1 = q2;
85  q2 = b/d;
86  } while (fabs(q1-q2)/q2 > MATH_TOLERANCE);
87  return one_sqrtpi*exp(-x*x)*q2;
88 }
89 
90 #endif
91 
92 #if (defined _MSC_VER && _MSC_VER < 1800) || defined __ANDROID__
93 
94 double
95 log2(double x)
96 {
97  return log10(x)/log10(2.0);
98 }
99 
100 #endif
101 
102 /*
103  TRIGONOMETRIC FUNCTIONS
104 */
105 
106 /*
107  * call-seq:
108  * Math.sin(x) -> float
109  *
110  * Computes the sine of <i>x</i> (expressed in radians). Returns
111  * -1..1.
112  */
113 static mrb_value
114 math_sin(mrb_state *mrb, mrb_value obj)
115 {
116  mrb_float x;
117 
118  mrb_get_args(mrb, "f", &x);
119  x = sin(x);
120 
121  return mrb_float_value(mrb, x);
122 }
123 
124 /*
125  * call-seq:
126  * Math.cos(x) -> float
127  *
128  * Computes the cosine of <i>x</i> (expressed in radians). Returns
129  * -1..1.
130  */
131 static mrb_value
132 math_cos(mrb_state *mrb, mrb_value obj)
133 {
134  mrb_float x;
135 
136  mrb_get_args(mrb, "f", &x);
137  x = cos(x);
138 
139  return mrb_float_value(mrb, x);
140 }
141 
142 /*
143  * call-seq:
144  * Math.tan(x) -> float
145  *
146  * Returns the tangent of <i>x</i> (expressed in radians).
147  */
148 static mrb_value
149 math_tan(mrb_state *mrb, mrb_value obj)
150 {
151  mrb_float x;
152 
153  mrb_get_args(mrb, "f", &x);
154  x = tan(x);
155 
156  return mrb_float_value(mrb, x);
157 }
158 
159 /*
160  INVERSE TRIGONOMETRIC FUNCTIONS
161 */
162 
163 /*
164  * call-seq:
165  * Math.asin(x) -> float
166  *
167  * Computes the arc sine of <i>x</i>. Returns -{PI/2} .. {PI/2}.
168  */
169 static mrb_value
170 math_asin(mrb_state *mrb, mrb_value obj)
171 {
172  mrb_float x;
173 
174  mrb_get_args(mrb, "f", &x);
175  x = asin(x);
176 
177  return mrb_float_value(mrb, x);
178 }
179 
180 /*
181  * call-seq:
182  * Math.acos(x) -> float
183  *
184  * Computes the arc cosine of <i>x</i>. Returns 0..PI.
185  */
186 static mrb_value
187 math_acos(mrb_state *mrb, mrb_value obj)
188 {
189  mrb_float x;
190 
191  mrb_get_args(mrb, "f", &x);
192  x = acos(x);
193 
194  return mrb_float_value(mrb, x);
195 }
196 
197 /*
198  * call-seq:
199  * Math.atan(x) -> float
200  *
201  * Computes the arc tangent of <i>x</i>. Returns -{PI/2} .. {PI/2}.
202  */
203 static mrb_value
204 math_atan(mrb_state *mrb, mrb_value obj)
205 {
206  mrb_float x;
207 
208  mrb_get_args(mrb, "f", &x);
209  x = atan(x);
210 
211  return mrb_float_value(mrb, x);
212 }
213 
214 /*
215  * call-seq:
216  * Math.atan2(y, x) -> float
217  *
218  * Computes the arc tangent given <i>y</i> and <i>x</i>. Returns
219  * -PI..PI.
220  *
221  * Math.atan2(-0.0, -1.0) #=> -3.141592653589793
222  * Math.atan2(-1.0, -1.0) #=> -2.356194490192345
223  * Math.atan2(-1.0, 0.0) #=> -1.5707963267948966
224  * Math.atan2(-1.0, 1.0) #=> -0.7853981633974483
225  * Math.atan2(-0.0, 1.0) #=> -0.0
226  * Math.atan2(0.0, 1.0) #=> 0.0
227  * Math.atan2(1.0, 1.0) #=> 0.7853981633974483
228  * Math.atan2(1.0, 0.0) #=> 1.5707963267948966
229  * Math.atan2(1.0, -1.0) #=> 2.356194490192345
230  * Math.atan2(0.0, -1.0) #=> 3.141592653589793
231  *
232  */
233 static mrb_value
234 math_atan2(mrb_state *mrb, mrb_value obj)
235 {
236  mrb_float x, y;
237 
238  mrb_get_args(mrb, "ff", &x, &y);
239  x = atan2(x, y);
240 
241  return mrb_float_value(mrb, x);
242 }
243 
244 
245 
246 /*
247  HYPERBOLIC TRIG FUNCTIONS
248 */
249 /*
250  * call-seq:
251  * Math.sinh(x) -> float
252  *
253  * Computes the hyperbolic sine of <i>x</i> (expressed in
254  * radians).
255  */
256 static mrb_value
257 math_sinh(mrb_state *mrb, mrb_value obj)
258 {
259  mrb_float x;
260 
261  mrb_get_args(mrb, "f", &x);
262  x = sinh(x);
263 
264  return mrb_float_value(mrb, x);
265 }
266 
267 /*
268  * call-seq:
269  * Math.cosh(x) -> float
270  *
271  * Computes the hyperbolic cosine of <i>x</i> (expressed in radians).
272  */
273 static mrb_value
274 math_cosh(mrb_state *mrb, mrb_value obj)
275 {
276  mrb_float x;
277 
278  mrb_get_args(mrb, "f", &x);
279  x = cosh(x);
280 
281  return mrb_float_value(mrb, x);
282 }
283 
284 /*
285  * call-seq:
286  * Math.tanh() -> float
287  *
288  * Computes the hyperbolic tangent of <i>x</i> (expressed in
289  * radians).
290  */
291 static mrb_value
292 math_tanh(mrb_state *mrb, mrb_value obj)
293 {
294  mrb_float x;
295 
296  mrb_get_args(mrb, "f", &x);
297  x = tanh(x);
298 
299  return mrb_float_value(mrb, x);
300 }
301 
302 
303 /*
304  INVERSE HYPERBOLIC TRIG FUNCTIONS
305 */
306 
307 /*
308  * call-seq:
309  * Math.asinh(x) -> float
310  *
311  * Computes the inverse hyperbolic sine of <i>x</i>.
312  */
313 static mrb_value
314 math_asinh(mrb_state *mrb, mrb_value obj)
315 {
316  mrb_float x;
317 
318  mrb_get_args(mrb, "f", &x);
319 
320  x = asinh(x);
321 
322  return mrb_float_value(mrb, x);
323 }
324 
325 /*
326  * call-seq:
327  * Math.acosh(x) -> float
328  *
329  * Computes the inverse hyperbolic cosine of <i>x</i>.
330  */
331 static mrb_value
332 math_acosh(mrb_state *mrb, mrb_value obj)
333 {
334  mrb_float x;
335 
336  mrb_get_args(mrb, "f", &x);
337  x = acosh(x);
338 
339  return mrb_float_value(mrb, x);
340 }
341 
342 /*
343  * call-seq:
344  * Math.atanh(x) -> float
345  *
346  * Computes the inverse hyperbolic tangent of <i>x</i>.
347  */
348 static mrb_value
349 math_atanh(mrb_state *mrb, mrb_value obj)
350 {
351  mrb_float x;
352 
353  mrb_get_args(mrb, "f", &x);
354  x = atanh(x);
355 
356  return mrb_float_value(mrb, x);
357 }
358 
359 /*
360  EXPONENTIALS AND LOGARITHMS
361 */
362 #if defined __CYGWIN__
363 # include <cygwin/version.h>
364 # if CYGWIN_VERSION_DLL_MAJOR < 1005
365 # define nan(x) nan()
366 # endif
367 # define log(x) ((x) < 0.0 ? nan("") : log(x))
368 # define log10(x) ((x) < 0.0 ? nan("") : log10(x))
369 #endif
370 
371 /*
372  * call-seq:
373  * Math.exp(x) -> float
374  *
375  * Returns e**x.
376  *
377  * Math.exp(0) #=> 1.0
378  * Math.exp(1) #=> 2.718281828459045
379  * Math.exp(1.5) #=> 4.4816890703380645
380  *
381  */
382 static mrb_value
383 math_exp(mrb_state *mrb, mrb_value obj)
384 {
385  mrb_float x;
386 
387  mrb_get_args(mrb, "f", &x);
388  x = exp(x);
389 
390  return mrb_float_value(mrb, x);
391 }
392 
393 /*
394  * call-seq:
395  * Math.log(numeric) -> float
396  * Math.log(num,base) -> float
397  *
398  * Returns the natural logarithm of <i>numeric</i>.
399  * If additional second argument is given, it will be the base
400  * of logarithm.
401  *
402  * Math.log(1) #=> 0.0
403  * Math.log(Math::E) #=> 1.0
404  * Math.log(Math::E**3) #=> 3.0
405  * Math.log(12,3) #=> 2.2618595071429146
406  *
407  */
408 static mrb_value
409 math_log(mrb_state *mrb, mrb_value obj)
410 {
411  mrb_float x, base;
412  int argc;
413 
414  argc = mrb_get_args(mrb, "f|f", &x, &base);
415  x = log(x);
416  if (argc == 2) {
417  x /= log(base);
418  }
419  return mrb_float_value(mrb, x);
420 }
421 
422 /*
423  * call-seq:
424  * Math.log2(numeric) -> float
425  *
426  * Returns the base 2 logarithm of <i>numeric</i>.
427  *
428  * Math.log2(1) #=> 0.0
429  * Math.log2(2) #=> 1.0
430  * Math.log2(32768) #=> 15.0
431  * Math.log2(65536) #=> 16.0
432  *
433  */
434 static mrb_value
435 math_log2(mrb_state *mrb, mrb_value obj)
436 {
437  mrb_float x;
438 
439  mrb_get_args(mrb, "f", &x);
440  x = log2(x);
441 
442  return mrb_float_value(mrb, x);
443 }
444 
445 /*
446  * call-seq:
447  * Math.log10(numeric) -> float
448  *
449  * Returns the base 10 logarithm of <i>numeric</i>.
450  *
451  * Math.log10(1) #=> 0.0
452  * Math.log10(10) #=> 1.0
453  * Math.log10(10**100) #=> 100.0
454  *
455  */
456 static mrb_value
457 math_log10(mrb_state *mrb, mrb_value obj)
458 {
459  mrb_float x;
460 
461  mrb_get_args(mrb, "f", &x);
462  x = log10(x);
463 
464  return mrb_float_value(mrb, x);
465 }
466 
467 /*
468  * call-seq:
469  * Math.sqrt(numeric) -> float
470  *
471  * Returns the square root of <i>numeric</i>.
472  *
473  */
474 static mrb_value
475 math_sqrt(mrb_state *mrb, mrb_value obj)
476 {
477  mrb_float x;
478 
479  mrb_get_args(mrb, "f", &x);
480  x = sqrt(x);
481 
482  return mrb_float_value(mrb, x);
483 }
484 
485 
486 /*
487  * call-seq:
488  * Math.cbrt(numeric) -> float
489  *
490  * Returns the cube root of <i>numeric</i>.
491  *
492  * -9.upto(9) {|x|
493  * p [x, Math.cbrt(x), Math.cbrt(x)**3]
494  * }
495  * #=>
496  * [-9, -2.0800838230519, -9.0]
497  * [-8, -2.0, -8.0]
498  * [-7, -1.91293118277239, -7.0]
499  * [-6, -1.81712059283214, -6.0]
500  * [-5, -1.7099759466767, -5.0]
501  * [-4, -1.5874010519682, -4.0]
502  * [-3, -1.44224957030741, -3.0]
503  * [-2, -1.25992104989487, -2.0]
504  * [-1, -1.0, -1.0]
505  * [0, 0.0, 0.0]
506  * [1, 1.0, 1.0]
507  * [2, 1.25992104989487, 2.0]
508  * [3, 1.44224957030741, 3.0]
509  * [4, 1.5874010519682, 4.0]
510  * [5, 1.7099759466767, 5.0]
511  * [6, 1.81712059283214, 6.0]
512  * [7, 1.91293118277239, 7.0]
513  * [8, 2.0, 8.0]
514  * [9, 2.0800838230519, 9.0]
515  *
516  */
517 static mrb_value
518 math_cbrt(mrb_state *mrb, mrb_value obj)
519 {
520  mrb_float x;
521 
522  mrb_get_args(mrb, "f", &x);
523  x = cbrt(x);
524 
525  return mrb_float_value(mrb, x);
526 }
527 
528 
529 /*
530  * call-seq:
531  * Math.frexp(numeric) -> [ fraction, exponent ]
532  *
533  * Returns a two-element array containing the normalized fraction (a
534  * <code>Float</code>) and exponent (a <code>Fixnum</code>) of
535  * <i>numeric</i>.
536  *
537  * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11]
538  * fraction * 2**exponent #=> 1234.0
539  */
540 static mrb_value
541 math_frexp(mrb_state *mrb, mrb_value obj)
542 {
543  mrb_float x;
544  int exp;
545 
546  mrb_get_args(mrb, "f", &x);
547  x = frexp(x, &exp);
548 
549  return mrb_assoc_new(mrb, mrb_float_value(mrb, x), mrb_fixnum_value(exp));
550 }
551 
552 /*
553  * call-seq:
554  * Math.ldexp(flt, int) -> float
555  *
556  * Returns the value of <i>flt</i>*(2**<i>int</i>).
557  *
558  * fraction, exponent = Math.frexp(1234)
559  * Math.ldexp(fraction, exponent) #=> 1234.0
560  */
561 static mrb_value
562 math_ldexp(mrb_state *mrb, mrb_value obj)
563 {
564  mrb_float x;
565  mrb_int i;
566 
567  mrb_get_args(mrb, "fi", &x, &i);
568  x = ldexp(x, i);
569 
570  return mrb_float_value(mrb, x);
571 }
572 
573 /*
574  * call-seq:
575  * Math.hypot(x, y) -> float
576  *
577  * Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle
578  * with sides <i>x</i> and <i>y</i>.
579  *
580  * Math.hypot(3, 4) #=> 5.0
581  */
582 static mrb_value
583 math_hypot(mrb_state *mrb, mrb_value obj)
584 {
585  mrb_float x, y;
586 
587  mrb_get_args(mrb, "ff", &x, &y);
588  x = hypot(x, y);
589 
590  return mrb_float_value(mrb, x);
591 }
592 
593 /*
594  * call-seq:
595  * Math.erf(x) -> float
596  *
597  * Calculates the error function of x.
598  */
599 static mrb_value
600 math_erf(mrb_state *mrb, mrb_value obj)
601 {
602  mrb_float x;
603 
604  mrb_get_args(mrb, "f", &x);
605  x = erf(x);
606 
607  return mrb_float_value(mrb, x);
608 }
609 
610 
611 /*
612  * call-seq:
613  * Math.erfc(x) -> float
614  *
615  * Calculates the complementary error function of x.
616  */
617 static mrb_value
618 math_erfc(mrb_state *mrb, mrb_value obj)
619 {
620  mrb_float x;
621 
622  mrb_get_args(mrb, "f", &x);
623  x = erfc(x);
624 
625  return mrb_float_value(mrb, x);
626 }
627 
628 /* ------------------------------------------------------------------------*/
629 void
631 {
632  struct RClass *mrb_math;
633  mrb_math = mrb_define_module(mrb, "Math");
634 
635 #ifdef M_PI
636  mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(mrb, M_PI));
637 #else
638  mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(mrb, atan(1.0)*4.0));
639 #endif
640 
641 #ifdef M_E
642  mrb_define_const(mrb, mrb_math, "E", mrb_float_value(mrb, M_E));
643 #else
644  mrb_define_const(mrb, mrb_math, "E", mrb_float_value(mrb, exp(1.0)));
645 #endif
646 
647 #ifdef MRB_USE_FLOAT
648  mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(mrb, 1e-5));
649 #else
650  mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(mrb, 1e-12));
651 #endif
652 
653  mrb_define_module_function(mrb, mrb_math, "sin", math_sin, MRB_ARGS_REQ(1));
654  mrb_define_module_function(mrb, mrb_math, "cos", math_cos, MRB_ARGS_REQ(1));
655  mrb_define_module_function(mrb, mrb_math, "tan", math_tan, MRB_ARGS_REQ(1));
656 
657  mrb_define_module_function(mrb, mrb_math, "asin", math_asin, MRB_ARGS_REQ(1));
658  mrb_define_module_function(mrb, mrb_math, "acos", math_acos, MRB_ARGS_REQ(1));
659  mrb_define_module_function(mrb, mrb_math, "atan", math_atan, MRB_ARGS_REQ(1));
660  mrb_define_module_function(mrb, mrb_math, "atan2", math_atan2, MRB_ARGS_REQ(2));
661 
662  mrb_define_module_function(mrb, mrb_math, "sinh", math_sinh, MRB_ARGS_REQ(1));
663  mrb_define_module_function(mrb, mrb_math, "cosh", math_cosh, MRB_ARGS_REQ(1));
664  mrb_define_module_function(mrb, mrb_math, "tanh", math_tanh, MRB_ARGS_REQ(1));
665 
666  mrb_define_module_function(mrb, mrb_math, "asinh", math_asinh, MRB_ARGS_REQ(1));
667  mrb_define_module_function(mrb, mrb_math, "acosh", math_acosh, MRB_ARGS_REQ(1));
668  mrb_define_module_function(mrb, mrb_math, "atanh", math_atanh, MRB_ARGS_REQ(1));
669 
670  mrb_define_module_function(mrb, mrb_math, "exp", math_exp, MRB_ARGS_REQ(1));
671  mrb_define_module_function(mrb, mrb_math, "log", math_log, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
672  mrb_define_module_function(mrb, mrb_math, "log2", math_log2, MRB_ARGS_REQ(1));
673  mrb_define_module_function(mrb, mrb_math, "log10", math_log10, MRB_ARGS_REQ(1));
674  mrb_define_module_function(mrb, mrb_math, "sqrt", math_sqrt, MRB_ARGS_REQ(1));
675  mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, MRB_ARGS_REQ(1));
676 
677  mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, MRB_ARGS_REQ(1));
678  mrb_define_module_function(mrb, mrb_math, "ldexp", math_ldexp, MRB_ARGS_REQ(2));
679 
680  mrb_define_module_function(mrb, mrb_math, "hypot", math_hypot, MRB_ARGS_REQ(2));
681 
682  mrb_define_module_function(mrb, mrb_math, "erf", math_erf, MRB_ARGS_REQ(1));
683  mrb_define_module_function(mrb, mrb_math, "erfc", math_erfc, MRB_ARGS_REQ(1));
684 }
685 
686 void
688 {
689 }