Module: Math

Defined in:
mrbgems/mruby-math/src/math.c

Class Method Summary collapse

Class Method Details

.acos(x) ⇒ Float

Computes the arc cosine of x. Returns 0..PI.

Returns:



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'mrbgems/mruby-math/src/math.c', line 268

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.

Returns:



416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'mrbgems/mruby-math/src/math.c', line 416

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.

Returns:

Returns:

  • computed value between -(PI/2) and (PI/2).



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'mrbgems/mruby-math/src/math.c', line 248

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.

Returns:



398
399
400
401
402
403
404
405
406
407
408
# File 'mrbgems/mruby-math/src/math.c', line 398

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).

Returns:



288
289
290
291
292
293
294
295
296
297
# File 'mrbgems/mruby-math/src/math.c', line 288

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

Returns:



318
319
320
321
322
323
324
325
326
327
# File 'mrbgems/mruby-math/src/math.c', line 318

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.

Returns:



436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'mrbgems/mruby-math/src/math.c', line 436

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]

Returns:



615
616
617
618
619
620
621
622
623
624
# File 'mrbgems/mruby-math/src/math.c', line 615

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.

Returns:



209
210
211
212
213
214
215
216
217
218
# File 'mrbgems/mruby-math/src/math.c', line 209

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).

Returns:



358
359
360
361
362
363
364
365
366
367
# File 'mrbgems/mruby-math/src/math.c', line 358

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.

Returns:



697
698
699
700
701
702
703
704
705
706
# File 'mrbgems/mruby-math/src/math.c', line 697

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.

Returns:



715
716
717
718
719
720
721
722
723
724
# File 'mrbgems/mruby-math/src/math.c', line 715

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

Returns:



465
466
467
468
469
470
471
472
473
474
# File 'mrbgems/mruby-math/src/math.c', line 465

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

Returns:



638
639
640
641
642
643
644
645
646
647
648
# File 'mrbgems/mruby-math/src/math.c', line 638

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

Returns:



680
681
682
683
684
685
686
687
688
689
# File 'mrbgems/mruby-math/src/math.c', line 680

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

Returns:



659
660
661
662
663
664
665
666
667
668
669
# File 'mrbgems/mruby-math/src/math.c', line 659

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

Overloads:



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'mrbgems/mruby-math/src/math.c', line 491

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

Returns:



548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'mrbgems/mruby-math/src/math.c', line 548

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

Returns:



523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'mrbgems/mruby-math/src/math.c', line 523

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.

Returns:



191
192
193
194
195
196
197
198
199
200
# File 'mrbgems/mruby-math/src/math.c', line 191

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).

Returns:



341
342
343
344
345
346
347
348
349
350
# File 'mrbgems/mruby-math/src/math.c', line 341

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.

Returns:



569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'mrbgems/mruby-math/src/math.c', line 569

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).

Returns:



226
227
228
229
230
231
232
233
234
235
# File 'mrbgems/mruby-math/src/math.c', line 226

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);
}

.tanhFloat

Computes the hyperbolic tangent of x (expressed in radians).

Returns:



376
377
378
379
380
381
382
383
384
385
# File 'mrbgems/mruby-math/src/math.c', line 376

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);
}