Class: Float

Inherits:
Numeric show all
Defined in:
mruby/src/numeric.c,
mruby/mrblib/numeric.rb

Overview

15.2.9

Instance Method Summary collapse

Methods inherited from Numeric

#+@, #-@, #allbits?, #anybits?, #eql?, #negative?, #nobits?, #nonzero?, #positive?, #to_c, #to_r, #zero?

Methods included from Comparable

#between?, #clamp

Instance Method Details

#%(other) ⇒ Float #modulo(other) ⇒ Float

Return the modulo after division of flt by other.

6543.21.modulo(137)      #=> 104.21
6543.21.modulo(137.24)   #=> 92.9299999999996

Overloads:



534
535
536
537
538
539
540
541
542
# File 'mruby/src/numeric.c', line 534

static mrb_value
flo_mod(mrb_state *mrb, mrb_value x)
{
  mrb_value y = mrb_get_arg1(mrb);
  mrb_float mod;

  flodivmod(mrb, mrb_float(x), mrb_as_float(mrb, y), 0, &mod);
  return mrb_float_value(mrb, mod);
}

#&Object



1392
# File 'mruby/src/numeric.c', line 1392

static mrb_value flo_and(mrb_state *mrb, mrb_value x);

#*(other) ⇒ Float

Returns a new float which is the product of float and other.

Returns:



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'mruby/src/numeric.c', line 467

static mrb_value
flo_mul(mrb_state *mrb, mrb_value x)
{
  mrb_value y = mrb_get_arg1(mrb);
  mrb_float a = mrb_float(x);

  switch (mrb_type(y)) {
  case MRB_TT_FLOAT:
    return mrb_float_value(mrb, a * mrb_float(y));
#if defined(MRB_USE_COMPLEX)
  case MRB_TT_COMPLEX:
    return mrb_complex_mul(mrb, y, x);
#endif
  default:
    return mrb_float_value(mrb, a * mrb_as_float(mrb, y));
  }
}

#**Object

******************************************************************

<code>Float</code> objects represent inexact real numbers using
the native architecture's double-precision floating-point
representation.


285
286
287
288
289
290
291
# File 'mruby/src/numeric.c', line 285

static mrb_value
flo_pow(mrb_state *mrb, mrb_value x)
{
  mrb_value y = mrb_get_arg1(mrb);
  mrb_float d = pow(mrb_as_float(mrb, x), mrb_as_float(mrb, y));
  return mrb_float_value(mrb, d);
}

#+(other) ⇒ Float

Returns a new float which is the sum of float and other.

Returns:



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'mruby/src/numeric.c', line 413

static mrb_value
flo_add(mrb_state *mrb, mrb_value x)
{
  mrb_value y = mrb_get_arg1(mrb);
  mrb_float a = mrb_float(x);

  switch (mrb_type(y)) {
  case MRB_TT_FLOAT:
    return mrb_float_value(mrb, a + mrb_float(y));
#if defined(MRB_USE_COMPLEX)
  case MRB_TT_COMPLEX:
    return mrb_complex_add(mrb, y, x);
#endif
  default:
    return mrb_float_value(mrb, a + mrb_as_float(mrb, y));
  }
}

#-(other) ⇒ Float

Returns a new float which is the difference of float and other.

Returns:



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'mruby/src/numeric.c', line 440

static mrb_value
flo_sub(mrb_state *mrb, mrb_value x)
{
  mrb_value y = mrb_get_arg1(mrb);
  mrb_float a = mrb_float(x);

  switch (mrb_type(y)) {
  case MRB_TT_FLOAT:
    return mrb_float_value(mrb, a - mrb_float(y));
#if defined(MRB_USE_COMPLEX)
  case MRB_TT_COMPLEX:
    return mrb_complex_sub(mrb, mrb_complex_new(mrb, a, 0), y);
#endif
  default:
    return mrb_float_value(mrb, a - mrb_as_float(mrb, y));
  }
}

#/(num) ⇒ Float

Returns a new Float which is the result of dividing float by num.

Returns:



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'mruby/src/numeric.c', line 323

static mrb_value
flo_div(mrb_state *mrb, mrb_value x)
{
  mrb_value y = mrb_get_arg1(mrb);
  mrb_float a = mrb_float(x);

  switch(mrb_type(y)) {
#ifdef MRB_USE_COMPLEX
  case MRB_TT_COMPLEX:
    return mrb_complex_div(mrb, mrb_complex_new(mrb, a, 0), y);
#endif
  case MRB_TT_FLOAT:
    a = mrb_div_float(a, mrb_float(y));
    return mrb_float_value(mrb, a);
  default:
    a = mrb_div_float(a, mrb_as_float(mrb, y));
    return mrb_float_value(mrb, a);
  }
  return mrb_float_value(mrb, a);
}

