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 (aFixnum) 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.
262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'mrbgems/mruby-math/src/math.c', line 262 static mrb_value math_acos(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); 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.
410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'mrbgems/mruby-math/src/math.c', line 410 static mrb_value math_acosh(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); 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.
242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'mrbgems/mruby-math/src/math.c', line 242 static mrb_value math_asin(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); 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.
392 393 394 395 396 397 398 399 400 401 402 |
# File 'mrbgems/mruby-math/src/math.c', line 392 static mrb_value math_asinh(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = asinh(x); return mrb_float_value(mrb, x); } |
.atan(x) ⇒ Float
Computes the arc tangent of x. Returns -(PI/2) .. (PI/2).
282 283 284 285 286 287 288 289 290 291 |
# File 'mrbgems/mruby-math/src/math.c', line 282 static mrb_value math_atan(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = atan(x); 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
312 313 314 315 316 317 318 319 320 321 |
# File 'mrbgems/mruby-math/src/math.c', line 312 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.
430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'mrbgems/mruby-math/src/math.c', line 430 static mrb_value math_atanh(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); 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]
609 610 611 612 613 614 615 616 617 618 |
# File 'mrbgems/mruby-math/src/math.c', line 609 static mrb_value math_cbrt(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = cbrt(x); return mrb_float_value(mrb, x); } |
.cos(x) ⇒ Float
Computes the cosine of x (expressed in radians). Returns -1..1.
203 204 205 206 207 208 209 210 211 212 |
# File 'mrbgems/mruby-math/src/math.c', line 203 static mrb_value math_cos(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = cos(x); return mrb_float_value(mrb, x); } |
.cosh(x) ⇒ Float
Computes the hyperbolic cosine of x (expressed in radians).
352 353 354 355 356 357 358 359 360 361 |
# File 'mrbgems/mruby-math/src/math.c', line 352 static mrb_value math_cosh(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = cosh(x); return mrb_float_value(mrb, x); } |
.erf(x) ⇒ Float
Calculates the error function of x.
691 692 693 694 695 696 697 698 699 700 |
# File 'mrbgems/mruby-math/src/math.c', line 691 static mrb_value math_erf(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = erf(x); return mrb_float_value(mrb, x); } |
.erfc(x) ⇒ Float
Calculates the complementary error function of x.
709 710 711 712 713 714 715 716 717 718 |
# File 'mrbgems/mruby-math/src/math.c', line 709 static mrb_value math_erfc(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = erfc(x); 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
459 460 461 462 463 464 465 466 467 468 |
# File 'mrbgems/mruby-math/src/math.c', line 459 static mrb_value math_exp(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = exp(x); return mrb_float_value(mrb, x); } |
.frexp(numeric) ⇒ Array
Returns a two-element array containing the normalized fraction (a
Float) and exponent (a Fixnum) of
numeric.
fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11] fraction * 2**exponent #=> 1234.0
632 633 634 635 636 637 638 639 640 641 642 |
# File 'mrbgems/mruby-math/src/math.c', line 632 static mrb_value math_frexp(mrb_state *mrb, mrb_value obj) { mrb_float x; int exp; mrb_get_args(mrb, "f", &x); 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
674 675 676 677 678 679 680 681 682 683 |
# File 'mrbgems/mruby-math/src/math.c', line 674 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
653 654 655 656 657 658 659 660 661 662 663 |
# File 'mrbgems/mruby-math/src/math.c', line 653 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
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'mrbgems/mruby-math/src/math.c', line 485 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
542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'mrbgems/mruby-math/src/math.c', line 542 static mrb_value math_log10(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); 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
517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'mrbgems/mruby-math/src/math.c', line 517 static mrb_value math_log2(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); 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.
185 186 187 188 189 190 191 192 193 194 |
# File 'mrbgems/mruby-math/src/math.c', line 185 static mrb_value math_sin(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = sin(x); return mrb_float_value(mrb, x); } |
.sinh(x) ⇒ Float
Computes the hyperbolic sine of x (expressed in radians).
335 336 337 338 339 340 341 342 343 344 |
# File 'mrbgems/mruby-math/src/math.c', line 335 static mrb_value math_sinh(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = sinh(x); return mrb_float_value(mrb, x); } |
.sqrt(numeric) ⇒ Float
Returns the square root of numeric.
563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'mrbgems/mruby-math/src/math.c', line 563 static mrb_value math_sqrt(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); 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).
220 221 222 223 224 225 226 227 228 229 |
# File 'mrbgems/mruby-math/src/math.c', line 220 static mrb_value math_tan(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = tan(x); return mrb_float_value(mrb, x); } |
.tanh ⇒ Float
Computes the hyperbolic tangent of x (expressed in radians).
370 371 372 373 374 375 376 377 378 379 |
# File 'mrbgems/mruby-math/src/math.c', line 370 static mrb_value math_tanh(mrb_state *mrb, mrb_value obj) { mrb_float x; mrb_get_args(mrb, "f", &x); x = tanh(x); return mrb_float_value(mrb, x); } |