Class: Rational
Overview
A rational number can be represented as a paired integer number; a/b (b>0). Where a is numerator and b is denominator. Integer a equals rational a/1 mathematically.
In ruby, you can create rational object with Rational, to_r or rationalize method. The return values will be irreducible.
Rational(1) #=> (1/1)
Rational(2, 3) #=> (2/3)
Rational(4, -6) #=> (-2/3)
3.to_r #=> (3/1)
You can also create rational object 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 program 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 has inexact factor (numerical value or operation), 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.
-
#**(numeric) ⇒ Numeric
Performs exponentiation.
-
#+(numeric) ⇒ Numeric
Performs addition.
-
#-(numeric) ⇒ Numeric
Performs subtraction.
-
#/ ⇒ Object
Performs division.
-
#// ⇒ Object
:nodoc:.
-
#<=>(numeric) ⇒ -1, ...
Performs comparison and returns -1, 0, or +1.
-
#==(object) ⇒ Boolean
Returns true if rat equals object numerically.
-
#ceil ⇒ Object
Returns the truncated value (toward positive infinity).
-
#coerce ⇒ Object
:nodoc:.
-
#denominator ⇒ Integer
Returns the denominator (always positive).
-
#exact? ⇒ Boolean
:nodoc:.
-
#fdiv(numeric) ⇒ Float
Performs division and returns the value as a float.
-
#floor ⇒ Object
Returns the truncated value (toward negative infinity).
-
#hash ⇒ Object
:nodoc:.
-
#inspect ⇒ String
Returns the value as a string for inspection.
-
#marshal_dump ⇒ Object
private
:nodoc:.
-
#numerator ⇒ Integer
Returns the numerator.
-
#quo ⇒ Object
Performs division.
-
#quot ⇒ Object
:nodoc:.
-
#quotrem ⇒ Object
:nodoc:.
-
#rational? ⇒ Boolean
:nodoc:.
-
#rationalize ⇒ Object
Returns a simpler approximation of the value if the optional argument eps is given (rat-|eps| <= result <= rat+|eps|), self otherwise.
-
#round ⇒ Object
Returns the truncated value (toward the nearest integer; 0.5 => 1; -0.5 => -1).
-
#to_f ⇒ Float
Return 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 ⇒ Object
Returns the truncated value (toward zero).
Methods inherited from Numeric
#%, #+@, #-@, #abs, #abs2, #angle, #arg, #conj, #conjugate, #div, #divmod, #eql?, #i, #imag, #imaginary, #initialize_copy, #integer?, #magnitude, #modulo, #nonzero?, #phase, #polar, #real, #real?, #rect, #rectangular, #remainder, #singleton_method_added, #step, #to_c, #to_int, #zero?
Methods included from Comparable
Instance Method Details
#*(numeric) ⇒ Numeric
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 |
# File 'rational.c', line 897
static VALUE
nurat_mul(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_FIXNUM) || RB_TYPE_P(other, T_BIGNUM)) {
{
get_dat1(self);
return f_muldiv(self,
dat->num, dat->den,
other, ONE, '*');
}
}
else if (RB_TYPE_P(other, T_FLOAT)) {
return f_mul(f_to_f(self), 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, '*');
}
}
|
#**(numeric) ⇒ Numeric
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 |
# File 'rational.c', line 1015
static VALUE
nurat_expt(VALUE self, VALUE other)
{
if (k_numeric_p(other) && k_exact_zero_p(other))
return f_rational_new_bang1(CLASS_OF(self), ONE);
if (k_rational_p(other)) {
get_dat1(other);
if (f_one_p(dat->den))
other = dat->num; /* c14n */
}
/* Deal with special cases of 0**n and 1**n */
if (k_numeric_p(other) && k_exact_p(other)) {
get_dat1(self);
if (f_one_p(dat->den)) {
if (f_one_p(dat->num)) {
return f_rational_new_bang1(CLASS_OF(self), ONE);
}
else if (f_minus_one_p(dat->num) && k_integer_p(other)) {
return f_rational_new_bang1(CLASS_OF(self), INT2FIX(f_odd_p(other) ? -1 : 1));
}
else if (f_zero_p(dat->num)) {
if (FIX2INT(f_cmp(other, ZERO)) == -1) {
rb_raise_zerodiv();
}
else {
return f_rational_new_bang1(CLASS_OF(self), ZERO);
}
}
}
}
/* General case */
if (RB_TYPE_P(other, T_FIXNUM)) {
{
VALUE num, den;
get_dat1(self);
switch (FIX2INT(f_cmp(other, ZERO))) {
case 1:
num = f_expt(dat->num, other);
den = f_expt(dat->den, other);
break;
case -1:
num = f_expt(dat->den, f_negate(other));
den = f_expt(dat->num, f_negate(other));
break;
default:
num = ONE;
den = ONE;
break;
}
return f_rational_new2(CLASS_OF(self), num, den);
}
}
else if (RB_TYPE_P(other, T_BIGNUM)) {
rb_warn("in a**b, b may be too big");
return f_expt(f_to_f(self), other);
}
else if (RB_TYPE_P(other, T_FLOAT) || RB_TYPE_P(other, T_RATIONAL)) {
return f_expt(f_to_f(self), other);
}
else {
return rb_num_coerce_bin(self, other, id_expt);
}
}
|
#+(numeric) ⇒ Numeric
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 |
# File 'rational.c', line 776
static VALUE
nurat_add(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_FIXNUM) || RB_TYPE_P(other, T_BIGNUM)) {
{
get_dat1(self);
return f_addsub(self,
dat->num, dat->den,
other, ONE, '+');
}
}
else if (RB_TYPE_P(other, T_FLOAT)) {
return f_add(f_to_f(self), 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
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 |
# File 'rational.c', line 817
static VALUE
nurat_sub(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_FIXNUM) || RB_TYPE_P(other, T_BIGNUM)) {
{
get_dat1(self);
return f_addsub(self,
dat->num, dat->den,
other, ONE, '-');
}
}
else if (RB_TYPE_P(other, T_FLOAT)) {
return f_sub(f_to_f(self), 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 #quo(numeric) ⇒ Numeric
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
# File 'rational.c', line 939
static VALUE
nurat_div(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_FIXNUM) || RB_TYPE_P(other, T_BIGNUM)) {
if (f_zero_p(other))
rb_raise_zerodiv();
{
get_dat1(self);
return f_muldiv(self,
dat->num, dat->den,
other, ONE, '/');
}
}
else if (RB_TYPE_P(other, T_FLOAT))
return rb_funcall(f_to_f(self), '/', 1, other);
else if (RB_TYPE_P(other, T_RATIONAL)) {
if (f_zero_p(other))
rb_raise_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, '/');
}
}
|
#// ⇒ Object
:nodoc:
1215 1216 1217 1218 1219 |
# File 'rational.c', line 1215
static VALUE
nurat_idiv(VALUE self, VALUE other)
{
return f_idiv(self, other);
}
|
#<=>(numeric) ⇒ -1, ...
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 |
# File 'rational.c', line 1099
static VALUE
nurat_cmp(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_FIXNUM) || RB_TYPE_P(other, T_BIGNUM)) {
{
get_dat1(self);
if (FIXNUM_P(dat->den) && FIX2LONG(dat->den) == 1)
return f_cmp(dat->num, other); /* c14n */
return f_cmp(self, f_rational_new_bang1(CLASS_OF(self), other));
}
}
else if (RB_TYPE_P(other, T_FLOAT)) {
return f_cmp(f_to_f(self), other);
}
else if (RB_TYPE_P(other, 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 = f_mul(adat->num, bdat->den);
num2 = f_mul(bdat->num, adat->den);
}
return f_cmp(f_sub(num1, num2), ZERO);
}
}
else {
return rb_num_coerce_cmp(self, other, id_cmp);
}
}
|
#==(object) ⇒ Boolean
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 |
# File 'rational.c', line 1149
static VALUE
nurat_eqeq_p(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_FIXNUM) || RB_TYPE_P(other, T_BIGNUM)) {
{
get_dat1(self);
if (f_zero_p(dat->num) && f_zero_p(other))
return Qtrue;
if (!FIXNUM_P(dat->den))
return Qfalse;
if (FIX2LONG(dat->den) != 1)
return Qfalse;
if (f_eqeq_p(dat->num, other))
return Qtrue;
return Qfalse;
}
}
else if (RB_TYPE_P(other, T_FLOAT)) {
return f_eqeq_p(f_to_f(self), other);
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
{
get_dat2(self, other);
if (f_zero_p(adat->num) && f_zero_p(bdat->num))
return Qtrue;
return f_boolcast(f_eqeq_p(adat->num, bdat->num) &&
f_eqeq_p(adat->den, bdat->den));
}
}
else {
return f_eqeq_p(other, self);
}
}
|
#ceil ⇒ Integer #ceil(precision = 0) ⇒ Object
Returns the truncated value (toward positive infinity).
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
'%f' % Rational('-123.456').ceil(+1) #=> "-123.400000"
'%f' % Rational('-123.456').ceil(-1) #=> "-120.000000"
1386 1387 1388 1389 1390 |
# File 'rational.c', line 1386
static VALUE
nurat_ceil_n(int argc, VALUE *argv, VALUE self)
{
return f_round_common(argc, argv, self, nurat_ceil);
}
|
#coerce ⇒ Object
:nodoc:
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 |
# File 'rational.c', line 1188
static VALUE
nurat_coerce(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_FIXNUM) || RB_TYPE_P(other, T_BIGNUM)) {
return rb_assoc_new(f_rational_new_bang1(CLASS_OF(self), other), self);
}
else if (RB_TYPE_P(other, T_FLOAT)) {
return rb_assoc_new(other, f_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(f_rational_new_bang1
(CLASS_OF(self), RCOMPLEX(other)->real), self);
else
return rb_assoc_new(other, rb_Complex(self, INT2FIX(0)));
}
rb_raise(rb_eTypeError, "%s can't be coerced into %s",
rb_obj_classname(other), rb_obj_classname(self));
return Qnil;
}
|
#denominator ⇒ Integer
673 674 675 676 677 678 |
# File 'rational.c', line 673
static VALUE
nurat_denominator(VALUE self)
{
get_dat1(self);
return dat->den;
}
|
#exact? ⇒ Boolean
:nodoc:
1239 1240 1241 1242 1243 |
# File 'rational.c', line 1239
static VALUE
nurat_true(VALUE self)
{
return Qtrue;
}
|
#fdiv(numeric) ⇒ Float
985 986 987 988 989 990 991 |
# File 'rational.c', line 985
static VALUE
nurat_fdiv(VALUE self, VALUE other)
{
if (f_zero_p(other))
return f_div(self, f_to_f(other));
return f_to_f(f_div(self, other));
}
|
#floor ⇒ Integer #floor(precision = 0) ⇒ Object
Returns the truncated value (toward negative infinity).
Rational(3).floor #=> 3
Rational(2, 3).floor #=> 0
Rational(-3, 2).floor #=> -1
decimal - 1 2 3 . 4 5 6
^ ^ ^ ^ ^ ^
precision -3 -2 -1 0 +1 +2
'%f' % Rational('-123.456').floor(+1) #=> "-123.500000"
'%f' % Rational('-123.456').floor(-1) #=> "-130.000000"
1362 1363 1364 1365 1366 |
# File 'rational.c', line 1362
static VALUE
nurat_floor_n(int argc, VALUE *argv, VALUE self)
{
return f_round_common(argc, argv, self, nurat_floor);
}
|
#hash ⇒ Object
:nodoc:
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 |
# File 'rational.c', line 1608
static VALUE
nurat_hash(VALUE self)
{
st_index_t v, h[2];
VALUE n;
get_dat1(self);
n = rb_hash(dat->num);
h[0] = NUM2LONG(n);
n = rb_hash(dat->den);
h[1] = NUM2LONG(n);
v = rb_memhash(h, sizeof(h));
return LONG2FIX(v);
}
|
#inspect ⇒ String
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 |
# File 'rational.c', line 1662
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;
}
|
#marshal_dump ⇒ Object (private)
:nodoc:
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 |
# File 'rational.c', line 1694
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;
}
|
#numerator ⇒ Integer
654 655 656 657 658 659 |
# File 'rational.c', line 654
static VALUE
nurat_numerator(VALUE self)
{
get_dat1(self);
return dat->num;
}
|
#/(numeric) ⇒ Numeric #quo(numeric) ⇒ Numeric
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
# File 'rational.c', line 939
static VALUE
nurat_div(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_FIXNUM) || RB_TYPE_P(other, T_BIGNUM)) {
if (f_zero_p(other))
rb_raise_zerodiv();
{
get_dat1(self);
return f_muldiv(self,
dat->num, dat->den,
other, ONE, '/');
}
}
else if (RB_TYPE_P(other, T_FLOAT))
return rb_funcall(f_to_f(self), '/', 1, other);
else if (RB_TYPE_P(other, T_RATIONAL)) {
if (f_zero_p(other))
rb_raise_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, '/');
}
}
|
#quot ⇒ Object
:nodoc:
1222 1223 1224 1225 1226 |
# File 'rational.c', line 1222
static VALUE
nurat_quot(VALUE self, VALUE other)
{
return f_truncate(f_div(self, other));
}
|
#quotrem ⇒ Object
:nodoc:
1229 1230 1231 1232 1233 1234 |
# File 'rational.c', line 1229
static VALUE
nurat_quotrem(VALUE self, VALUE other)
{
VALUE val = f_truncate(f_div(self, other));
return rb_assoc_new(val, f_sub(self, f_mul(other, val)));
}
|
#rational? ⇒ Boolean
:nodoc:
1239 1240 1241 1242 1243 |
# File 'rational.c', line 1239
static VALUE
nurat_true(VALUE self)
{
return Qtrue;
}
|
#rationalize ⇒ self #rationalize(eps) ⇒ Object
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 |
# File 'rational.c', line 1584
static VALUE
nurat_rationalize(int argc, VALUE *argv, VALUE self)
{
VALUE e, a, b, p, q;
if (argc == 0)
return self;
if (f_negative_p(self))
return f_negate(nurat_rationalize(argc, argv, f_abs(self)));
rb_scan_args(argc, argv, "01", &e);
e = f_abs(e);
a = f_sub(self, e);
b = f_add(self, e);
if (f_eqeq_p(a, b))
return self;
nurat_rationalize_internal(a, b, &p, &q);
return f_rational_new2(CLASS_OF(self), p, q);
}
|
#round ⇒ Integer #round(precision = 0) ⇒ Object
Returns the truncated value (toward the nearest integer; 0.5 => 1; -0.5 => -1).
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
'%f' % Rational('-123.456').round(+1) #=> "-123.500000"
'%f' % Rational('-123.456').round(-1) #=> "-120.000000"
1435 1436 1437 1438 1439 |
# File 'rational.c', line 1435
static VALUE
nurat_round_n(int argc, VALUE *argv, VALUE self)
{
return f_round_common(argc, argv, self, nurat_round);
}
|
#to_f ⇒ Float
1452 1453 1454 1455 1456 1457 |
# File 'rational.c', line 1452
static VALUE
nurat_to_f(VALUE self)
{
get_dat1(self);
return f_fdiv(dat->num, dat->den);
}
|
#to_i ⇒ Integer
1275 1276 1277 1278 1279 1280 1281 1282 |
# File 'rational.c', line 1275
static VALUE
nurat_truncate(VALUE self)
{
get_dat1(self);
if (f_negative_p(dat->num))
return f_negate(f_idiv(f_negate(dat->num), dat->den));
return f_idiv(dat->num, dat->den);
}
|
#to_r ⇒ self
1468 1469 1470 1471 1472 |
# File 'rational.c', line 1468
static VALUE
nurat_to_r(VALUE self)
{
return self;
}
|
#to_s ⇒ String
1646 1647 1648 1649 1650 |
# File 'rational.c', line 1646
static VALUE
nurat_to_s(VALUE self)
{
return f_format(self, f_to_s);
}
|
#truncate ⇒ Integer #truncate(precision = 0) ⇒ Object
Returns the truncated value (toward zero).
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
'%f' % Rational('-123.456').truncate(+1) #=> "-123.400000"
'%f' % Rational('-123.456').truncate(-1) #=> "-120.000000"
1410 1411 1412 1413 1414 |
# File 'rational.c', line 1410
static VALUE
nurat_truncate_n(int argc, VALUE *argv, VALUE self)
{
return f_round_common(argc, argv, self, nurat_truncate);
}
|