Module: Math

Defined in:
mruby/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:



265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'mruby/mrbgems/mruby-math/src/math.c', line 265

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:



411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'mruby/mrbgems/mruby-math/src/math.c', line 411

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



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'mruby/mrbgems/mruby-math/src/math.c', line 245

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:



393
394
395
396
397
398
399
400
401
402
403
# File 'mruby/mrbgems/mruby-math/src/math.c', line 393

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:



285
286
287
288
289
290
291
292
293
294
# File 'mruby/mrbgems/mruby-math/src/math.c', line 285

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:



315
316
317
318
319
320
321
322
323
324
# File 'mruby/mrbgems/mruby-math/src/math.c', line 315

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:



431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'mruby/mrbgems/mruby-math/src/math.c', line 431

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:



610
611
612
613
614
615
616
617
618
619
# File 'mruby/mrbgems/mruby-math/src/math.c', line 610

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:



206
207
208
209
210
211
212
213
214
215
# File 'mruby/mrbgems/mruby-math/src/math.c', line 206

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:



353
354
355
356
357
358
359
360
361
362
# File 'mruby/mrbgems/mruby-math/src/math.c', line 353

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:



692
693
694
695
696
697
698
699
700
701
# File 'mruby/mrbgems/mruby-math/src/math.c', line 692

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:



710
711
712
713
714
715
716
717
718
719
# File 'mruby/mrbgems/mruby-math/src/math.c', line 710

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:



460
461
462
463
464
465
466
467
468
469
# File 'mruby/mrbgems/mruby-math/src/math.c', line 460

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 Integer) of numeric.

fraction, exponent = Math.frexp(1234)   #=> [0.6025390625, 11]
fraction * 2**exponent                  #=> 1234.0

Returns:



633
634
635
636
637
638
639
640
641
642
643
# File 'mruby/mrbgems/mruby-math/src/math.c', line 633

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(x**2 + y**2), the hypotenuse of a right-angled triangle with sides x and y.

Math.hypot(3, 4)   #=> 5.0

Returns:



675
676
677
678
679
680
681
682
683
684
# File 'mruby/mrbgems/mruby-math/src/math.c', line 675

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:



654
655
656
657
658
659
660
661
662
663
664
# File 'mruby/mrbgems/mruby-math/src/math.c', line 654

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:



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'mruby/mrbgems/mruby-math/src/math.c', line 486

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:



543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'mruby/mrbgems/mruby-math/src/math.c', line 543

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:



518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'mruby/mrbgems/mruby-math/src/math.c', line 518

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:



188
189
190
191
192
193
194
195
196
197
# File 'mruby/mrbgems/mruby-math/src/math.c', line 188

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:



336
337
338
339
340
341
342
343
344
345
# File 'mruby/mrbgems/mruby-math/src/math.c', line 336

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:



564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'mruby/mrbgems/mruby-math/src/math.c', line 564

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:



223
224
225
226
227
228
229
230
231
232
# File 'mruby/mrbgems/mruby-math/src/math.c', line 223

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:



371
372
373
374
375
376
377
378
379
380
# File 'mruby/mrbgems/mruby-math/src/math.c', line 371

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