Class: Float

Inherits:
Numeric show all
Defined in:
numeric.c,
numeric.c

Overview

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

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

Floating point has a different arithmetic and is a inexact number.
So you should know its esoteric system. see following:

- http://docs.sun.com/source/806-3568/ncg_goldberg.html
- http://wiki.github.com/rdp/ruby_tutorials_core/ruby-talk-faq#floats_imprecise
- http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

Constant Summary collapse

ROUNDS =

-1:: Indeterminable 0:: Rounding towards zero 1:: Rounding to the nearest number 2:: Rounding towards positive infinity 3:: Rounding towards negative infinity

Represents the rounding mode for floating point addition.

Usually defaults to 1, rounding to the nearest number.

Other modes include
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.
INT2FIX(FLT_RADIX)
MANT_DIG =

The number of base digits for the double data type.

Usually defaults to 53.

INT2FIX(DBL_MANT_DIG)
DIG =

The number of decimal digits in a double-precision floating point.

Usually defaults to 15.

INT2FIX(DBL_DIG)
MIN_EXP =

The smallest posable exponent value in a double-precision floating point.

Usually defaults to -1021.

INT2FIX(DBL_MIN_EXP)
MAX_EXP =

The largest possible exponent value in a double-precision floating point.

Usually defaults to 1024.

INT2FIX(DBL_MAX_EXP)
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.

INT2FIX(DBL_MIN_10_EXP)
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.

INT2FIX(DBL_MAX_10_EXP)
MIN =

The smallest positive integer in a double-precision floating point.

Usually defaults to 2.2250738585072014e-308.

DBL2NUM(DBL_MIN)
MAX =

The largest possible integer in a double-precision floating point number.

Usually defaults to 1.7976931348623157e+308.

DBL2NUM(DBL_MAX)
EPSILON =

The difference between 1 and the smallest double-precision floating point number.

Usually defaults to 2.2204460492503131e-16.

DBL2NUM(DBL_EPSILON)
INFINITY =

An expression representing positive infinity.

DBL2NUM(INFINITY)
NAN =

An expression representing a value which is "not a number".

DBL2NUM(NAN)

Instance Method Summary collapse

Methods inherited from Numeric

#[email protected], #abs2, #conj, #conjugate, #div, #i, #imag, #imaginary, #initialize_copy, #integer?, #nonzero?, #polar, #real, #real?, #rect, #rectangular, #remainder, #singleton_method_added, #step, #to_c

Methods included from Comparable

#between?

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:


911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
# File 'numeric.c', line 911

static VALUE
flo_mod(VALUE x, VALUE y)
{
    double fy;

    switch (TYPE(y)) {
      case T_FIXNUM:
	fy = (double)FIX2LONG(y);
	break;
      case T_BIGNUM:
	fy = rb_big2dbl(y);
	break;
      case T_FLOAT:
	fy = RFLOAT_VALUE(y);
	break;
      default:
	return rb_num_coerce_bin(x, y, '%');
    }
    return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
}

#*(other) ⇒ Float

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

Returns:


800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'numeric.c', line 800

static VALUE
flo_mul(VALUE x, VALUE y)
{
    switch (TYPE(y)) {
      case T_FIXNUM:
	return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y));
      case T_BIGNUM:
	return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y));
      case T_FLOAT:
	return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y));
      default:
	return rb_num_coerce_bin(x, y, '*');
    }
}

#**(other) ⇒ Float

Raises float the other power.

2.0**3      #=> 8.0

Returns:


984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'numeric.c', line 984

static VALUE
flo_pow(VALUE x, VALUE y)
{
    switch (TYPE(y)) {
      case T_FIXNUM:
	return DBL2NUM(pow(RFLOAT_VALUE(x), (double)FIX2LONG(y)));
      case T_BIGNUM:
	return DBL2NUM(pow(RFLOAT_VALUE(x), rb_big2dbl(y)));
      case T_FLOAT:
	{
	    double dx = RFLOAT_VALUE(x);
	    double dy = RFLOAT_VALUE(y);
	    if (dx < 0 && dy != round(dy))
		return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
	    return DBL2NUM(pow(dx, dy));
	}
      default:
	return rb_num_coerce_bin(x, y, rb_intern("**"));
    }
}

