Class: Rational
Overview
A rational number can be represented as a pair of integer numbers: a/b (b>0), where a is the numerator and b is the denominator. Integer a equals rational a/1 mathematically.
You can create a Rational object explicitly with:
You can convert certain objects to Rationals with:
-
Method #Rational.
Examples
Rational(1) #=> (1/1)
Rational(2, 3) #=> (2/3)
Rational(4, -6) #=> (-2/3) # Reduced.
3.to_r #=> (3/1)
2/3r #=> (2/3)
You can also create rational objects from floating-point numbers or strings.
Rational(0.3) #=> (5404319552844595/18014398509481984)
Rational('0.3') #=> (3/10)
Rational('2/3') #=> (2/3)
0.3.to_r #=> (5404319552844595/18014398509481984)
'0.3'.to_r #=> (3/10)
'2/3'.to_r #=> (2/3)
0.3.rationalize #=> (3/10)
A rational object is an exact number, which helps you to write programs without any rounding errors.
10.times.inject(0) {|t| t + 0.1 } #=> 0.9999999999999999
10.times.inject(0) {|t| t + Rational('0.1') } #=> (1/1)
However, when an expression includes an inexact component (numerical value or operation), it will produce an inexact result.
Rational(10) / 3 #=> (10/3)
Rational(10) / 3.0 #=> 3.3333333333333335
Rational(-8) ** Rational(1, 3)
#=> (1.0000000000000002+1.7320508075688772i)
Defined Under Namespace
Classes: compatible
Instance Method Summary collapse
-
#*(numeric) ⇒ Numeric
Performs multiplication.
- #** ⇒ Object
-
#+(numeric) ⇒ Numeric
Performs addition.
-
#-(numeric) ⇒ Numeric
Performs subtraction.
-
#- ⇒ Object
Negates
rat
. -
#/(other) ⇒ Object
Performs division.
-
#<=>(numeric) ⇒ -1, ...
Returns -1, 0, or +1 depending on whether
rational
is less than, equal to, or greater thannumeric
. -
#==(object) ⇒ Boolean
Returns
true
ifrat
equalsobject
numerically. -
#abs ⇒ Object
Returns the absolute value of
rat
. -
#ceil([ndigits]) ⇒ Integer
Returns the smallest number greater than or equal to
rat
with a precision ofndigits
decimal digits (default: 0). -
#coerce(other) ⇒ Object
:nodoc:.
-
#denominator ⇒ Integer
Returns the denominator (always positive).
-
#fdiv(numeric) ⇒ Float
Performs division and returns the value as a Float.
-
#floor([ndigits]) ⇒ Integer
Returns the largest number less than or equal to
rat
with a precision ofndigits
decimal digits (default: 0). - #hash ⇒ Object
-
#inspect ⇒ String
Returns the value as a string for inspection.
-
#magnitude ⇒ Object
Returns the absolute value of
rat
. -
#marshal_dump ⇒ Object
private
:nodoc:.
-
#negative? ⇒ Boolean
Returns
true
ifrat
is less than 0. -
#numerator ⇒ Integer
Returns the numerator.
-
#positive? ⇒ Boolean
Returns
true
ifrat
is greater than 0. -
#quo(other) ⇒ Object
Performs division.
-
#rationalize(*args) ⇒ Object
Returns a simpler approximation of the value if the optional argument
eps
is given (rat-|eps| <= result <= rat+|eps|), self otherwise. -
#round([ndigits][, half: mode]) ⇒ Integer
Returns
rat
rounded to the nearest value with a precision ofndigits
decimal digits (default: 0). -
#to_f ⇒ Float
Returns the value as a Float.
-
#to_i ⇒ Integer
Returns the truncated value as an integer.
-
#to_r ⇒ self
Returns self.
-
#to_s ⇒ String
Returns the value as a string.
-
#truncate([ndigits]) ⇒ Integer
Returns
rat
truncated (toward zero) to a precision ofndigits
decimal digits (default: 0).
Methods inherited from Numeric
#%, #abs2, #angle, #arg, #clone, #div, #divmod, #eql?, #i, #modulo, #nonzero?, #phase, #polar, #rect, #rectangular, #remainder, #singleton_method_added, #step, #to_c, #to_int, #zero?
Methods included from Comparable
#<, #<=, #>, #>=, #between?, #clamp
Instance Method Details
#*(numeric) ⇒ Numeric
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 |
# File 'rational.c', line 866
VALUE
rb_rational_mul(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
{
get_dat1(self);
return f_muldiv(self,
dat->num, dat->den,
other, ONE, '*');
}
}
else if (RB_FLOAT_TYPE_P(other)) {
return DBL2NUM(nurat_to_double(self) * RFLOAT_VALUE(other));
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
{
get_dat2(self, other);
return f_muldiv(self,
adat->num, adat->den,
bdat->num, bdat->den, '*');
}
}
else {
return rb_num_coerce_bin(self, other, '*');
}
}
|
#** ⇒ Object
#+(numeric) ⇒ Numeric
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 |
# File 'rational.c', line 729
VALUE
rb_rational_plus(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
{
get_dat1(self);
return f_rational_new_no_reduce2(CLASS_OF(self),
rb_int_plus(dat->num, rb_int_mul(other, dat->den)),
dat->den);
}
}
else if (RB_FLOAT_TYPE_P(other)) {
return DBL2NUM(nurat_to_double(self) + RFLOAT_VALUE(other));
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
{
get_dat2(self, other);
return f_addsub(self,
adat->num, adat->den,
bdat->num, bdat->den, '+');
}
}
else {
return rb_num_coerce_bin(self, other, '+');
}
}
|
#-(numeric) ⇒ Numeric
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 |
# File 'rational.c', line 770
VALUE
rb_rational_minus(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
{
get_dat1(self);
return f_rational_new_no_reduce2(CLASS_OF(self),
rb_int_minus(dat->num, rb_int_mul(other, dat->den)),
dat->den);
}
}
else if (RB_FLOAT_TYPE_P(other)) {
return DBL2NUM(nurat_to_double(self) - RFLOAT_VALUE(other));
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
{
get_dat2(self, other);
return f_addsub(self,
adat->num, adat->den,
bdat->num, bdat->den, '-');
}
}
else {
return rb_num_coerce_bin(self, other, '-');
}
}
|
#- ⇒ Object
Negates rat
.
616 617 618 619 620 621 622 623 |
# File 'rational.c', line 616
VALUE
rb_rational_uminus(VALUE self)
{
const int unused = (RUBY_ASSERT(RB_TYPE_P(self, T_RATIONAL)), 0);
get_dat1(self);
(void)unused;
return f_rational_new2(CLASS_OF(self), rb_int_uminus(dat->num), dat->den);
}
|
#/(numeric) ⇒ Numeric #quo(numeric) ⇒ Numeric
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 |
# File 'rational.c', line 908
VALUE
rb_rational_div(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
if (f_zero_p(other))
rb_num_zerodiv();
{
get_dat1(self);
return f_muldiv(self,
dat->num, dat->den,
other, ONE, '/');
}
}
else if (RB_FLOAT_TYPE_P(other)) {
VALUE v = nurat_to_f(self);
return rb_flo_div_flo(v, other);
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
if (f_zero_p(other))
rb_num_zerodiv();
{
get_dat2(self, other);
if (f_one_p(self))
return f_rational_new_no_reduce2(CLASS_OF(self),
bdat->den, bdat->num);
return f_muldiv(self,
adat->num, adat->den,
bdat->num, bdat->den, '/');
}
}
else {
return rb_num_coerce_bin(self, other, '/');
}
}
|
#<=>(numeric) ⇒ -1, ...
Returns -1, 0, or +1 depending on whether rational
is less than, equal to, or greater than numeric
.
nil
is returned if the two values are incomparable.
Rational(2, 3) <=> Rational(2, 3) #=> 0
Rational(5) <=> 5 #=> 0
Rational(2, 3) <=> Rational(1, 3) #=> 1
Rational(1, 3) <=> 1 #=> -1
Rational(1, 3) <=> 0.3 #=> 1
Rational(1, 3) <=> "0.3" #=> nil
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 |
# File 'rational.c', line 1079
VALUE
rb_rational_cmp(VALUE self, VALUE other)
{
switch (TYPE(other)) {
case T_FIXNUM:
case T_BIGNUM:
{
get_dat1(self);
if (dat->den == LONG2FIX(1))
return rb_int_cmp(dat->num, other); /* c14n */
other = f_rational_new_bang1(CLASS_OF(self), other);
/* FALLTHROUGH */
}
case T_RATIONAL:
{
VALUE num1, num2;
get_dat2(self, other);
if (FIXNUM_P(adat->num) && FIXNUM_P(adat->den) &&
FIXNUM_P(bdat->num) && FIXNUM_P(bdat->den)) {
num1 = f_imul(FIX2LONG(adat->num), FIX2LONG(bdat->den));
num2 = f_imul(FIX2LONG(bdat->num), FIX2LONG(adat->den));
}
else {
num1 = rb_int_mul(adat->num, bdat->den);
num2 = rb_int_mul(bdat->num, adat->den);
}
return rb_int_cmp(rb_int_minus(num1, num2), ZERO);
}
case T_FLOAT:
return rb_dbl_cmp(nurat_to_double(self), RFLOAT_VALUE(other));
default:
return rb_num_coerce_cmp(self, other, idCmp);
}
}
|
#==(object) ⇒ Boolean
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 1163 1164 1165 1166 1167 1168 1169 1170 1171 |
# File 'rational.c', line 1132
static VALUE
nurat_eqeq_p(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
get_dat1(self);
if (RB_INTEGER_TYPE_P(dat->num) && RB_INTEGER_TYPE_P(dat->den)) {
if (INT_ZERO_P(dat->num) && INT_ZERO_P(other))
return Qtrue;
if (!FIXNUM_P(dat->den))
return Qfalse;
if (FIX2LONG(dat->den) != 1)
return Qfalse;
return rb_int_equal(dat->num, other);
}
else {
const double d = nurat_to_double(self);
return RBOOL(FIXNUM_ZERO_P(rb_dbl_cmp(d, NUM2DBL(other))));
}
}
else if (RB_FLOAT_TYPE_P(other)) {
const double d = nurat_to_double(self);
return RBOOL(FIXNUM_ZERO_P(rb_dbl_cmp(d, RFLOAT_VALUE(other))));
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
{
get_dat2(self, other);
if (INT_ZERO_P(adat->num) && INT_ZERO_P(bdat->num))
return Qtrue;
return RBOOL(rb_int_equal(adat->num, bdat->num) &&
rb_int_equal(adat->den, bdat->den));
}
}
else {
return rb_equal(other, self);
}
}
|
#abs ⇒ Object #magnitude ⇒ Object
Returns the absolute value of rat
.
(1/2r).abs #=> (1/2)
(-1/2r).abs #=> (1/2)
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 |
# File 'rational.c', line 1243
VALUE
rb_rational_abs(VALUE self)
{
get_dat1(self);
if (INT_NEGATIVE_P(dat->num)) {
VALUE num = rb_int_abs(dat->num);
return nurat_s_canonicalize_internal_no_reduce(CLASS_OF(self), num, dat->den);
}
return self;
}
|
#ceil([ndigits]) ⇒ Integer
Returns the smallest number greater than or equal to rat
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 rational when ndigits
is positive, otherwise returns an integer.
Rational(3).ceil #=> 3
Rational(2, 3).ceil #=> 1
Rational(-3, 2).ceil #=> -1
# decimal - 1 2 3 . 4 5 6
# ^ ^ ^ ^ ^ ^
# precision -3 -2 -1 0 +1 +2
Rational('-123.456').ceil(+1).to_f #=> -123.4
Rational('-123.456').ceil(-1) #=> -120
1469 1470 1471 1472 1473 |
# File 'rational.c', line 1469
static VALUE
nurat_ceil_n(int argc, VALUE *argv, VALUE self)
{
return f_round_common(argc, argv, self, nurat_ceil);
}
|
#coerce(other) ⇒ Object
:nodoc:
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 'rational.c', line 1174
static VALUE
nurat_coerce(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
return rb_assoc_new(f_rational_new_bang1(CLASS_OF(self), other), self);
}
else if (RB_FLOAT_TYPE_P(other)) {
return rb_assoc_new(other, nurat_to_f(self));
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
return rb_assoc_new(other, self);
}
else if (RB_TYPE_P(other, T_COMPLEX)) {
if (!k_exact_zero_p(RCOMPLEX(other)->imag))
return rb_assoc_new(other, rb_Complex(self, INT2FIX(0)));
other = RCOMPLEX(other)->real;
if (RB_FLOAT_TYPE_P(other)) {
other = float_to_r(other);
RBASIC_SET_CLASS(other, CLASS_OF(self));
}
else {
other = f_rational_new_bang1(CLASS_OF(self), other);
}
return rb_assoc_new(other, self);
}
rb_raise(rb_eTypeError, "%s can't be coerced into %s",
rb_obj_classname(other), rb_obj_classname(self));
return Qnil;
}
|
#denominator ⇒ Integer
603 604 605 606 607 608 |
# File 'rational.c', line 603
static VALUE
nurat_denominator(VALUE self)
{
get_dat1(self);
return dat->den;
}
|
#fdiv(numeric) ⇒ Float
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 |
# File 'rational.c', line 956
static VALUE
nurat_fdiv(VALUE self, VALUE other)
{
VALUE div;
if (f_zero_p(other))
return rb_rational_div(self, rb_float_new(0.0));
if (FIXNUM_P(other) && other == LONG2FIX(1))
return nurat_to_f(self);
div = rb_rational_div(self, other);
if (RB_TYPE_P(div, T_RATIONAL))
return nurat_to_f(div);
if (RB_FLOAT_TYPE_P(div))
return div;
return rb_funcall(div, idTo_f, 0);
}
|
#floor([ndigits]) ⇒ Integer
Returns the largest number less than or equal to rat
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 rational when ndigits
is positive, otherwise returns an integer.
Rational(3).floor #=> 3
Rational(2, 3).floor #=> 0
Rational(-3, 2).floor #=> -2
# decimal - 1 2 3 . 4 5 6
# ^ ^ ^ ^ ^ ^
# precision -3 -2 -1 0 +1 +2
Rational('-123.456').floor(+1).to_f #=> -123.5
Rational('-123.456').floor(-1) #=> -130
1439 1440 1441 1442 1443 |
# File 'rational.c', line 1439
static VALUE
nurat_floor_n(int argc, VALUE *argv, VALUE self)
{
return f_round_common(argc, argv, self, nurat_floor);
}
|
#hash ⇒ Object
1776 1777 1778 1779 1780 |
# File 'rational.c', line 1776
static VALUE
nurat_hash(VALUE self)
{
return ST2FIX(rb_rational_hash(self));
}
|
#inspect ⇒ String
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 |
# File 'rational.c', line 1822
static VALUE
nurat_inspect(VALUE self)
{
VALUE s;
s = rb_usascii_str_new2("(");
rb_str_concat(s, f_format(self, f_inspect));
rb_str_cat2(s, ")");
return s;
}
|
#abs ⇒ Object #magnitude ⇒ Object
Returns the absolute value of rat
.
(1/2r).abs #=> (1/2)
(-1/2r).abs #=> (1/2)
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 |
# File 'rational.c', line 1243
VALUE
rb_rational_abs(VALUE self)
{
get_dat1(self);
if (INT_NEGATIVE_P(dat->num)) {
VALUE num = rb_int_abs(dat->num);
return nurat_s_canonicalize_internal_no_reduce(CLASS_OF(self), num, dat->den);
}
return self;
}
|
#marshal_dump ⇒ Object (private)
:nodoc:
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 |
# File 'rational.c', line 1861
static VALUE
nurat_marshal_dump(VALUE self)
{
VALUE a;
get_dat1(self);
a = rb_assoc_new(dat->num, dat->den);
rb_copy_generic_ivar(a, self);
return a;
}
|
#negative? ⇒ Boolean
Returns true
if rat
is less than 0.
1224 1225 1226 1227 1228 1229 |
# File 'rational.c', line 1224
static VALUE
nurat_negative_p(VALUE self)
{
get_dat1(self);
return RBOOL(INT_NEGATIVE_P(dat->num));
}
|
#numerator ⇒ Integer
585 586 587 588 589 590 |
# File 'rational.c', line 585
static VALUE
nurat_numerator(VALUE self)
{
get_dat1(self);
return dat->num;
}
|
#positive? ⇒ Boolean
Returns true
if rat
is greater than 0.
1211 1212 1213 1214 1215 1216 |
# File 'rational.c', line 1211
static VALUE
nurat_positive_p(VALUE self)
{
get_dat1(self);
return RBOOL(INT_POSITIVE_P(dat->num));
}
|
#/(numeric) ⇒ Numeric #quo(numeric) ⇒ Numeric
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 |
# File 'rational.c', line 908
VALUE
rb_rational_div(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
if (f_zero_p(other))
rb_num_zerodiv();
{
get_dat1(self);
return f_muldiv(self,
dat->num, dat->den,
other, ONE, '/');
}
}
else if (RB_FLOAT_TYPE_P(other)) {
VALUE v = nurat_to_f(self);
return rb_flo_div_flo(v, other);
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
if (f_zero_p(other))
rb_num_zerodiv();
{
get_dat2(self, other);
if (f_one_p(self))
return f_rational_new_no_reduce2(CLASS_OF(self),
bdat->den, bdat->num);
return f_muldiv(self,
adat->num, adat->den,
bdat->num, bdat->den, '/');
}
}
else {
return rb_num_coerce_bin(self, other, '/');
}
}
|
#rationalize ⇒ self #rationalize(eps) ⇒ Object
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 |
# File 'rational.c', line 1729
static VALUE
nurat_rationalize(int argc, VALUE *argv, VALUE self)
{
VALUE e, a, b, p, q;
VALUE rat = self;
get_dat1(self);
if (rb_check_arity(argc, 0, 1) == 0)
return self;
e = f_abs(argv[0]);
if (INT_NEGATIVE_P(dat->num)) {
rat = f_rational_new2(RBASIC_CLASS(self), rb_int_uminus(dat->num), dat->den);
}
a = FIXNUM_ZERO_P(e) ? rat : rb_rational_minus(rat, e);
b = FIXNUM_ZERO_P(e) ? rat : rb_rational_plus(rat, e);
if (f_eqeq_p(a, b))
return self;
nurat_rationalize_internal(a, b, &p, &q);
if (rat != self) {
RATIONAL_SET_NUM(rat, rb_int_uminus(p));
RATIONAL_SET_DEN(rat, q);
return rat;
}
return f_rational_new2(CLASS_OF(self), p, q);
}
|
#round([ndigits][, half: mode]) ⇒ Integer
Returns rat
rounded to the nearest value 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 rational when ndigits
is positive, otherwise returns an integer.
Rational(3).round #=> 3
Rational(2, 3).round #=> 1
Rational(-3, 2).round #=> -2
# decimal - 1 2 3 . 4 5 6
# ^ ^ ^ ^ ^ ^
# precision -3 -2 -1 0 +1 +2
Rational('-123.456').round(+1).to_f #=> -123.5
Rational('-123.456').round(-1) #=> -120
The optional half
keyword argument is available similar to Float#round.
Rational(25, 100).round(1, half: :up) #=> (3/10)
Rational(25, 100).round(1, half: :down) #=> (1/5)
Rational(25, 100).round(1, half: :even) #=> (1/5)
Rational(35, 100).round(1, half: :up) #=> (2/5)
Rational(35, 100).round(1, half: :down) #=> (3/10)
Rational(35, 100).round(1, half: :even) #=> (2/5)
Rational(-25, 100).round(1, half: :up) #=> (-3/10)
Rational(-25, 100).round(1, half: :down) #=> (-1/5)
Rational(-25, 100).round(1, half: :even) #=> (-1/5)
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 |
# File 'rational.c', line 1542
static VALUE
nurat_round_n(int argc, VALUE *argv, VALUE self)
{
VALUE opt;
enum ruby_num_rounding_mode mode = (
argc = rb_scan_args(argc, argv, "*:", NULL, &opt),
rb_num_get_rounding_option(opt));
VALUE (*round_func)(VALUE) = ROUND_FUNC(mode, nurat_round);
return f_round_common(argc, argv, self, round_func);
}
|
#to_f ⇒ Float
1580 1581 1582 1583 1584 |
# File 'rational.c', line 1580
static VALUE
nurat_to_f(VALUE self)
{
return DBL2NUM(nurat_to_double(self));
}
|
#to_i ⇒ Integer
1282 1283 1284 1285 1286 1287 1288 1289 |
# File 'rational.c', line 1282
static VALUE
nurat_truncate(VALUE self)
{
get_dat1(self);
if (INT_NEGATIVE_P(dat->num))
return rb_int_uminus(rb_int_idiv(rb_int_uminus(dat->num), dat->den));
return rb_int_idiv(dat->num, dat->den);
}
|
#to_r ⇒ self
1595 1596 1597 1598 1599 |
# File 'rational.c', line 1595
static VALUE
nurat_to_r(VALUE self)
{
return self;
}
|
#to_s ⇒ String
1806 1807 1808 1809 1810 |
# File 'rational.c', line 1806
static VALUE
nurat_to_s(VALUE self)
{
return f_format(self, f_to_s);
}
|
#truncate([ndigits]) ⇒ Integer
Returns rat
truncated (toward zero) to 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 rational when ndigits
is positive, otherwise returns an integer.
Rational(3).truncate #=> 3
Rational(2, 3).truncate #=> 0
Rational(-3, 2).truncate #=> -1
# decimal - 1 2 3 . 4 5 6
# ^ ^ ^ ^ ^ ^
# precision -3 -2 -1 0 +1 +2
Rational('-123.456').truncate(+1).to_f #=> -123.4
Rational('-123.456').truncate(-1) #=> -120
1499 1500 1501 1502 1503 |
# File 'rational.c', line 1499
static VALUE
nurat_truncate_n(int argc, VALUE *argv, VALUE self)
{
return f_round_common(argc, argv, self, nurat_truncate);
}
|