Class: Float
Overview
15.2.9
Instance Method Summary collapse
-
#% ⇒ Object
Return the modulo after division of
flt
byother
. -
#& ⇒ Object
-
#*(other) ⇒ Float
Returns a new float which is the product of
float
andother
. -
#+ ⇒ Object
15.2.9.3.1.
-
#-(other) ⇒ Float
Returns a new float which is the difference of
float
andother
. -
#<< ⇒ Object
-
#==(obj) ⇒ Boolean
Returns
true
only if obj has the same value as flt. -
#>> ⇒ Object
-
#^ ⇒ Object
-
#ceil ⇒ Integer
Returns the smallest
Integer
greater than or equal to flt. -
#divmod ⇒ Object
15.2.9.3.15.
-
#eql? ⇒ Boolean
15.2.8.3.16.
-
#finite? ⇒ Boolean
Returns
true
if 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
flt
is 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 inherited from Numeric
#+@, #-@, #abs, #negative?, #nonzero?, #positive?, #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
343 344 345 346 347 348 349 350 351 352 353 |
# File 'src/numeric.c', line 343 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
1037 |
# File 'src/numeric.c', line 1037
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
.
281 282 283 284 285 286 287 288 |
# File 'src/numeric.c', line 281 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
1597 1598 1599 1600 1601 1602 1603 1604 |
# File 'src/numeric.c', line 1597 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
.
263 264 265 266 267 268 269 270 |
# File 'src/numeric.c', line 263 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
535 536 537 538 539 540 541 542 |
# File 'src/numeric.c', line 535 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); } |
#==(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
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'src/numeric.c', line 402 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
526 527 528 529 530 531 532 533 |
# File 'src/numeric.c', line 526 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); } |
#^ ⇒ Object
1039 |
# File 'src/numeric.c', line 1039
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
646 647 648 649 650 651 652 653 |
# File 'src/numeric.c', line 646 static mrb_value flo_ceil(mrb_state *mrb, mrb_value num) { mrb_float f = ceil(mrb_float(num)); mrb_check_num_exact(mrb, f); return mrb_int_value(mrb, f); } |
#divmod ⇒ Object
15.2.9.3.15
967 968 969 970 971 972 973 974 975 976 977 978 979 980 |
# File 'src/numeric.c', line 967 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_int_value(mrb, div); b = mrb_float_value(mrb, mod); return mrb_assoc_new(mrb, a, b); } |
#eql? ⇒ Boolean
15.2.8.3.16
379 380 381 382 383 384 385 386 387 |
# File 'src/numeric.c', line 379 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(y)); } |
#finite? ⇒ Boolean
Returns true
if flt is a valid IEEE floating
point number (it is not infinite, and nan?
is
false
).
593 594 595 596 597 |
# File 'src/numeric.c', line 593 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
623 624 625 626 627 628 629 630 |
# File 'src/numeric.c', line 623 static mrb_value flo_floor(mrb_state *mrb, mrb_value num) { mrb_float f = floor(mrb_float(num)); mrb_check_num_exact(mrb, f); return mrb_int_value(mrb, 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
571 572 573 574 575 576 577 578 579 580 |
# File 'src/numeric.c', line 571 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
”.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'src/numeric.c', line 209 static mrb_value flo_to_s(mrb_state *mrb, mrb_value flt) { mrb_float f = mrb_float(flt); if (isinf(f)) { return f < 0 ? mrb_str_new_lit(mrb, "-Infinity") : mrb_str_new_lit(mrb, "Infinity"); } else if (isnan(f)) { return mrb_str_new_lit(mrb, "NaN"); } else { char fmt[] = "%." MRB_STRINGIZE(FLO_TO_STR_PREC) "g"; mrb_value str = mrb_float_to_str(mrb, flt, fmt); mrb_int len; char *begp, *p, *endp; insert_dot_zero: begp = RSTRING_PTR(str); len = RSTRING_LEN(str); for (p = begp, endp = p + len; p < endp; ++p) { if (*p == '.') { return str; } else if (*p == 'e') { ptrdiff_t e_pos = p - begp; mrb_str_cat(mrb, str, ".0", 2); p = RSTRING_PTR(str) + e_pos; memmove(p + 2, p, len - e_pos); memcpy(p, ".0", 2); return str; } } if (FLO_TO_STR_PREC + (begp[0] == '-') <= len) { --fmt[sizeof(fmt) - 3]; /* %.16g(%.8g) -> %.15g(%.7g) */ str = mrb_float_to_str(mrb, flt, fmt); goto insert_dot_zero; } return str; } } |
#nan? ⇒ Boolean
759 760 761 762 763 |
# File 'src/numeric.c', line 759 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
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 |
# File 'src/numeric.c', line 686 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; 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); } return mrb_int_value(mrb, number); } |
#to_f ⇒ self
As flt
is already a float, returns +self+.
552 553 554 555 556 |
# File 'src/numeric.c', line 552 static mrb_value flo_to_f(mrb_state *mrb, mrb_value num) { return num; } |
#to_i ⇒ Integer #truncate ⇒ Integer
Returns flt truncated to an Integer
.
747 748 749 750 751 752 753 754 755 756 757 |
# File 'src/numeric.c', line 747 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); return mrb_int_value(mrb, f); } |
#to_i ⇒ Integer #truncate ⇒ Integer
Returns flt truncated to an Integer
.
747 748 749 750 751 752 753 754 755 756 757 |
# File 'src/numeric.c', line 747 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); return mrb_int_value(mrb, 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
”.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'src/numeric.c', line 209 static mrb_value flo_to_s(mrb_state *mrb, mrb_value flt) { mrb_float f = mrb_float(flt); if (isinf(f)) { return f < 0 ? mrb_str_new_lit(mrb, "-Infinity") : mrb_str_new_lit(mrb, "Infinity"); } else if (isnan(f)) { return mrb_str_new_lit(mrb, "NaN"); } else { char fmt[] = "%." MRB_STRINGIZE(FLO_TO_STR_PREC) "g"; mrb_value str = mrb_float_to_str(mrb, flt, fmt); mrb_int len; char *begp, *p, *endp; insert_dot_zero: begp = RSTRING_PTR(str); len = RSTRING_LEN(str); for (p = begp, endp = p + len; p < endp; ++p) { if (*p == '.') { return str; } else if (*p == 'e') { ptrdiff_t e_pos = p - begp; mrb_str_cat(mrb, str, ".0", 2); p = RSTRING_PTR(str) + e_pos; memmove(p + 2, p, len - e_pos); memcpy(p, ".0", 2); return str; } } if (FLO_TO_STR_PREC + (begp[0] == '-') <= len) { --fmt[sizeof(fmt) - 3]; /* %.16g(%.8g) -> %.15g(%.7g) */ str = mrb_float_to_str(mrb, flt, fmt); goto insert_dot_zero; } return str; } } |
#to_i ⇒ Integer #truncate ⇒ Integer
Returns flt truncated to an Integer
.
747 748 749 750 751 752 753 754 755 756 757 |
# File 'src/numeric.c', line 747 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); return mrb_int_value(mrb, f); } |
#| ⇒ Object
1038 |
# File 'src/numeric.c', line 1038
static mrb_value flo_or(mrb_state *mrb, mrb_value x);
|
#~ ⇒ Object
15.2.9.3.7
444 445 446 447 448 449 450 |
# File 'src/numeric.c', line 444 static mrb_value flo_rev(mrb_state *mrb, mrb_value x) { int64_t v1; v1 = (int64_t)mrb_float(x); return int64_value(mrb, ~v1); } |