#+(other) ⇒ Float

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

Returns:


754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'numeric.c', line 754

static VALUE
flo_plus(VALUE x, VALUE y)
{
    switch (TYPE(y)) {
      case T_FIXNUM:
	return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
      case T_BIGNUM:
	return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
      case T_FLOAT:
	return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
      default:
	return rb_num_coerce_bin(x, y, '+');
    }
}

#-(other) ⇒ Float

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

Returns:


777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'numeric.c', line 777

static VALUE
flo_minus(VALUE x, VALUE y)
{
    switch (TYPE(y)) {
      case T_FIXNUM:
	return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y));
      case T_BIGNUM:
	return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y));
      case T_FLOAT:
	return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y));
      default:
	return rb_num_coerce_bin(x, y, '-');
    }
}

#-Float

Returns float, negated.

Returns:


740
741
742
743
744
# File 'numeric.c', line 740

static VALUE
flo_uminus(VALUE flt)
{
    return DBL2NUM(-RFLOAT_VALUE(flt));
}

#/(other) ⇒ Float

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

Returns:


823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# File 'numeric.c', line 823

static VALUE
flo_div(VALUE x, VALUE y)
{
    long f_y;
    double d;

    switch (TYPE(y)) {
      case T_FIXNUM:
	f_y = FIX2LONG(y);
	return DBL2NUM(RFLOAT_VALUE(x) / (double)f_y);
      case T_BIGNUM:
	d = rb_big2dbl(y);
	return DBL2NUM(RFLOAT_VALUE(x) / d);
      case T_FLOAT:
	return DBL2NUM(RFLOAT_VALUE(x) / RFLOAT_VALUE(y));
      default:
	return rb_num_coerce_bin(x, y, '/');
    }
}

#<(real) ⇒ Boolean

true if flt is less than real. The result of NaN < NaN is undefined, so the implementation-dependent value is returned.

Returns:

  • (Boolean)

1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
# File 'numeric.c', line 1256

static VALUE
flo_lt(VALUE x, VALUE y)
{
    double a, b;

    a = RFLOAT_VALUE(x);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
      {
        VALUE rel = rb_integer_float_cmp(y, x);
        if (FIXNUM_P(rel))
            return -FIX2INT(rel) < 0 ? Qtrue : Qfalse;
        return Qfalse;
      }

      case T_FLOAT:
	b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
	if (isnan(b)) return Qfalse;
#endif
	break;

      default:
	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;
}

#<=(real) ⇒ Boolean

true if flt is less than or equal to real. The result of NaN <= NaN is undefined, so the implementation-dependent value is returned.

Returns:

  • (Boolean)

1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
# File 'numeric.c', line 1298

static VALUE
flo_le(VALUE x, VALUE y)
{
    double a, b;

    a = RFLOAT_VALUE(x);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
      {
        VALUE rel = rb_integer_float_cmp(y, x);
        if (FIXNUM_P(rel))
            return -FIX2INT(rel) <= 0 ? Qtrue : Qfalse;
        return Qfalse;
      }

      case T_FLOAT:
	b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
	if (isnan(b)) return Qfalse;
#endif
	break;

      default:
	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;
}

#<=>(real) ⇒ -1, ...

Returns -1, 0, +1 or nil depending on whether flt 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.

Returns:

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

1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
# File 'numeric.c', line 1127