#<Object

15.2.9.3.1



2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
# File 'mruby/src/numeric.c', line 2080

static mrb_value
num_lt(mrb_state *mrb, mrb_value self)
{
  mrb_value other = mrb_get_arg1(mrb);
  mrb_int n;

  n = cmpnum(mrb, self, other);
  if (n == -2) cmperr(mrb, self, other);
  if (n < 0) return mrb_true_value();
  return mrb_false_value();
}

#<<Object



741
742
743
744
745
746
747
748
# File 'mruby/src/numeric.c', line 741

static mrb_value
flo_lshift(mrb_state *mrb, mrb_value x)
{
  mrb_int width;

  mrb_get_args(mrb, "i", &width);
  return flo_shift(mrb, x, width);
}

#<=Object



2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
# File 'mruby/src/numeric.c', line 2092

static mrb_value
num_le(mrb_state *mrb, mrb_value self)
{
  mrb_value other = mrb_get_arg1(mrb);
  mrb_int n;

  n = cmpnum(mrb, self, other);
  if (n == -2) cmperr(mrb, self, other);
  if (n <= 0) return mrb_true_value();
  return mrb_false_value();
}

#<=>(other.f) ⇒ -1, ... #<-1

> => +1

Comparison---Returns -1, 0, or +1 depending on whether <i>int</i> is
less than, equal to, or greater than <i>numeric</i>. This is the
basis for the tests in <code>Comparable</code>. When the operands are
not comparable, it returns nil instead of raising an exception.

Overloads:

  • #<=>(other.f) ⇒ -1, ...

    Returns:

    • (-1, 0, +1, nil)
  • #<-1

    Returns:

    • (-1)


2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
# File 'mruby/src/numeric.c', line 2063

static mrb_value
num_cmp(mrb_state *mrb, mrb_value self)
{
  mrb_value other = mrb_get_arg1(mrb);
  mrb_int n;

  n = cmpnum(mrb, self, other);
  if (n == -2) return mrb_nil_value();
  return mrb_fixnum_value(n);
}

#==(obj) ⇒ Boolean

Returns true only if obj has the same value as flt. Contrast this with Float#eql?, which requires obj to be a Float.

1.0 == 1   #=> true

Returns:

  • (Boolean)


594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'mruby/src/numeric.c', line 594

static mrb_value
flo_eq(mrb_state *mrb, mrb_value x)
{
  mrb_value y = mrb_get_arg1(mrb);

  switch (mrb_type(y)) {
  case MRB_TT_INTEGER:
    return mrb_bool_value(mrb_float(x) == (mrb_float)mrb_integer(y));
  case MRB_TT_FLOAT:
    return mrb_bool_value(mrb_float(x) == mrb_float(y));
#ifdef MRB_USE_RATIONAL
  case MRB_TT_RATIONAL:
    return mrb_bool_value(mrb_float(x) == mrb_as_float(mrb, y));
#endif
#ifdef MRB_USE_COMPLEX
  case MRB_TT_COMPLEX:
    return mrb_bool_value(mrb_equal(mrb, y, x));
#endif
  default:
    return mrb_false_value();
  }
}

#>Object



2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
# File 'mruby/src/numeric.c', line 2104

static mrb_value
num_gt(mrb_state *mrb, mrb_value self)
{
  mrb_value other = mrb_get_arg1(mrb);
  mrb_int n;

  n = cmpnum(mrb, self, other);
  if (n == -2) cmperr(mrb, self, other);
  if (n > 0) return mrb_true_value();
  return mrb_false_value();
}

#>=Object



2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
# File 'mruby/src/numeric.c', line 2116

static mrb_value
num_ge(mrb_state *mrb, mrb_value self)
{
  mrb_value other = mrb_get_arg1(mrb);
  mrb_int n;

  n = cmpnum(mrb, self, other);
  if (n == -2) cmperr(mrb, self, other);
  if (n >= 0) return mrb_true_value();
  return mrb_false_value();
}

#>>Object



731
732
733
734
735
736
737
738
739
# File 'mruby/src/numeric.c', line 731

static mrb_value
flo_rshift(mrb_state *mrb, mrb_value x)
{
  mrb_int width;

  mrb_get_args(mrb, "i", &width);
  if (width == MRB_INT_MIN) return flo_shift(mrb, x, -MRB_INT_BIT);
  return flo_shift(mrb, x, -width);
}

