Class: Float
- Includes:
- Integral
- Defined in:
- mrblib/float.rb,
src/numeric.c
Overview
Float
ISO 15.2.9
Instance Method Summary collapse
-
#% ⇒ Object
Return the modulo after division of
fltbyother. -
#& ⇒ Object
-
#*(other) ⇒ Float
Returns a new float which is the product of
floatandother. -
#+ ⇒ Object
15.2.9.3.1.
-
#-(other) ⇒ Float
Returns a new float which is the difference of
floatandother. -
#<< ⇒ Object
-
#==(obj) ⇒ Boolean
Returns
trueonly if obj has the same value as flt. -
#>> ⇒ Object
-
#^ ⇒ Object
-
#ceil ⇒ Integer
Returns the smallest
Integergreater than or equal to flt. -
#divmod ⇒ Object
15.2.9.3.15.
-
#eql? ⇒ Boolean
15.2.8.3.16.
-
#finite? ⇒ Boolean
Returns
trueif flt is a valid IEEE floating point number (it is not infinite, andnan?isfalse). -
#floor ⇒ Integer
Returns the largest integer less than or equal to flt.
-
#infinite? ⇒ nil, ...
Returns
nil, -1, or +1 depending on whether flt is finite, -infinity, or +infinity. -
#to_s ⇒ String
Returns a string containing a representation of self.
-
#nan? ⇒ Boolean
-
#round([ndigits]) ⇒ Integer, Float
Rounds flt to a given precision in decimal digits (default 0 digits).
-
#to_f ⇒ self
As
fltis already a float, returns +self+. -
#to_i ⇒ Object
Returns flt truncated to an
Integer. -
#to_int ⇒ Object
Returns flt truncated to an
Integer. -
#to_s ⇒ String
Returns a string containing a representation of self.
-
#truncate ⇒ Object
Returns flt truncated to an
Integer. -
#| ⇒ Object
-
#~ ⇒ Object
15.2.9.3.7.
Methods included from Integral
#div, #downto, #negative?, #next, #nonzero?, #positive?, #step, #times, #upto, #zero?
Methods inherited from Numeric
#**, #+@, #-@, #/, #<, #<=, #<=>, #>, #>=, #abs, #quo
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
262 263 264 265 266 267 268 269 270 271 272 |
# File 'src/numeric.c', line 262 static mrb_value flo_mod(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_float mod; mrb_get_args(mrb, "o", &y); flodivmod(mrb, mrb_float(x), mrb_to_flo(mrb, y), 0, &mod); return mrb_float_value(mrb, mod); } |
#& ⇒ Object
949 |
# File 'src/numeric.c', line 949
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.
213 214 215 216 217 218 219 220 |
# File 'src/numeric.c', line 213 static mrb_value flo_mul(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_get_args(mrb, "o", &y); return mrb_float_value(mrb, mrb_float(x) * mrb_to_flo(mrb, y)); } |
#+ ⇒ Object
15.2.9.3.1
1467 1468 1469 1470 1471 1472 1473 1474 |
# File 'src/numeric.c', line 1467 static mrb_value flo_plus(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_get_args(mrb, "o", &y); return mrb_float_value(mrb, mrb_float(x) + mrb_to_flo(mrb, y)); } |
#-(other) ⇒ Float
Returns a new float which is the difference of float
and other.
195 196 197 198 199 200 201 202 |
# File 'src/numeric.c', line 195 static mrb_value flo_minus(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_get_args(mrb, "o", &y); return mrb_float_value(mrb, mrb_float(x) - mrb_to_flo(mrb, y)); } |
#<< ⇒ Object
454 455 456 457 458 459 460 461 |
# File 'src/numeric.c', line 454 static mrb_value flo_rshift(mrb_state *mrb, mrb_value x) { mrb_int width; mrb_get_args(mrb, "i", &width); return flo_shift(mrb, x, width); } |
#==(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
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'src/numeric.c', line 321 static mrb_value flo_eq(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_get_args(mrb, "o", &y); switch (mrb_type(y)) { case MRB_TT_FIXNUM: return mrb_bool_value(mrb_float(x) == (mrb_float)mrb_fixnum(y)); case MRB_TT_FLOAT: return mrb_bool_value(mrb_float(x) == mrb_float(y)); default: return mrb_false_value(); } } |
#>> ⇒ Object
445 446 447 448 449 450 451 452 |
# File 'src/numeric.c', line 445 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
951 |
# File 'src/numeric.c', line 951
static mrb_value flo_xor(mrb_state *mrb, mrb_value x);
|
#ceil ⇒ Integer
Returns the smallest Integer greater than or equal to
flt.
1.2.ceil #=> 2 2.0.ceil #=> 2 (-1.2).ceil #=> -1 (-2.0).ceil #=> -2
568 569 570 571 572 573 574 575 576 577 578 |
# File 'src/numeric.c', line 568 static mrb_value flo_ceil(mrb_state *mrb, mrb_value num) { mrb_float f = ceil(mrb_float(num)); mrb_check_num_exact(mrb, f); if (!FIXABLE_FLOAT(f)) { return mrb_float_value(mrb, f); } return mrb_fixnum_value((mrb_int)f); } |
#divmod ⇒ Object
15.2.9.3.15
879 880 881 882 883 884 885 886 887 888 889 890 891 892 |
# File 'src/numeric.c', line 879 static mrb_value flo_divmod(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_float div, mod; mrb_value a, b; mrb_get_args(mrb, "o", &y); flodivmod(mrb, mrb_float(x), mrb_to_flo(mrb, y), &div, &mod); a = mrb_float_value(mrb, div); b = mrb_float_value(mrb, mod); return mrb_assoc_new(mrb, a, b); } |
#eql? ⇒ Boolean
15.2.8.3.16
298 299 300 301 302 303 304 305 306 |
# File 'src/numeric.c', line 298 static mrb_value flo_eql(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_get_args(mrb, "o", &y); if (!mrb_float_p(y)) return mrb_false_value(); return mrb_bool_value(mrb_float(x) == (mrb_float)mrb_fixnum(y)); } |
#finite? ⇒ Boolean
Returns true if flt is a valid IEEE floating
point number (it is not infinite, and nan? is
false).
512 513 514 515 516 |
# File 'src/numeric.c', line 512 static mrb_value flo_finite_p(mrb_state *mrb, mrb_value num) { return mrb_bool_value(isfinite(mrb_float(num))); } |
#floor ⇒ Integer
Returns the largest integer less than or equal to flt.
1.2.floor #=> 1 2.0.floor #=> 2 (-1.2).floor #=> -2 (-2.0).floor #=> -2
542 543 544 545 546 547 548 549 550 551 552 |
# File 'src/numeric.c', line 542 static mrb_value flo_floor(mrb_state *mrb, mrb_value num) { mrb_float f = floor(mrb_float(num)); mrb_check_num_exact(mrb, f); if (!FIXABLE_FLOAT(f)) { return mrb_float_value(mrb, f); } return mrb_fixnum_value((mrb_int)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
490 491 492 493 494 495 496 497 498 499 |
# File 'src/numeric.c', line 490 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_s ⇒ String
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”.
177 178 179 180 181 182 183 184 |
# File 'src/numeric.c', line 177 static mrb_value flo_to_s(mrb_state *mrb, mrb_value flt) { if (isnan(mrb_float(flt))) { return mrb_str_new_lit(mrb, "NaN"); } return mrb_float_to_str(mrb, flt, MRB_FLO_TO_STR_FMT); } |
#nan? ⇒ Boolean
687 688 689 690 691 |
# File 'src/numeric.c', line 687 static mrb_value flo_nan_p(mrb_state *mrb, mrb_value num) { return mrb_bool_value(isnan(mrb_float(num))); } |
#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
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
# File 'src/numeric.c', line 611 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; i = ndigits >= 0 ? ndigits : -ndigits; 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); } return mrb_fixnum_value((mrb_int)number); } |
#to_f ⇒ self
As flt is already a float, returns +self+.
471 472 473 474 475 |
# File 'src/numeric.c', line 471 static mrb_value flo_to_f(mrb_state *mrb, mrb_value num) { return num; } |
#to_i ⇒ Integer #to_int ⇒ Integer #truncate ⇒ Integer
Returns flt truncated to an Integer.
672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'src/numeric.c', line 672 static mrb_value flo_truncate(mrb_state *mrb, mrb_value num) { mrb_float f = mrb_float(num); if (f > 0.0) f = floor(f); if (f < 0.0) f = ceil(f); mrb_check_num_exact(mrb, f); if (!FIXABLE_FLOAT(f)) { return mrb_float_value(mrb, f); } return mrb_fixnum_value((mrb_int)f); } |
#to_i ⇒ Integer #to_int ⇒ Integer #truncate ⇒ Integer
Returns flt truncated to an Integer.
672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'src/numeric.c', line 672 static mrb_value flo_truncate(mrb_state *mrb, mrb_value num) { mrb_float f = mrb_float(num); if (f > 0.0) f = floor(f); if (f < 0.0) f = ceil(f); mrb_check_num_exact(mrb, f); if (!FIXABLE_FLOAT(f)) { return mrb_float_value(mrb, f); } return mrb_fixnum_value((mrb_int)f); } |
#to_s ⇒ String
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”.
177 178 179 180 181 182 183 184 |
# File 'src/numeric.c', line 177 static mrb_value flo_to_s(mrb_state *mrb, mrb_value flt) { if (isnan(mrb_float(flt))) { return mrb_str_new_lit(mrb, "NaN"); } return mrb_float_to_str(mrb, flt, MRB_FLO_TO_STR_FMT); } |
#to_i ⇒ Integer #to_int ⇒ Integer #truncate ⇒ Integer
Returns flt truncated to an Integer.
672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'src/numeric.c', line 672 static mrb_value flo_truncate(mrb_state *mrb, mrb_value num) { mrb_float f = mrb_float(num); if (f > 0.0) f = floor(f); if (f < 0.0) f = ceil(f); mrb_check_num_exact(mrb, f); if (!FIXABLE_FLOAT(f)) { return mrb_float_value(mrb, f); } return mrb_fixnum_value((mrb_int)f); } |
#| ⇒ Object
950 |
# File 'src/numeric.c', line 950
static mrb_value flo_or(mrb_state *mrb, mrb_value x);
|
#~ ⇒ Object
15.2.9.3.7
363 364 365 366 367 368 369 370 |
# File 'src/numeric.c', line 363 static mrb_value flo_rev(mrb_state *mrb, mrb_value x) { int64_t v1; mrb_get_args(mrb, ""); v1 = (int64_t)mrb_float(x); return int64_value(mrb, ~v1); } |