BigDecimal extends the native Float class to provide the to_d method.
When you require BigDecimal in your application, this method will be available on Float objects.
When mathn is required, Float is changed to handle Complex numbers.
Float objects represent inexact real numbers using the native architecture's double-precision floating point representation.
Floating point has a different arithmetic and is an inexact number. So you should know its esoteric system. see following:
- #
- A
- C
- D
- E
- F
- H
- I
- M
- N
- P
- Q
- R
- T
- Z
| ROUNDS | = | INT2FIX(FLT_ROUNDS) |
Represents the rounding mode for floating point addition. Usually defaults to 1, rounding to the nearest number. Other modes include:
|
||
| RADIX | = | INT2FIX(FLT_RADIX) |
The base of the floating point, or number of unique digits used to represent the number. Usually defaults to 2 on most systems, which would represent a base-10 decimal. |
||
| MANT_DIG | = | INT2FIX(DBL_MANT_DIG) |
The number of base digits for the Usually defaults to 53. |
||
| DIG | = | INT2FIX(DBL_DIG) |
The number of decimal digits in a double-precision floating point. Usually defaults to 15. |
||
| MIN_EXP | = | INT2FIX(DBL_MIN_EXP) |
The smallest posable exponent value in a double-precision floating point. Usually defaults to -1021. |
||
| MAX_EXP | = | INT2FIX(DBL_MAX_EXP) |
The largest possible exponent value in a double-precision floating point. Usually defaults to 1024. |
||
| MIN_10_EXP | = | INT2FIX(DBL_MIN_10_EXP) |
The smallest negative exponent in a double-precision floating point where 10 raised to this power minus 1. Usually defaults to -307. |
||
| MAX_10_EXP | = | INT2FIX(DBL_MAX_10_EXP) |
The largest positive exponent in a double-precision floating point where 10 raised to this power minus 1. Usually defaults to 308. |
||
| MIN | = | DBL2NUM(DBL_MIN) |
The smallest positive integer in a double-precision floating point. Usually defaults to 2.2250738585072014e-308. |
||
| MAX | = | DBL2NUM(DBL_MAX) |
The largest possible integer in a double-precision floating point number. Usually defaults to 1.7976931348623157e+308. |
||
| EPSILON | = | DBL2NUM(DBL_EPSILON) |
The difference between 1 and the smallest double-precision floating point number. Usually defaults to 2.2204460492503131e-16. |
||
| INFINITY | = | DBL2NUM(INFINITY) |
An expression representing positive infinity. |
||
| NAN | = | DBL2NUM(NAN) |
An expression representing a value which is “not a number”. |
||
Return the modulo after division of float by
other.
6543.21.modulo(137) #=> 104.21
6543.21.modulo(137.24) #=> 92.9299999999996
Source: show
static VALUE
flo_mod(VALUE x, VALUE y)
{
double fy;
if (RB_TYPE_P(y, T_FIXNUM)) {
fy = (double)FIX2LONG(y);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
fy = rb_big2dbl(y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
fy = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, '%');
}
return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
}
Returns a new float which is the product of float and
other.
Source: show
static VALUE
flo_mul(VALUE x, VALUE y)
{
if (RB_TYPE_P(y, T_FIXNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y));
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y));
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '*');
}
}
Exponentiate by other
Source: show
# File lib/mathn.rb, line 316 def ** (other) if self < 0 && other.round != other Complex(self, 0.0) ** other else power!(other) end end
Returns a new float which is the sum of float and
other.
Source: show
static VALUE
flo_plus(VALUE x, VALUE y)
{
if (RB_TYPE_P(y, T_FIXNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '+');
}
}
Returns a new float which is the difference of float and
other.
Source: show
static VALUE
flo_minus(VALUE x, VALUE y)
{
if (RB_TYPE_P(y, T_FIXNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y));
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y));
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '-');
}
}
Returns float, negated.
Source: show
static VALUE
flo_uminus(VALUE flt)
{
return DBL2NUM(-RFLOAT_VALUE(flt));
}
Returns a new float which is the result of dividing float by
other.
Source: show
static VALUE
flo_div(VALUE x, VALUE y)
{
long f_y;
double d;
if (RB_TYPE_P(y, T_FIXNUM)) {
f_y = FIX2LONG(y);
return DBL2NUM(RFLOAT_VALUE(x) / (double)f_y);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
d = rb_big2dbl(y);
return DBL2NUM(RFLOAT_VALUE(x) / d);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM(RFLOAT_VALUE(x) / RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '/');
}
}
Returns true if float is less than
real.
The result of NaN < NaN is undefined, so the
implementation-dependent value is returned.
Source: show
static VALUE
flo_lt(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return -FIX2INT(rel) < 0 ? Qtrue : Qfalse;
return Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(b)) return Qfalse;
#endif
}
else {
return rb_num_coerce_relop(x, y, '<');
}
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(a)) return Qfalse;
#endif
return (a < b)?Qtrue:Qfalse;
}
Returns true if float is less than or equal to
real.
The result of NaN <= NaN is undefined, so the
implementation-dependent value is returned.
Source: show
static VALUE
flo_le(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return -FIX2INT(rel) <= 0 ? Qtrue : Qfalse;
return Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(b)) return Qfalse;
#endif
}
else {
return rb_num_coerce_relop(x, y, rb_intern("<="));
}
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(a)) return Qfalse;
#endif
return (a <= b)?Qtrue:Qfalse;
}
Returns -1, 0, +1 or nil depending on whether float is less
than, equal to, or greater than real. This is the basis for
the tests in Comparable.
The result of NaN <=> NaN is undefined, so the
implementation-dependent value is returned.
nil is returned if the two values are incomparable.
Source: show
static VALUE
flo_cmp(VALUE x, VALUE y)
{
double a, b;
VALUE i;
a = RFLOAT_VALUE(x);
if (isnan(a)) return Qnil;
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return INT2FIX(-FIX2INT(rel));
return rel;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
}
else {
if (isinf(a) && (i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0)) != Qundef) {
if (RTEST(i)) {
int j = rb_cmpint(i, x, y);
j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
return INT2FIX(j);
}
if (a > 0.0) return INT2FIX(1);
return INT2FIX(-1);
}
return rb_num_coerce_cmp(x, y, id_cmp);
}
return rb_dbl_cmp(a, b);
}
Returns true only if obj has the same value as
float. Contrast this with #eql?, which requires obj to be a Float.
The result of NaN == NaN is undefined, so the
implementation-dependent value is returned.
1.0 == 1 #=> true
Source: show
static VALUE
flo_eq(VALUE x, VALUE y)
{
volatile double a, b;
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
return rb_integer_float_eq(y, x);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(b)) return Qfalse;
#endif
}
else {
return num_equal(x, y);
}
a = RFLOAT_VALUE(x);
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(a)) return Qfalse;
#endif
return (a == b)?Qtrue:Qfalse;
}
Returns true only if obj has the same value as
float. Contrast this with #eql?, which requires obj to be a Float.
The result of NaN == NaN is undefined, so the
implementation-dependent value is returned.
1.0 == 1 #=> true
Source: show
static VALUE
flo_eq(VALUE x, VALUE y)
{
volatile double a, b;
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
return rb_integer_float_eq(y, x);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(b)) return Qfalse;
#endif
}
else {
return num_equal(x, y);
}
a = RFLOAT_VALUE(x);
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(a)) return Qfalse;
#endif
return (a == b)?Qtrue:Qfalse;
}
Returns true if float is greater than
real.
The result of NaN > NaN is undefined, so the
implementation-dependent value is returned.
Source: show
static VALUE
flo_gt(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return -FIX2INT(rel) > 0 ? Qtrue : Qfalse;
return Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(b)) return Qfalse;
#endif
}
else {
return rb_num_coerce_relop(x, y, '>');
}
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(a)) return Qfalse;
#endif
return (a > b)?Qtrue:Qfalse;
}
Returns true if float is greater than or equal to
real.
The result of NaN >= NaN is undefined, so the
implementation-dependent value is returned.
Source: show
static VALUE
flo_ge(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return -FIX2INT(rel) >= 0 ? Qtrue : Qfalse;
return Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(b)) return Qfalse;
#endif
}
else {
return rb_num_coerce_relop(x, y, rb_intern(">="));
}
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(a)) return Qfalse;
#endif
return (a >= b)?Qtrue:Qfalse;
}
Returns the absolute value of float.
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
Source: show
static VALUE
flo_abs(VALUE flt)
{
double val = fabs(RFLOAT_VALUE(flt));
return DBL2NUM(val);
}
Returns 0 if the value is positive, pi otherwise.
Source: show
static VALUE
float_arg(VALUE self)
{
if (isnan(RFLOAT_VALUE(self)))
return self;
if (f_tpositive_p(self))
return INT2FIX(0);
return rb_const_get(rb_mMath, id_PI);
}
Returns 0 if the value is positive, pi otherwise.
Source: show
static VALUE
float_arg(VALUE self)
{
if (isnan(RFLOAT_VALUE(self)))
return self;
if (f_tpositive_p(self))
return INT2FIX(0);
return rb_const_get(rb_mMath, id_PI);
}
Returns the smallest Integer greater than or
equal to float.
1.2.ceil #=> 2
2.0.ceil #=> 2
(-1.2).ceil #=> -1
(-2.0).ceil #=> -2
Source: show
static VALUE
flo_ceil(VALUE num)
{
double f = ceil(RFLOAT_VALUE(num));
long val;
if (!FIXABLE(f)) {
return rb_dbl2big(f);
}
val = (long)f;
return LONG2FIX(val);
}
Returns an array with both a numeric and a float
represented as Float objects.
This is achieved by converting a numeric to a Float.
1.2.coerce(3) #=> [3.0, 1.2]
2.5.coerce(1.1) #=> [1.1, 2.5]
Source: show
static VALUE
flo_coerce(VALUE x, VALUE y)
{
return rb_assoc_new(rb_Float(y), x);
}
provides a unified clone operation, for REXML::XPathParser to use across multiple
Object types
Source: show
# File lib/rexml/xpath_parser.rb, line 27 def dclone ; self ; end
Returns the denominator (always positive). The result is machine dependent.
See numerator.
Source: show
static VALUE
float_denominator(VALUE self)
{
double d = RFLOAT_VALUE(self);
if (isinf(d) || isnan(d))
return INT2FIX(1);
return rb_call_super(0, 0);
}
See Numeric#divmod.
42.0.divmod 6 #=> [7, 0.0]
42.0.divmod 5 #=> [8, 2.0]
Source: show
static VALUE
flo_divmod(VALUE x, VALUE y)
{
double fy, div, mod;
volatile VALUE a, b;
if (RB_TYPE_P(y, T_FIXNUM)) {
fy = (double)FIX2LONG(y);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
fy = rb_big2dbl(y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
fy = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, rb_intern("divmod"));
}
flodivmod(RFLOAT_VALUE(x), fy, &div, &mod);
a = dbl2ival(div);
b = DBL2NUM(mod);
return rb_assoc_new(a, b);
}
Returns true only if obj is a Float with the same value as float.
Contrast this with Float#==, which performs type conversions.
The result of NaN.eql?(NaN) is undefined, so the
implementation-dependent value is returned.
1.0.eql?(1) #=> false
Source: show
static VALUE
flo_eql(VALUE x, VALUE y)
{
if (RB_TYPE_P(y, T_FLOAT)) {
double a = RFLOAT_VALUE(x);
double b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
if (isnan(a) || isnan(b)) return Qfalse;
#endif
if (a == b)
return Qtrue;
}
return Qfalse;
}
Returns float / numeric, same as Float#/.
Source: show
static VALUE
flo_quo(VALUE x, VALUE y)
{
return rb_funcall(x, '/', 1, y);
}
Returns true if float is a valid IEEE floating
point number (it is not infinite, and #nan? is false).
Source: show
static VALUE
flo_is_finite_p(VALUE num)
{
double value = RFLOAT_VALUE(num);
#if HAVE_ISFINITE
if (!isfinite(value))
return Qfalse;
#else
if (isinf(value) || isnan(value))
return Qfalse;
#endif
return Qtrue;
}
Returns the largest integer less than or equal to float.
1.2.floor #=> 1
2.0.floor #=> 2
(-1.2).floor #=> -2
(-2.0).floor #=> -2
Source: show
static VALUE
flo_floor(VALUE num)
{
double f = floor(RFLOAT_VALUE(num));
long val;
if (!FIXABLE(f)) {
return rb_dbl2big(f);
}
val = (long)f;
return LONG2FIX(val);
}
Returns a hash code for this float.
Source: show
static VALUE
flo_hash(VALUE num)
{
double d;
st_index_t hash;
d = RFLOAT_VALUE(num);
/* normalize -0.0 to 0.0 */
if (d == 0.0) d = 0.0;
hash = rb_memhash(&d, sizeof(d));
return LONG2FIX(hash);
}
Return values corresponding to the value of float:
- finite
-
nil - -Infinity
-
-1 - +
Infinity -
1
For example:
(0.0).infinite? #=> nil
(-1.0/0.0).infinite? #=> -1
(+1.0/0.0).infinite? #=> 1
Source: show
static VALUE
flo_is_infinite_p(VALUE num)
{
double value = RFLOAT_VALUE(num);
if (isinf(value)) {
return INT2FIX( value < 0 ? -1 : 1 );
}
return Qnil;
}
Returns the absolute value of float.
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
Source: show
static VALUE
flo_abs(VALUE flt)
{
double val = fabs(RFLOAT_VALUE(flt));
return DBL2NUM(val);
}
Return the modulo after division of float by
other.
6543.21.modulo(137) #=> 104.21
6543.21.modulo(137.24) #=> 92.9299999999996
Source: show
static VALUE
flo_mod(VALUE x, VALUE y)
{
double fy;
if (RB_TYPE_P(y, T_FIXNUM)) {
fy = (double)FIX2LONG(y);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
fy = rb_big2dbl(y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
fy = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, '%');
}
return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
}
Returns true if float is an invalid IEEE floating
point number.
a = -1.0 #=> -1.0
a.nan? #=> false
a = 0.0/0.0 #=> NaN
a.nan? #=> true
Source: show
static VALUE
flo_is_nan_p(VALUE num)
{
double value = RFLOAT_VALUE(num);
return isnan(value) ? Qtrue : Qfalse;
}
Returns the numerator. The result is machine dependent.
n = 0.3.numerator #=> 5404319552844595
d = 0.3.denominator #=> 18014398509481984
n.fdiv(d) #=> 0.3
Source: show
static VALUE
float_numerator(VALUE self)
{
double d = RFLOAT_VALUE(self);
if (isinf(d) || isnan(d))
return self;
return rb_call_super(0, 0);
}
Returns 0 if the value is positive, pi otherwise.
Source: show
static VALUE
float_arg(VALUE self)
{
if (isnan(RFLOAT_VALUE(self)))
return self;
if (f_tpositive_p(self))
return INT2FIX(0);
return rb_const_get(rb_mMath, id_PI);
}
Returns float / numeric, same as Float#/.
Source: show
static VALUE
flo_quo(VALUE x, VALUE y)
{
return rb_funcall(x, '/', 1, y);
}
Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|). if the optional eps is not given, it will be chosen automatically.
0.3.rationalize #=> (3/10)
1.333.rationalize #=> (1333/1000)
1.333.rationalize(0.01) #=> (4/3)
See to_r.
Source: show
static VALUE
float_rationalize(int argc, VALUE *argv, VALUE self)
{
VALUE e;
if (f_negative_p(self))
return f_negate(float_rationalize(argc, argv, f_abs(self)));
rb_scan_args(argc, argv, "01", &e);
if (argc != 0) {
return rb_flt_rationalize_with_prec(self, e);
}
else {
return rb_flt_rationalize(self);
}
}
Rounds float 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
Source: show
static VALUE
flo_round(int argc, VALUE *argv, VALUE num)
{
VALUE nd;
double number, f;
int ndigits = 0;
int binexp;
enum {float_dig = DBL_DIG+2};
if (argc > 0 && rb_scan_args(argc, argv, "01", &nd) == 1) {
ndigits = NUM2INT(nd);
}
if (ndigits < 0) {
return int_round_0(flo_truncate(num), ndigits);
}
number = RFLOAT_VALUE(num);
if (ndigits == 0) {
return dbl2ival(number);
}
frexp(number, &binexp);
/* Let `exp` be such that `number` is written as:"0.#{digits}e#{exp}",
i.e. such that 10 ** (exp - 1) <= |number| < 10 ** exp
Recall that up to float_dig digits can be needed to represent a double,
so if ndigits + exp >= float_dig, the intermediate value (number * 10 ** ndigits)
will be an integer and thus the result is the original number.
If ndigits + exp <= 0, the result is 0 or "1e#{exp}", so
if ndigits + exp < 0, the result is 0.
We have:
2 ** (binexp-1) <= |number| < 2 ** binexp
10 ** ((binexp-1)/log_2(10)) <= |number| < 10 ** (binexp/log_2(10))
If binexp >= 0, and since log_2(10) = 3.322259:
10 ** (binexp/4 - 1) < |number| < 10 ** (binexp/3)
floor(binexp/4) <= exp <= ceil(binexp/3)
If binexp <= 0, swap the /4 and the /3
So if ndigits + floor(binexp/(4 or 3)) >= float_dig, the result is number
If ndigits + ceil(binexp/(3 or 4)) < 0 the result is 0
*/
if (isinf(number) || isnan(number) ||
(ndigits >= float_dig - (binexp > 0 ? binexp / 4 : binexp / 3 - 1))) {
return num;
}
if (ndigits < - (binexp > 0 ? binexp / 3 + 1 : binexp / 4)) {
return DBL2NUM(0);
}
f = pow(10, ndigits);
return DBL2NUM(round(number * f) / f);
}
Convert flt to a BigDecimal and
return it.
require 'bigdecimal'
require 'bigdecimal/util'
0.5.to_d
# => #<BigDecimal:1dc69e0,'0.5E0',9(18)>
Source: show
# File ext/bigdecimal/lib/bigdecimal/util.rb, line 38 def to_d(precision=nil) BigDecimal(self, precision || Float::DIG) end
Since float is already a float, returns self.
Source: show
static VALUE
flo_to_f(VALUE num)
{
return num;
}
float.to_int → integer Link
Source: show
static VALUE
flo_truncate(VALUE num)
{
double f = RFLOAT_VALUE(num);
long val;
if (f > 0.0) f = floor(f);
if (f < 0.0) f = ceil(f);
if (!FIXABLE(f)) {
return rb_dbl2big(f);
}
val = (long)f;
return LONG2FIX(val);
}
Source: show
static VALUE
flo_truncate(VALUE num)
{
double f = RFLOAT_VALUE(num);
long val;
if (f > 0.0) f = floor(f);
if (f < 0.0) f = ceil(f);
if (!FIXABLE(f)) {
return rb_dbl2big(f);
}
val = (long)f;
return LONG2FIX(val);
}
Returns the value as a rational.
NOTE: 0.3.to_r isn't the same as '0.3'.to_r. The latter is equivalent to '3/10'.to_r, but the former isn't so.
2.0.to_r #=> (2/1)
2.5.to_r #=> (5/2)
-0.75.to_r #=> (-3/4)
0.0.to_r #=> (0/1)
See rationalize.
Source: show
static VALUE
float_to_r(VALUE self)
{
VALUE f, n;
float_decode_internal(self, &f, &n);
#if FLT_RADIX == 2
{
long ln = FIX2LONG(n);
if (ln == 0)
return f_to_r(f);
if (ln > 0)
return f_to_r(f_lshift(f, n));
ln = -ln;
return rb_rational_new2(f, f_lshift(ONE, INT2FIX(ln)));
}
#else
return f_to_r(f_mul(f, f_expt(INT2FIX(FLT_RADIX), n)));
#endif
}
Returns a string containing a representation of self. As well as a fixed or
exponential form of the float, the call may return
NaN, Infinity, and -Infinity.
Source: show
static VALUE
flo_to_s(VALUE flt)
{
char *ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve);
enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
enum {float_dig = DBL_DIG+1};
char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
double value = RFLOAT_VALUE(flt);
VALUE s;
char *p, *e;
int sign, decpt, digs;
if (isinf(value))
return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity");
else if (isnan(value))
return rb_usascii_str_new2("NaN");
p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
s = sign ? rb_usascii_str_new_cstr("-") : rb_usascii_str_new(0, 0);
if ((digs = (int)(e - p)) >= (int)sizeof(buf)) digs = (int)sizeof(buf) - 1;
memcpy(buf, p, digs);
xfree(p);
if (decpt > 0) {
if (decpt < digs) {
memmove(buf + decpt + 1, buf + decpt, digs - decpt);
buf[decpt] = '.';
rb_str_cat(s, buf, digs + 1);
}
else if (decpt <= DBL_DIG) {
long len;
char *ptr;
rb_str_cat(s, buf, digs);
rb_str_resize(s, (len = RSTRING_LEN(s)) + decpt - digs + 2);
ptr = RSTRING_PTR(s) + len;
if (decpt > digs) {
memset(ptr, '0', decpt - digs);
ptr += decpt - digs;
}
memcpy(ptr, ".0", 2);
}
else {
goto exp;
}
}
else if (decpt > -4) {
long len;
char *ptr;
rb_str_cat(s, "0.", 2);
rb_str_resize(s, (len = RSTRING_LEN(s)) - decpt + digs);
ptr = RSTRING_PTR(s);
memset(ptr += len, '0', -decpt);
memcpy(ptr -= decpt, buf, digs);
}
else {
exp:
if (digs > 1) {
memmove(buf + 2, buf + 1, digs - 1);
}
else {
buf[2] = '0';
digs++;
}
buf[1] = '.';
rb_str_cat(s, buf, digs + 1);
rb_str_catf(s, "e%+03d", decpt - 1);
}
return s;
}