#^Object



1394
# File 'mruby/src/numeric.c', line 1394

static mrb_value flo_xor(mrb_state *mrb, mrb_value x);

#absObject

15.2.7.4.3



1094
1095
1096
1097
1098
1099
1100
1101
# File 'mruby/src/numeric.c', line 1094

static mrb_value
flo_abs(mrb_state *mrb, mrb_value num)
{
  mrb_float f = mrb_float(num);

  if (signbit(f)) return mrb_float_value(mrb, -f);
  return num;
}

#ceil([ndigits]) ⇒ Integer, Float

Returns the smallest number greater than or equal to float with a precision of ndigits decimal digits (default: 0).

When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros.

Returns a floating-point number when ndigits is positive, otherwise returns an integer.

1.2.ceil      #=> 2
2.0.ceil      #=> 2
(-1.2).ceil   #=> -1
(-2.0).ceil   #=> -2

1.234567.ceil(2)   #=> 1.24
1.234567.ceil(3)   #=> 1.235
1.234567.ceil(4)   #=> 1.2346
1.234567.ceil(5)   #=> 1.23457

34567.89.ceil(-5)  #=> 100000
34567.89.ceil(-4)  #=> 40000
34567.89.ceil(-3)  #=> 35000
34567.89.ceil(-2)  #=> 34600
34567.89.ceil(-1)  #=> 34570
34567.89.ceil(0)   #=> 34568
34567.89.ceil(1)   #=> 34567.9
34567.89.ceil(2)   #=> 34567.89
34567.89.ceil(3)   #=> 34567.89

Note that the limited precision of floating-point arithmetic might lead to surprising results:

(2.1 / 0.7).ceil  #=> 4 (!)

Returns:



961
962
963
964
965
# File 'mruby/src/numeric.c', line 961

static mrb_value
flo_ceil(mrb_state *mrb, mrb_value num)
{
  return flo_rounding(mrb, num, ceil);
}

#divObject

15.2.7.4.5(x)



293
294
295
296
297
298
299
300
# File 'mruby/src/numeric.c', line 293

static mrb_value
flo_idiv(mrb_state *mrb, mrb_value xv)
{
  mrb_int y;

  mrb_get_args(mrb, "i", &y);
  return mrb_div_int_value(mrb, (mrb_int)mrb_float(xv), y);
}

#divmodObject

15.2.9.3.15



1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
# File 'mruby/src/numeric.c', line 1305

static mrb_value
flo_divmod(mrb_state *mrb, mrb_value x)
{
  mrb_value y = mrb_get_arg1(mrb);
  mrb_float div, mod;
  mrb_value a, b;

  flodivmod(mrb, mrb_float(x), mrb_as_float(mrb, y), &div, &mod);
  if (!FIXABLE_FLOAT(div))
    a = mrb_float_value(mrb, div);
  else
    a = mrb_int_value(mrb, (mrb_int)div);
  b = mrb_float_value(mrb, mod);
  return mrb_assoc_new(mrb, a, b);
}

#finite?Boolean

Returns true if flt is a valid IEEE floating point number (it is not infinite, and nan? is false).

Returns:

  • (Boolean)


799
800
801
802
803
# File 'mruby/src/numeric.c', line 799

static mrb_value
flo_finite_p(mrb_state *mrb, mrb_value num)
{
  return mrb_bool_value(isfinite(mrb_float(num)));
}

#floor([ndigits]) ⇒ Integer, Float

Returns the largest number less than or equal to float with a precision of ndigits decimal digits (default: 0).

When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros.

Returns a floating-point number when ndigits is positive, otherwise returns an integer.

1.2.floor      #=> 1
2.0.floor      #=> 2
(-1.2).floor   #=> -2
(-2.0).floor   #=> -2

1.234567.floor(2)   #=> 1.23
1.234567.floor(3)   #=> 1.234
1.234567.floor(4)   #=> 1.2345
1.234567.floor(5)   #=> 1.23456

34567.89.floor(-5)  #=> 0
34567.89.floor(-4)  #=> 30000
34567.89.floor(-3)  #=> 34000
34567.89.floor(-2)  #=> 34500
34567.89.floor(-1)  #=> 34560
34567.89.floor(0)   #=> 34567
34567.89.floor(1)   #=> 34567.8
34567.89.floor(2)   #=> 34567.89
34567.89.floor(3)   #=> 34567.89

Note that the limited precision of floating-point arithmetic might lead to surprising results:

(0.3 / 0.1).floor  #=> 2 (!)

