Module: Math
- Defined in:
- mrbgems/mruby-math/src/math.c
Class Method Summary collapse
-
.acos(x) ⇒ Float
Computes the arc cosine of x.
-
.acosh(x) ⇒ Float
Computes the inverse hyperbolic cosine of x.
-
.asin(x) ⇒ Float
Computes the arc sine of x.
-
.asinh(x) ⇒ Float
Computes the inverse hyperbolic sine of x.
-
.atan(x) ⇒ Float
Computes the arc tangent of x.
-
.atan2(y, x) ⇒ Float
Computes the arc tangent given y and x.
-
.atanh(x) ⇒ Float
Computes the inverse hyperbolic tangent of x.
-
.cbrt(numeric) ⇒ Float
Returns the cube root of numeric.
-
.cos(x) ⇒ Float
Computes the cosine of x (expressed in radians).
-
.cosh(x) ⇒ Float
Computes the hyperbolic cosine of x (expressed in radians).
-
.erf(x) ⇒ Float
Calculates the error function of x.
-
.erfc(x) ⇒ Float
Calculates the complementary error function of x.
-
.exp(x) ⇒ Float
Returns e**x.
-
.frexp(numeric) ⇒ Array
Returns a two-element array containing the normalized fraction (a
Float) and exponent (aInteger) of numeric. -
.hypot(x, y) ⇒ Float
Returns sqrt(x2 + y2), the hypotenuse of a right-angled triangle with sides x and y.
-
.ldexp(flt, int) ⇒ Float
Returns the value of flt(2*int).
-
.log ⇒ Object
Returns the natural logarithm of numeric.
-
.log10(numeric) ⇒ Float
Returns the base 10 logarithm of numeric.
-
.log2(numeric) ⇒ Float
Returns the base 2 logarithm of numeric.
-
.sin(x) ⇒ Float
Computes the sine of x (expressed in radians).
-
.sinh(x) ⇒ Float
Computes the hyperbolic sine of x (expressed in radians).
-
.sqrt(numeric) ⇒ Float
Returns the square root of numeric.
-
.tan(x) ⇒ Float
Returns the tangent of x (expressed in radians).
-
.tanh ⇒ Float
Computes the hyperbolic tangent of x (expressed in radians).
Class Method Details
.acos(x) ⇒ Float
Computes the arc cosine of x. Returns 0..PI.
254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'mrbgems/mruby-math/src/math.c', line 254 static mrb_value math_acos(mrb_state *mrb, mrb_value obj) { mrb_float x = get_float_arg(mrb); if (x < -1.0 || x > 1.0) { domain_error(mrb, "acos"); } x = acos(x); return mrb_float_value(mrb, x); } |
.acosh(x) ⇒ Float
Computes the inverse hyperbolic cosine of x.
378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'mrbgems/mruby-math/src/math.c', line 378 static mrb_value math_acosh(mrb_state *mrb, mrb_value obj) { mrb_float x = get_float_arg(mrb); if (x < 1.0) { domain_error(mrb, "acosh"); } x = acosh(x); return mrb_float_value(mrb, x); } |
.asin(x) ⇒ Float
Computes the arc sine of x.
235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'mrbgems/mruby-math/src/math.c', line 235 static mrb_value math_asin(mrb_state *mrb, mrb_value obj) { mrb_float x = get_float_arg(mrb); if (x < -1.0 || x > 1.0) { domain_error(mrb, "asin"); } x = asin(x); return mrb_float_value(mrb, x); } |
.asinh(x) ⇒ Float
Computes the inverse hyperbolic sine of x.
365 366 367 368 369 370 |
# File 'mrbgems/mruby-math/src/math.c', line 365 static mrb_value math_asinh(mrb_state *mrb, mrb_value obj) { mrb_float x = asinh(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.atan(x) ⇒ Float
Computes the arc tangent of x. Returns -(PI/2) .. (PI/2).
273 274 275 276 277 278 |
# File 'mrbgems/mruby-math/src/math.c', line 273 static mrb_value math_atan(mrb_state *mrb, mrb_value obj) { mrb_float x = atan(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.atan2(y, x) ⇒ Float
Computes the arc tangent given y and x. Returns -PI..PI.
Math.atan2(-0.0, -1.0) #=> -3.141592653589793 Math.atan2(-1.0, -1.0) #=> -2.356194490192345 Math.atan2(-1.0, 0.0) #=> -1.5707963267948966 Math.atan2(-1.0, 1.0) #=> -0.7853981633974483 Math.atan2(-0.0, 1.0) #=> -0.0 Math.atan2(0.0, 1.0) #=> 0.0 Math.atan2(1.0, 1.0) #=> 0.7853981633974483 Math.atan2(1.0, 0.0) #=> 1.5707963267948966 Math.atan2(1.0, -1.0) #=> 2.356194490192345 Math.atan2(0.0, -1.0) #=> 3.141592653589793
299 300 301 302 303 304 305 306 307 308 |
# File 'mrbgems/mruby-math/src/math.c', line 299 static mrb_value math_atan2(mrb_state *mrb, mrb_value obj) { mrb_float x, y; mrb_get_args(mrb, "ff", &x, &y); x = atan2(x, y); return mrb_float_value(mrb, x); } |
.atanh(x) ⇒ Float
Computes the inverse hyperbolic tangent of x.
397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'mrbgems/mruby-math/src/math.c', line 397 static mrb_value math_atanh(mrb_state *mrb, mrb_value obj) { mrb_float x = get_float_arg(mrb); if (x < -1.0 || x > 1.0) { domain_error(mrb, "atanh"); } x = atanh(x); return mrb_float_value(mrb, x); } |
.cbrt(numeric) ⇒ Float
Returns the cube root of numeric.
-9.upto(9) {|x| p [x, Math.cbrt(x), Math.cbrt(x)**3] } #=> [-9, -2.0800838230519, -9.0] [-8, -2.0, -8.0] [-7, -1.91293118277239, -7.0] [-6, -1.81712059283214, -6.0] [-5, -1.7099759466767, -5.0] [-4, -1.5874010519682, -4.0] [-3, -1.44224957030741, -3.0] [-2, -1.25992104989487, -2.0] [-1, -1.0, -1.0] [0, 0.0, 0.0] [1, 1.0, 1.0] [2, 1.25992104989487, 2.0] [3, 1.44224957030741, 3.0] [4, 1.5874010519682, 4.0] [5, 1.7099759466767, 5.0] [6, 1.81712059283214, 6.0] [7, 1.91293118277239, 7.0] [8, 2.0, 8.0] [9, 2.0800838230519, 9.0]
568 569 570 571 572 573 |
# File 'mrbgems/mruby-math/src/math.c', line 568 static mrb_value math_cbrt(mrb_state *mrb, mrb_value obj) { mrb_float x = cbrt(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.cos(x) ⇒ Float
Computes the cosine of x (expressed in radians). Returns -1..1.
204 205 206 207 208 209 |
# File 'mrbgems/mruby-math/src/math.c', line 204 static mrb_value math_cos(mrb_state *mrb, mrb_value obj) { mrb_float x = cos(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.cosh(x) ⇒ Float
Computes the hyperbolic cosine of x (expressed in radians).
333 334 335 336 337 338 |
# File 'mrbgems/mruby-math/src/math.c', line 333 static mrb_value math_cosh(mrb_state *mrb, mrb_value obj) { mrb_float x = cosh(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.erf(x) ⇒ Float
Calculates the error function of x.
645 646 647 648 649 650 |
# File 'mrbgems/mruby-math/src/math.c', line 645 static mrb_value math_erf(mrb_state *mrb, mrb_value obj) { mrb_float x = erf(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.erfc(x) ⇒ Float
Calculates the complementary error function of x.
659 660 661 662 663 664 |
# File 'mrbgems/mruby-math/src/math.c', line 659 static mrb_value math_erfc(mrb_state *mrb, mrb_value obj) { mrb_float x = erfc(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.exp(x) ⇒ Float
Returns e**x.
Math.exp(0) #=> 1.0 Math.exp(1) #=> 2.718281828459045 Math.exp(1.5) #=> 4.4816890703380645
425 426 427 428 429 430 |
# File 'mrbgems/mruby-math/src/math.c', line 425 static mrb_value math_exp(mrb_state *mrb, mrb_value obj) { mrb_float x = exp(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.frexp(numeric) ⇒ Array
Returns a two-element array containing the normalized fraction (a Float) and exponent (a Integer) of numeric.
fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11] fraction * 2**exponent #=> 1234.0
587 588 589 590 591 592 593 594 595 596 |
# File 'mrbgems/mruby-math/src/math.c', line 587 static mrb_value math_frexp(mrb_state *mrb, mrb_value obj) { mrb_float x = get_float_arg(mrb); int exp; x = frexp(x, &exp); return mrb_assoc_new(mrb, mrb_float_value(mrb, x), mrb_fixnum_value(exp)); } |
.hypot(x, y) ⇒ Float
Returns sqrt(x2 + y2), the hypotenuse of a right-angled triangle with sides x and y.
Math.hypot(3, 4) #=> 5.0
628 629 630 631 632 633 634 635 636 637 |
# File 'mrbgems/mruby-math/src/math.c', line 628 static mrb_value math_hypot(mrb_state *mrb, mrb_value obj) { mrb_float x, y; mrb_get_args(mrb, "ff", &x, &y); x = hypot(x, y); return mrb_float_value(mrb, x); } |
.ldexp(flt, int) ⇒ Float
Returns the value of flt(2*int).
fraction, exponent = Math.frexp(1234) Math.ldexp(fraction, exponent) #=> 1234.0
607 608 609 610 611 612 613 614 615 616 617 |
# File 'mrbgems/mruby-math/src/math.c', line 607 static mrb_value math_ldexp(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_int i; mrb_get_args(mrb, "fi", &x, &i); x = ldexp(x, (int)i); return mrb_float_value(mrb, x); } |
.log(numeric) ⇒ Float .log(num, base) ⇒ Float
Returns the natural logarithm of numeric. If additional second argument is given, it will be the base of logarithm.
Math.log(1) #=> 0.0 Math.log(Math::E) #=> 1.0 Math.log(Math::E**3) #=> 3.0 Math.log(12,3) #=> 2.2618595071429146
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'mrbgems/mruby-math/src/math.c', line 447 static mrb_value math_log(mrb_state *mrb, mrb_value obj) { mrb_float x, base; mrb_int argc; argc = mrb_get_args(mrb, "f|f", &x, &base); if (x < 0.0) { domain_error(mrb, "log"); } x = log(x); if (argc == 2) { if (base < 0.0) { domain_error(mrb, "log"); } x /= log(base); } return mrb_float_value(mrb, x); } |
.log10(numeric) ⇒ Float
Returns the base 10 logarithm of numeric.
Math.log10(1) #=> 0.0 Math.log10(10) #=> 1.0 Math.log10(10**100) #=> 100.0
503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'mrbgems/mruby-math/src/math.c', line 503 static mrb_value math_log10(mrb_state *mrb, mrb_value obj) { mrb_float x = get_float_arg(mrb); if (x < 0.0) { domain_error(mrb, "log10"); } x = log10(x); return mrb_float_value(mrb, x); } |
.log2(numeric) ⇒ Float
Returns the base 2 logarithm of numeric.
Math.log2(1) #=> 0.0 Math.log2(2) #=> 1.0 Math.log2(32768) #=> 15.0 Math.log2(65536) #=> 16.0
479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'mrbgems/mruby-math/src/math.c', line 479 static mrb_value math_log2(mrb_state *mrb, mrb_value obj) { mrb_float x = get_float_arg(mrb); if (x < 0.0) { domain_error(mrb, "log2"); } x = log2(x); return mrb_float_value(mrb, x); } |
.sin(x) ⇒ Float
Computes the sine of x (expressed in radians). Returns -1..1.
190 191 192 193 194 195 |
# File 'mrbgems/mruby-math/src/math.c', line 190 static mrb_value math_sin(mrb_state *mrb, mrb_value obj) { mrb_float x = sin(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.sinh(x) ⇒ Float
Computes the hyperbolic sine of x (expressed in radians).
320 321 322 323 324 325 |
# File 'mrbgems/mruby-math/src/math.c', line 320 static mrb_value math_sinh(mrb_state *mrb, mrb_value obj) { mrb_float x = sinh(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.sqrt(numeric) ⇒ Float
Returns the square root of numeric.
523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'mrbgems/mruby-math/src/math.c', line 523 static mrb_value math_sqrt(mrb_state *mrb, mrb_value obj) { mrb_float x = get_float_arg(mrb); if (x < 0.0) { domain_error(mrb, "sqrt"); } x = sqrt(x); return mrb_float_value(mrb, x); } |
.tan(x) ⇒ Float
Returns the tangent of x (expressed in radians).
217 218 219 220 221 222 |
# File 'mrbgems/mruby-math/src/math.c', line 217 static mrb_value math_tan(mrb_state *mrb, mrb_value obj) { mrb_float x = tan(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |
.tanh ⇒ Float
Computes the hyperbolic tangent of x (expressed in radians).
347 348 349 350 351 352 |
# File 'mrbgems/mruby-math/src/math.c', line 347 static mrb_value math_tanh(mrb_state *mrb, mrb_value obj) { mrb_float x = tanh(get_float_arg(mrb)); return mrb_float_value(mrb, x); } |