static VALUE
flo_cmp(VALUE x, VALUE y)
{
    double a, b;
    VALUE i;

    a = RFLOAT_VALUE(x);
    if (isnan(a)) return Qnil;
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
      {
        VALUE rel = rb_integer_float_cmp(y, x);
        if (FIXNUM_P(rel))
            return INT2FIX(-FIX2INT(rel));
        return rel;
      }

      case T_FLOAT:
	b = RFLOAT_VALUE(y);
	break;

      default:
	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, rb_intern("<=>"));
    }
    return rb_dbl_cmp(a, b);
}

#==(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. The result of NaN == NaN is undefined, so the implementation-dependent value is returned.

1.0 == 1   #=> true

Returns:

  • (Boolean)

1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
# File 'numeric.c', line 1061

static VALUE
flo_eq(VALUE x, VALUE y)
{
    volatile double a, b;

    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
        return rb_integer_float_eq(y, x);
      case T_FLOAT:
	b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
	if (isnan(b)) return Qfalse;
#endif
	break;
      default:
	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;
}

#==(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. The result of NaN == NaN is undefined, so the implementation-dependent value is returned.

1.0 == 1   #=> true

Returns:

  • (Boolean)

1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
# File 'numeric.c', line 1061

static VALUE
flo_eq(VALUE x, VALUE y)
{
    volatile double a, b;

    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
        return rb_integer_float_eq(y, x);
      case T_FLOAT:
	b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
	if (isnan(b)) return Qfalse;
#endif
	break;
      default:
	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;
}

#>(real) ⇒ Boolean

true if flt is greater than real. The result of NaN > NaN is undefined, so the implementation-dependent value is returned.

Returns:

  • (Boolean)

1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
# File 'numeric.c', line 1173

static VALUE
flo_gt(VALUE x, VALUE y)
{
    double a, b;

    a = RFLOAT_VALUE(x);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
      {
        VALUE rel = rb_integer_float_cmp(y, x);
        if (FIXNUM_P(rel))
            return -FIX2INT(rel) > 0 ? Qtrue : Qfalse;
        return Qfalse;
      }

      case T_FLOAT:
	b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
	if (isnan(b)) return Qfalse;
#endif
	break;

      default:
	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;
}

#>=(real) ⇒ Boolean

true if flt is greater than or equal to real. The result of NaN >= NaN is undefined, so the implementation-dependent value is returned.

Returns:

  • (Boolean)

1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
# File 'numeric.c', line 1215