Returns:



915
916
917
918
919
# File 'mruby/src/numeric.c', line 915

static mrb_value
flo_floor(mrb_state *mrb, mrb_value num)
{
  return flo_rounding(mrb, num, floor);
}

#hashObject

15.2.7.4.3



2164
2165
2166
2167
2168
2169
2170
2171
# File 'mruby/src/numeric.c', line 2164

static mrb_value
flo_hash(mrb_state *mrb, mrb_value flo)
{
  mrb_float f = mrb_float(flo);
  /* normalize -0.0 to 0.0 */
  if (f == 0) f = 0.0;
  return mrb_int_value(mrb, (mrb_int)mrb_byte_hash((uint8_t*)&f, sizeof(f)));
}

#infinite?nil, ...

Returns nil, -1, or 1 depending on whether flt is finite, -infinity, or infinity.

(0.0).infinite?        #=> nil
(-1.0/0.0).infinite?   #=> -1
(+1.0/0.0).infinite?   #=> 1

Returns:

  • (nil, -1, +1)


777
778
779
780
781
782
783
784
785
786
# File 'mruby/src/numeric.c', line 777

static mrb_value
flo_infinite_p(mrb_state *mrb, mrb_value num)
{
  mrb_float value = mrb_float(num);

  if (isinf(value)) {
    return mrb_fixnum_value(value < 0 ? -1 : 1);
  }
  return mrb_nil_value();
}

#to_sString #inspectString

Returns a string containing a representation of self. As well as a fixed or exponential form of the number, the call may return “NaN”, “Infinity”, and “-Infinity”.

3.0.to_s   #=> 3.0
3.25.to_s  #=> 3.25

Overloads:



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'mruby/src/numeric.c', line 384

static mrb_value
flo_to_s(mrb_state *mrb, mrb_value flt)
{
  mrb_float f = mrb_float(flt);
  mrb_value str;

  if (isinf(f)) {
    str = f < 0 ? mrb_str_new_lit(mrb, "-Infinity")
                : mrb_str_new_lit(mrb, "Infinity");
  }
  else if (isnan(f)) {
    str = mrb_str_new_lit(mrb, "NaN");
  }
  else {
    str = mrb_float_to_str(mrb, flt, NULL);
  }

  RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
  return str;
}

#nan?Boolean

Returns:

  • (Boolean)


1088
1089
1090
1091
1092
# File 'mruby/src/numeric.c', line 1088

static mrb_value
flo_nan_p(mrb_state *mrb, mrb_value num)
{
  return mrb_bool_value(isnan(mrb_float(num)));
}

#/(num) ⇒ Float

Returns a new Float which is the result of dividing float by num.

Returns:



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'mruby/src/numeric.c', line 323

static mrb_value
flo_div(mrb_state *mrb, mrb_value x)
{
  mrb_value y = mrb_get_arg1(mrb);
  mrb_float a = mrb_float(x);

  switch(mrb_type(y)) {
#ifdef MRB_USE_COMPLEX
  case MRB_TT_COMPLEX:
    return mrb_complex_div(mrb, mrb_complex_new(mrb, a, 0), y);
#endif
  case MRB_TT_FLOAT:
    a = mrb_div_float(a, mrb_float(y));
    return mrb_float_value(mrb, a);
  default:
    a = mrb_div_float(a, mrb_as_float(mrb, y));
    return mrb_float_value(mrb, a);
  }
  return mrb_float_value(mrb, a);
}

#round([ndigits]) ⇒ Integer, Float

Rounds flt to a given precision in decimal digits (default 0 digits). Precision may be negative. Returns a floating-point number when ndigits is more than zero.

1.4.round      #=> 1
1.5.round      #=> 2
1.6.round      #=> 2
(-1.5).round   #=> -2

1.234567.round(2)  #=> 1.23
1.234567.round(3)  #=> 1.235
1.234567.round(4)  #=> 1.2346
1.234567.round(5)  #=> 1.23457

34567.89.round(-5) #=> 0
34567.89.round(-4) #=> 30000
34567.89.round(-3) #=> 35000
34567.89.round(-2) #=> 34600
34567.89.round(-1) #=> 34570
34567.89.round(0)  #=> 34568
34567.89.round(1)  #=> 34567.9
34567.89.round(2)  #=> 34567.89
34567.89.round(3)  #=> 34567.89

Returns:



998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
# File 'mruby/src/numeric.c', line 998