static VALUE
flo_ge(VALUE x, VALUE y)
{
    double a, b;

    a = RFLOAT_VALUE(x);
    switch (TYPE(y)) {
      case T_FIXNUM:
      case T_BIGNUM:
      {
        VALUE rel = rb_integer_float_cmp(y, x);
        if (FIXNUM_P(rel))
            return -FIX2INT(rel) >= 0 ? Qtrue : Qfalse;
        return Qfalse;
      }

      case T_FLOAT:
	b = RFLOAT_VALUE(y);
#if defined(_MSC_VER) && _MSC_VER < 1300
	if (isnan(b)) return Qfalse;
#endif
	break;

      default:
	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;
}

#absFloat #magnitudeFloat

Returns the absolute value of flt.

(-34.56).abs   #=> 34.56
-34.56.abs     #=> 34.56

Overloads:


1383
1384
1385
1386
1387
1388
# File 'numeric.c', line 1383

static VALUE
flo_abs(VALUE flt)
{
    double val = fabs(RFLOAT_VALUE(flt));
    return DBL2NUM(val);
}

#arg0, Float #angle0, Float #phase0, Float

Returns 0 if the value is positive, pi otherwise.

Overloads:


2039
2040
2041
2042
2043
2044
2045
2046
2047
# File 'complex.c', line 2039

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

#arg0, Float #angle0, Float #phase0, Float

Returns 0 if the value is positive, pi otherwise.

Overloads:


2039
2040
2041
2042
2043
2044
2045
2046
2047
# File 'complex.c', line 2039

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

#ceilInteger

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

Returns:


1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
# File 'numeric.c', line 1516

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

#coerce(numeric) ⇒ Array

Returns an array with both aNumeric and flt represented as Float objects. This is achieved by converting aNumeric to a Float.

1.2.coerce(3)       #=> [3.0, 1.2]
2.5.coerce(1.1)     #=> [1.1, 2.5]

Returns:


727
728
729
730
731
# File 'numeric.c', line 727

static VALUE
flo_coerce(VALUE x, VALUE y)
{
    return rb_assoc_new(rb_Float(y), x);
}

#denominatorInteger

Returns the denominator (always positive). The result is machine dependent.

See numerator.

Returns:


1825
1826
1827
1828
1829
1830
1831
1832
# File 'rational.c', line 1825

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

#divmod(numeric) ⇒ Array

See Numeric#divmod.

Returns:


949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
# File 'numeric.c', line 949

static VALUE
flo_divmod(VALUE x, VALUE y)
{
    double fy, div, mod;
    volatile VALUE a, b;

    switch (TYPE(y)) {
      case T_FIXNUM:
	fy = (double)FIX2LONG(y);
	break;
      case T_BIGNUM:
	fy = rb_big2dbl(y);
	break;
      case T_FLOAT:
	fy = RFLOAT_VALUE(y);
	break;
      default:
	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);
}

#eql?(obj) ⇒ Boolean

Returns true only if obj is a Float with the same value as flt. 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

Returns:

  • (Boolean)

Returns:

  • (Boolean)

1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
# File 'numeric.c', line 1343

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

#quo(numeric) ⇒ Float

Returns float / numeric.

Returns:


850
851
852
853
854
# File 'numeric.c', line 850

static VALUE
flo_quo(VALUE x, VALUE y)
{
    return rb_funcall(x, '/', 1, y);
}

#finite?Boolean

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

Returns:

  • (Boolean)

Returns:

  • (Boolean)

1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
# File 'numeric.c', line 1462

static VALUE
flo_is_finite_p(VALUE num)
{
    double value = RFLOAT_VALUE(num);

#if HAVE_FINITE
    if (!finite(value))
	return Qfalse;
#else
    if (isinf(value) || isnan(value))
	return Qfalse;
#endif

    return Qtrue;
}

#floorInteger

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

Returns:


1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
# File 'numeric.c', line 1490

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

#hashInteger

Returns a hash code for this float.

Returns:


1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'numeric.c', line 1093

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

#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)

Returns:

  • (Boolean)

1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
# File 'numeric.c', line 1440

static VALUE
flo_is_infinite_p(VALUE num)
{
    double value = RFLOAT_VALUE(num);

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

    return Qnil;
}

#absFloat #magnitudeFloat

Returns the absolute value of flt.

(-34.56).abs   #=> 34.56
-34.56.abs     #=> 34.56

Overloads:


1383
1384
1385
1386
1387
1388
# File 'numeric.c', line 1383

static VALUE
flo_abs(VALUE flt)
{
    double val = fabs(RFLOAT_VALUE(flt));
    return DBL2NUM(val);
}

#%(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:


911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
# File 'numeric.c', line 911

static VALUE
flo_mod(VALUE x, VALUE y)
{
    double fy;

    switch (TYPE(y)) {
      case T_FIXNUM:
	fy = (double)FIX2LONG(y);
	break;
      case T_BIGNUM:
	fy = rb_big2dbl(y);
	break;
      case T_FLOAT:
	fy = RFLOAT_VALUE(y);
	break;
      default:
	return rb_num_coerce_bin(x, y, '%');
    }
    return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
}

#nan?Boolean

Returns true if flt is an invalid IEEE floating point number.

a = -1.0      #=> -1.0
a.nan?        #=> false
a = 0.0/0.0   #=> NaN
a.nan?        #=> true

Returns:

  • (Boolean)

Returns:

  • (Boolean)

1420
1421
1422
1423
1424
1425
1426
# File 'numeric.c', line 1420

static VALUE
flo_is_nan_p(VALUE num)
{
    double value = RFLOAT_VALUE(num);

    return isnan(value) ? Qtrue : Qfalse;
}

#numeratorInteger

Returns the numerator. The result is machine dependent.

n = 0.3.numerator    #=> 5404319552844595
d = 0.3.denominator  #=> 18014398509481984
n.fdiv(d)            #=> 0.3

Returns:


1807
1808
1809
1810
1811
1812
1813
1814
# File 'rational.c', line 1807

static VALUE
float_numerator(VALUE self)
{
    double d = RFLOAT_VALUE(self);
    if (isinf(d) || isnan(d))
	return self;
    return rb_call_super(0, 0);
}

#arg0, Float #angle0, Float #phase0, Float

Returns 0 if the value is positive, pi otherwise.

Overloads:


2039
2040
2041
2042
2043
2044
2045
2046
2047
# File 'complex.c', line 2039

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

#quo(numeric) ⇒ Float

Returns float / numeric.

Returns:


850
851
852
853
854
# File 'numeric.c', line 850

static VALUE
flo_quo(VALUE x, VALUE y)
{
    return rb_funcall(x, '/', 1, y);
}

#rationalize([eps]) ⇒ Object

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.


1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
# File 'rational.c', line 1968

static VALUE
float_rationalize(int argc, VALUE *argv, VALUE self)
{
    VALUE e, a, b, p, q;

    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) {
	e = f_abs(e);
	a = f_sub(self, e);
	b = f_add(self, e);
    }
    else {
	VALUE f, n;

	float_decode_internal(self, &f, &n);
	if (f_zero_p(f) || f_positive_p(n))
	    return rb_rational_new1(f_lshift(f, n));

#if FLT_RADIX == 2
	{
	    VALUE two_times_f, den;

	    two_times_f = f_mul(TWO, f);
	    den = f_lshift(ONE, f_sub(ONE, n));

	    a = rb_rational_new2(f_sub(two_times_f, ONE), den);
	    b = rb_rational_new2(f_add(two_times_f, ONE), den);
	}
#else
	{
	    VALUE radix_times_f, den;

	    radix_times_f = f_mul(INT2FIX(FLT_RADIX), f);
	    den = f_expt(INT2FIX(FLT_RADIX), f_sub(ONE, n));

	    a = rb_rational_new2(f_sub(radix_times_f, INT2FIX(FLT_RADIX - 1)), den);
	    b = rb_rational_new2(f_add(radix_times_f, INT2FIX(FLT_RADIX - 1)), den);
	}
#endif
    }

    if (f_eqeq_p(a, b))
	return f_to_r(self);

    nurat_rationalize_internal(a, b, &p, &q);
    return rb_rational_new2(p, q);
}

#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:


1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
# File 'numeric.c', line 1601

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

#to_fself

As flt is already a float, returns self.

Returns:

  • (self)

1365
1366
1367
1368
1369
# File 'numeric.c', line 1365

static VALUE
flo_to_f(VALUE num)
{
    return num;
}

#to_iInteger #to_intInteger #truncateInteger

Returns flt truncated to an Integer.

Overloads:


1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
# File 'numeric.c', line 1659

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

#to_iInteger #to_intInteger #truncateInteger

Returns flt truncated to an Integer.

Overloads:


1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
# File 'numeric.c', line 1659

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

#to_rObject

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.


1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
# File 'rational.c', line 1932

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
}

#to_sString Also known as: inspect

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''.

Returns:


646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
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
# File 'numeric.c', line 646

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

#to_iInteger #to_intInteger #truncateInteger

Returns flt truncated to an Integer.

Overloads:


1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
# File 'numeric.c', line 1659

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

#zero?Boolean

Returns true if flt is 0.0.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

1398
1399
1400
1401
1402
1403
1404
1405
# File 'numeric.c', line 1398

static VALUE
flo_zero_p(VALUE num)
{
    if (RFLOAT_VALUE(num) == 0.0) {
	return Qtrue;
    }
    return Qfalse;
}