static mrb_value
flo_round(mrb_state *mrb, mrb_value num)
{
  double number, f;
  mrb_int ndigits = 0;
  mrb_int i;

  mrb_get_args(mrb, "|i", &ndigits);
  number = mrb_float(num);

  if (0 < ndigits && (isinf(number) || isnan(number))) {
    return num;
  }
  mrb_check_num_exact(mrb, number);

  f = 1.0;
  if (ndigits < -DBL_DIG-2) return mrb_fixnum_value(0);
  i = ndigits >= 0 ? ndigits : -ndigits;
  if (ndigits > DBL_DIG+2) return num;
  while  (--i >= 0)
    f = f*10.0;

  if (isinf(f)) {
    if (ndigits < 0) number = 0;
  }
  else {
    double d;

    if (ndigits < 0) number /= f;
    else number *= f;

    /* home-made inline implementation of round(3) */
    if (number > 0.0) {
      d = floor(number);
      number = d + (number - d >= 0.5);
    }
    else if (number < 0.0) {
      d = ceil(number);
      number = d - (d - number >= 0.5);
    }

    if (ndigits < 0) number *= f;
    else number /= f;
  }

  if (ndigits > 0) {
    if (!isfinite(number)) return num;
    return mrb_float_value(mrb, number);
  }
  if (!FIXABLE_FLOAT(number))
    return mrb_float_value(mrb, number);
  return mrb_int_value(mrb, (mrb_int)number);
}

#step(num = nil, step = 1, &block) ⇒ Object

Raises:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'mruby/mrblib/numeric.rb', line 135

def step(num=nil, step=1, &block)
  raise ArgumentError, "step can't be 0" if step == 0
  return to_enum(:step, num, step) unless block

  i = self
  if num == self || step.infinite?
    block.call(i) if step > 0 && i <= (num||i) || step < 0 && i >= (num||-i)
  elsif num == nil
    while true
      block.call(i)
      i += step
    end
  elsif step > 0
    while i <= num
      block.call(i)
      i += step
    end
  else
    while i >= num
      block.call(i)
      i += step
    end
  end
  self
end

#to_fself

As flt is already a float, returns self.

Returns:

  • (self)


758
759
760
761
762
# File 'mruby/src/numeric.c', line 758

static mrb_value
flo_to_f(mrb_state *mrb, mrb_value num)
{
  return num;
}

#to_iObject

15.2.9.3.14



1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'mruby/src/numeric.c', line 1053

static mrb_value
flo_to_i(mrb_state *mrb, mrb_value num)
{
  mrb_float f = mrb_float(num);

  mrb_check_num_exact(mrb, f);
  if (!FIXABLE_FLOAT(f)) {
#ifdef MRB_USE_BIGINT
    return mrb_bint_new_float(mrb, f);
#else
    mrb_int_overflow(mrb, "to_f");
#endif
  }
  if (f > 0.0) f = floor(f);
  if (f < 0.0) f = ceil(f);

  return mrb_int_value(mrb, (mrb_int)f);
}

#to_sString #inspectString

Returns a string containing a representation of self. As well as a fixed or exponential form of the number, the call may return “NaN”, “Infinity”, and “-Infinity”.

3.0.to_s   #=> 3.0
3.25.to_s  #=> 3.25

Overloads:



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'mruby/src/numeric.c', line 384

static mrb_value
flo_to_s(mrb_state *mrb, mrb_value flt)
{
  mrb_float f = mrb_float(flt);
  mrb_value str;

  if (isinf(f)) {
    str = f < 0 ? mrb_str_new_lit(mrb, "-Infinity")
                : mrb_str_new_lit(mrb, "Infinity");
  }
  else if (isnan(f)) {
    str = mrb_str_new_lit(mrb, "NaN");
  }
  else {
    str = mrb_float_to_str(mrb, flt, NULL);
  }

  RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
  return str;
}

#to_iInteger #truncateInteger

Returns flt truncated to an Integer.

Overloads:



1081
1082
1083
1084
1085
1086
# File 'mruby/src/numeric.c', line 1081

static mrb_value
flo_truncate(mrb_state *mrb, mrb_value num)
{
  if (signbit(mrb_float(num))) return flo_ceil(mrb, num);
  return flo_floor(mrb, num);
}

#|Object



1393
# File 'mruby/src/numeric.c', line 1393

static mrb_value flo_or(mrb_state *mrb, mrb_value x);

#~Object

15.2.9.3.2



647
648
649
650
651
652
# File 'mruby/src/numeric.c', line 647

static mrb_value
flo_rev(mrb_state *mrb, mrb_value x)
{
  int64_t v1 = value_int64(mrb, x);
  return int64_value(mrb, ~v1);
}