8#include "ruby/internal/config.h"
18#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
25#include "internal/array.h"
26#include "internal/complex.h"
27#include "internal/gc.h"
28#include "internal/numeric.h"
29#include "internal/object.h"
30#include "internal/rational.h"
31#include "ruby_assert.h"
33#define ZERO INT2FIX(0)
37#define GMP_GCD_DIGITS 1
39#define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))
43static ID id_abs, id_integer_p,
49#define f_inspect rb_inspect
50#define f_to_s rb_obj_as_string
52static VALUE nurat_to_f(VALUE
self);
53static VALUE float_to_r(VALUE
self);
56f_add(VALUE x, VALUE y)
63 return rb_int_plus(x, y);
68f_div(VALUE x, VALUE y)
73 return rb_int_div(x, y);
78f_lt_p(VALUE x, VALUE y)
83 VALUE r = rb_int_cmp(x, y);
84 if (!
NIL_P(r))
return rb_int_negative_p(r);
92f_mod(VALUE x, VALUE y)
95 return rb_int_modulo(x, y);
101f_mul(VALUE x, VALUE y)
105 if (y == ONE)
return x;
108 if (x == ONE)
return y;
110 return rb_int_mul(x, y);
115f_sub(VALUE x, VALUE y)
117 if (
FIXNUM_P(y) && FIXNUM_ZERO_P(y))
126 return rb_int_abs(x);
146f_eqeq_p(VALUE x, VALUE y)
151 return RTEST(rb_int_equal(x, y));
152 return (
int)rb_equal(x, y);
156f_idiv(VALUE x, VALUE y)
159 return rb_int_idiv(x, y);
163#define f_expt10(x) rb_int_pow(INT2FIX(10), x)
169 return FIXNUM_ZERO_P(x);
172 VALUE num = RRATIONAL(x)->num;
174 return FIXNUM_ZERO_P(num);
176 return (
int)rb_equal(x, ZERO);
179#define f_nonzero_p(x) (!f_zero_p(x))
188 VALUE num = RRATIONAL(x)->num;
189 VALUE den = RRATIONAL(x)->den;
193 return (
int)rb_equal(x, ONE);
197f_minus_one_p(VALUE x)
202 else if (RB_BIGNUM_TYPE_P(x)) {
206 VALUE num = RRATIONAL(x)->num;
207 VALUE den = RRATIONAL(x)->den;
211 return (
int)rb_equal(x,
INT2FIX(-1));
215f_kind_of_p(VALUE x, VALUE c)
217 return (
int)rb_obj_is_kind_of(x, c);
244#define k_exact_p(x) (!k_float_p(x))
245#define k_inexact_p(x) k_float_p(x)
247#define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
248#define k_exact_one_p(x) (k_exact_p(x) && f_one_p(x))
252rb_gcd_gmp(VALUE x, VALUE y)
254 const size_t nails = (
sizeof(BDIGIT)-SIZEOF_BDIGIT)*CHAR_BIT;
263 mpz_import(mx, BIGNUM_LEN(x), -1,
sizeof(BDIGIT), 0, nails, BIGNUM_DIGITS(x));
264 mpz_import(my, BIGNUM_LEN(y), -1,
sizeof(BDIGIT), 0, nails, BIGNUM_DIGITS(y));
271 zn = (mpz_sizeinbase(mz, 16) + SIZEOF_BDIGIT*2 - 1) / (SIZEOF_BDIGIT*2);
273 mpz_export(BIGNUM_DIGITS(z), &count, -1,
sizeof(BDIGIT), 0, nails, mz);
282#define f_gcd f_gcd_orig
288 unsigned long u, v, t;
301 u = (
unsigned long)x;
302 v = (
unsigned long)y;
303 for (shift = 0; ((u | v) & 1) == 0; ++shift) {
323 return (
long)(u << shift);
327f_gcd_normal(VALUE x, VALUE y)
334 if (INT_NEGATIVE_P(x))
335 x = rb_int_uminus(x);
336 if (INT_NEGATIVE_P(y))
337 y = rb_int_uminus(y);
346 if (FIXNUM_ZERO_P(x))
352 x = rb_int_modulo(y, x);
359rb_gcd_normal(VALUE x, VALUE y)
361 return f_gcd_normal(x, y);
365f_gcd(VALUE x, VALUE y)
368 if (RB_BIGNUM_TYPE_P(x) && RB_BIGNUM_TYPE_P(y)) {
369 size_t xn = BIGNUM_LEN(x);
370 size_t yn = BIGNUM_LEN(y);
371 if (GMP_GCD_DIGITS <= xn || GMP_GCD_DIGITS <= yn)
372 return rb_gcd_gmp(x, y);
375 return f_gcd_normal(x, y);
382f_gcd(VALUE x, VALUE y)
384 VALUE r = f_gcd_orig(x, y);
385 if (f_nonzero_p(r)) {
386 assert(f_zero_p(f_mod(x, r)));
387 assert(f_zero_p(f_mod(y, r)));
394f_lcm(VALUE x, VALUE y)
396 if (INT_ZERO_P(x) || INT_ZERO_P(y))
398 return f_abs(f_mul(f_div(x, f_gcd(x, y)), y));
402 struct RRational *dat = RRATIONAL(x)
404#define get_dat2(x,y) \
405 struct RRational *adat = RRATIONAL(x), *bdat = RRATIONAL(y)
408nurat_s_new_internal(VALUE klass, VALUE num, VALUE den)
412 RATIONAL_SET_NUM((VALUE)obj, num);
413 RATIONAL_SET_DEN((VALUE)obj, den);
420nurat_s_alloc(VALUE klass)
422 return nurat_s_new_internal(klass, ZERO, ONE);
426f_rational_new_bang1(VALUE klass, VALUE x)
428 return nurat_s_new_internal(klass, x, ONE);
432nurat_int_check(VALUE num)
435 if (!k_numeric_p(num) || !f_integer_p(num))
436 rb_raise(rb_eTypeError,
"not an integer");
441nurat_int_value(VALUE num)
443 nurat_int_check(num);
444 if (!k_integer_p(num))
450nurat_canonicalize(VALUE *num, VALUE *den)
454 if (INT_NEGATIVE_P(*den)) {
455 *num = rb_int_uminus(*num);
456 *den = rb_int_uminus(*den);
458 else if (INT_ZERO_P(*den)) {
464nurat_reduce(VALUE *x, VALUE *y)
467 if (*x == ONE || *y == ONE)
return;
469 *x = f_idiv(*x, gcd);
470 *y = f_idiv(*y, gcd);
474nurat_s_canonicalize_internal(VALUE klass, VALUE num, VALUE den)
476 nurat_canonicalize(&num, &den);
477 nurat_reduce(&num, &den);
479 return nurat_s_new_internal(klass, num, den);
483nurat_s_canonicalize_internal_no_reduce(VALUE klass, VALUE num, VALUE den)
485 nurat_canonicalize(&num, &den);
487 return nurat_s_new_internal(klass, num, den);
491f_rational_new2(VALUE klass, VALUE x, VALUE y)
493 assert(!k_rational_p(x));
494 assert(!k_rational_p(y));
495 return nurat_s_canonicalize_internal(klass, x, y);
499f_rational_new_no_reduce2(VALUE klass, VALUE x, VALUE y)
501 assert(!k_rational_p(x));
502 assert(!k_rational_p(y));
503 return nurat_s_canonicalize_internal_no_reduce(klass, x, y);
506static VALUE nurat_convert(VALUE klass, VALUE numv, VALUE denv,
int raise);
507static VALUE nurat_s_convert(
int argc, VALUE *argv, VALUE klass);
547nurat_f_rational(
int argc, VALUE *argv, VALUE klass)
549 VALUE a1, a2, opts =
Qnil;
552 if (
rb_scan_args(argc, argv,
"11:", &a1, &a2, &opts) == 1) {
556 raise = rb_opts_exception_p(opts,
raise);
573nurat_numerator(VALUE
self)
591nurat_denominator(VALUE
self)
604rb_rational_uminus(VALUE
self)
609 return f_rational_new2(
CLASS_OF(
self), rb_int_uminus(dat->num), dat->den);
613#define f_imul f_imul_orig
617f_imul(
long a,
long b)
621 if (a == 0 || b == 0)
628 if (MUL_OVERFLOW_LONG_P(a, b))
639f_imul(
long x,
long y)
641 VALUE r = f_imul_orig(x, y);
648f_addsub(VALUE
self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden,
int k)
658 long ig = i_gcd(ad, bd);
661 VALUE a = f_imul(an, bd / ig);
662 VALUE b = f_imul(bn, ad / ig);
666 c = rb_int_plus(a, b);
668 c = rb_int_minus(a, b);
670 b = rb_int_idiv(aden, g);
672 num = rb_int_idiv(c, g);
673 a = rb_int_idiv(bden, g);
674 den = rb_int_mul(a, b);
678 VALUE g = f_gcd(aden, bden);
679 VALUE a = rb_int_mul(anum, rb_int_idiv(bden, g));
680 VALUE b = rb_int_mul(bnum, rb_int_idiv(aden, g));
684 c = rb_int_plus(a, b);
686 c = rb_int_minus(a, b);
688 b = rb_int_idiv(aden, g);
690 num = rb_int_idiv(c, g);
691 a = rb_int_idiv(bden, g);
692 den = rb_int_mul(a, b);
697 double c = k ==
'+' ? a + b : a - b;
700 return f_rational_new_no_reduce2(
CLASS_OF(
self), num, den);
703static double nurat_to_double(VALUE
self);
717rb_rational_plus(VALUE
self, VALUE other)
723 return f_rational_new_no_reduce2(
CLASS_OF(
self),
724 rb_int_plus(dat->num, rb_int_mul(other, dat->den)),
733 get_dat2(
self, other);
735 return f_addsub(
self,
736 adat->num, adat->den,
737 bdat->num, bdat->den,
'+');
758rb_rational_minus(VALUE
self, VALUE other)
764 return f_rational_new_no_reduce2(
CLASS_OF(
self),
765 rb_int_minus(dat->num, rb_int_mul(other, dat->den)),
774 get_dat2(
self, other);
776 return f_addsub(
self,
777 adat->num, adat->den,
778 bdat->num, bdat->den,
'-');
787f_muldiv(VALUE
self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden,
int k)
798 double x = (an * bn) / (ad * bd);
810 if (INT_NEGATIVE_P(bnum)) {
811 anum = rb_int_uminus(anum);
812 bnum = rb_int_uminus(bnum);
825 long g1 = i_gcd(an, bd);
826 long g2 = i_gcd(ad, bn);
828 num = f_imul(an / g1, bn / g2);
829 den = f_imul(ad / g2, bd / g1);
832 VALUE g1 = f_gcd(anum, bden);
833 VALUE g2 = f_gcd(aden, bnum);
835 num = rb_int_mul(rb_int_idiv(anum, g1), rb_int_idiv(bnum, g2));
836 den = rb_int_mul(rb_int_idiv(aden, g2), rb_int_idiv(bden, g1));
838 return f_rational_new_no_reduce2(
CLASS_OF(
self), num, den);
854rb_rational_mul(VALUE
self, VALUE other)
860 return f_muldiv(
self,
870 get_dat2(
self, other);
872 return f_muldiv(
self,
873 adat->num, adat->den,
874 bdat->num, bdat->den,
'*');
896rb_rational_div(VALUE
self, VALUE other)
904 return f_muldiv(
self,
910 VALUE v = nurat_to_f(
self);
911 return rb_flo_div_flo(v, other);
917 get_dat2(
self, other);
920 return f_rational_new_no_reduce2(
CLASS_OF(
self),
921 bdat->den, bdat->num);
923 return f_muldiv(
self,
924 adat->num, adat->den,
925 bdat->num, bdat->den,
'/');
944nurat_fdiv(VALUE
self, VALUE other)
950 return nurat_to_f(
self);
951 div = rb_rational_div(
self, other);
953 return nurat_to_f(div);
973rb_rational_pow(VALUE
self, VALUE other)
975 if (k_numeric_p(other) && k_exact_zero_p(other))
976 return f_rational_new_bang1(
CLASS_OF(
self), ONE);
978 if (k_rational_p(other)) {
981 if (f_one_p(dat->den))
986 if (k_numeric_p(other) && k_exact_p(other)) {
988 if (f_one_p(dat->den)) {
989 if (f_one_p(dat->num)) {
990 return f_rational_new_bang1(
CLASS_OF(
self), ONE);
993 return f_rational_new_bang1(
CLASS_OF(
self),
INT2FIX(rb_int_odd_p(other) ? -1 : 1));
995 else if (INT_ZERO_P(dat->num)) {
996 if (rb_num_negative_p(other)) {
1000 return f_rational_new_bang1(
CLASS_OF(
self), ZERO);
1013 if (INT_POSITIVE_P(other)) {
1014 num = rb_int_pow(dat->num, other);
1015 den = rb_int_pow(dat->den, other);
1017 else if (INT_NEGATIVE_P(other)) {
1018 num = rb_int_pow(dat->den, rb_int_uminus(other));
1019 den = rb_int_pow(dat->num, rb_int_uminus(other));
1034 return f_rational_new2(
CLASS_OF(
self), num, den);
1037 else if (RB_BIGNUM_TYPE_P(other)) {
1038 rb_warn(
"in a**b, b may be too big");
1039 return rb_float_pow(nurat_to_f(
self), other);
1042 return rb_float_pow(nurat_to_f(
self), other);
1048#define nurat_expt rb_rational_pow
1068rb_rational_cmp(VALUE
self, VALUE other)
1070 switch (
TYPE(other)) {
1077 return rb_int_cmp(dat->num, other);
1078 other = f_rational_new_bang1(
CLASS_OF(
self), other);
1086 get_dat2(
self, other);
1094 num1 = rb_int_mul(adat->num, bdat->den);
1095 num2 = rb_int_mul(bdat->num, adat->den);
1097 return rb_int_cmp(rb_int_minus(num1, num2), ZERO);
1121nurat_eqeq_p(VALUE
self, VALUE other)
1127 if (INT_ZERO_P(dat->num) && INT_ZERO_P(other))
1134 return rb_int_equal(dat->num, other);
1137 const double d = nurat_to_double(
self);
1142 const double d = nurat_to_double(
self);
1147 get_dat2(
self, other);
1149 if (INT_ZERO_P(adat->num) && INT_ZERO_P(bdat->num))
1152 return RBOOL(rb_int_equal(adat->num, bdat->num) &&
1153 rb_int_equal(adat->den, bdat->den));
1157 return rb_equal(other,
self);
1163nurat_coerce(VALUE
self, VALUE other)
1175 if (!k_exact_zero_p(RCOMPLEX(other)->imag))
1177 other = RCOMPLEX(other)->real;
1179 other = float_to_r(other);
1180 RBASIC_SET_CLASS(other,
CLASS_OF(
self));
1183 other = f_rational_new_bang1(
CLASS_OF(
self), other);
1188 rb_raise(rb_eTypeError,
"%s can't be coerced into %s",
1200nurat_positive_p(VALUE
self)
1203 return RBOOL(INT_POSITIVE_P(dat->num));
1213nurat_negative_p(VALUE
self)
1216 return RBOOL(INT_NEGATIVE_P(dat->num));
1233rb_rational_abs(VALUE
self)
1236 if (INT_NEGATIVE_P(dat->num)) {
1237 VALUE num = rb_int_abs(dat->num);
1238 return nurat_s_canonicalize_internal_no_reduce(
CLASS_OF(
self), num, dat->den);
1244nurat_floor(VALUE
self)
1247 return rb_int_idiv(dat->num, dat->den);
1251nurat_ceil(VALUE
self)
1254 return rb_int_uminus(rb_int_idiv(rb_int_uminus(dat->num), dat->den));
1272nurat_truncate(VALUE
self)
1275 if (INT_NEGATIVE_P(dat->num))
1276 return rb_int_uminus(rb_int_idiv(rb_int_uminus(dat->num), dat->den));
1277 return rb_int_idiv(dat->num, dat->den);
1281nurat_round_half_up(VALUE
self)
1283 VALUE num, den, neg;
1289 neg = INT_NEGATIVE_P(num);
1292 num = rb_int_uminus(num);
1294 num = rb_int_plus(rb_int_mul(num, TWO), den);
1295 den = rb_int_mul(den, TWO);
1296 num = rb_int_idiv(num, den);
1299 num = rb_int_uminus(num);
1305nurat_round_half_down(VALUE
self)
1307 VALUE num, den, neg;
1313 neg = INT_NEGATIVE_P(num);
1316 num = rb_int_uminus(num);
1318 num = rb_int_plus(rb_int_mul(num, TWO), den);
1319 num = rb_int_minus(num, ONE);
1320 den = rb_int_mul(den, TWO);
1321 num = rb_int_idiv(num, den);
1324 num = rb_int_uminus(num);
1330nurat_round_half_even(VALUE
self)
1332 VALUE num, den, neg, qr;
1338 neg = INT_NEGATIVE_P(num);
1341 num = rb_int_uminus(num);
1343 num = rb_int_plus(rb_int_mul(num, TWO), den);
1344 den = rb_int_mul(den, TWO);
1345 qr = rb_int_divmod(num, den);
1348 num = rb_int_and(num,
LONG2FIX(((
int)~1)));
1351 num = rb_int_uminus(num);
1357f_round_common(
int argc, VALUE *argv, VALUE
self, VALUE (*func)(VALUE))
1362 return (*func)(
self);
1366 if (!k_integer_p(n))
1367 rb_raise(rb_eTypeError,
"not an integer");
1370 s = rb_rational_mul(
self, b);
1373 if (INT_NEGATIVE_P(n))
1378 if (!k_rational_p(s)) {
1379 s = f_rational_new_bang1(
CLASS_OF(
self), s);
1384 s = rb_rational_div(f_rational_new_bang1(
CLASS_OF(
self), s), b);
1387 s = nurat_truncate(s);
1393rb_rational_floor(VALUE
self,
int ndigits)
1396 return nurat_floor(
self);
1400 return f_round_common(1, &n,
self, nurat_floor);
1429nurat_floor_n(
int argc, VALUE *argv, VALUE
self)
1431 return f_round_common(argc, argv,
self, nurat_floor);
1459nurat_ceil_n(
int argc, VALUE *argv, VALUE
self)
1461 return f_round_common(argc, argv,
self, nurat_ceil);
1489nurat_truncate_n(
int argc, VALUE *argv, VALUE
self)
1491 return f_round_common(argc, argv,
self, nurat_truncate);
1532nurat_round_n(
int argc, VALUE *argv, VALUE
self)
1535 enum ruby_num_rounding_mode mode = (
1537 rb_num_get_rounding_option(opt));
1538 VALUE (*round_func)(
VALUE) = ROUND_FUNC(mode, nurat_round);
1539 return f_round_common(argc, argv,
self, round_func);
1543rb_flo_round_by_rational(
int argc, VALUE *argv, VALUE num)
1545 return nurat_to_f(nurat_round_n(argc, argv, float_to_r(num)));
1549nurat_to_double(VALUE
self)
1555 return rb_int_fdiv_double(dat->num, dat->den);
1570nurat_to_f(VALUE
self)
1572 return DBL2NUM(nurat_to_double(
self));
1585nurat_to_r(VALUE
self)
1590#define id_ceil rb_intern("ceil")
1597 return rb_float_ceil(x, 0);
1604f_quo(VALUE x, VALUE y)
1607 return rb_int_div(x, y);
1614#define f_reciprocal(x) f_quo(ONE, (x))
1676nurat_rationalize_internal(VALUE a, VALUE b, VALUE *p, VALUE *q)
1678 VALUE c, k, t, p0, p1, p2, q0, q1, q2;
1690 p2 = f_add(f_mul(k, p1), p0);
1691 q2 = f_add(f_mul(k, q1), q0);
1692 t = f_reciprocal(f_sub(b, k));
1693 b = f_reciprocal(f_sub(a, k));
1700 *p = f_add(f_mul(c, p1), p0);
1701 *q = f_add(f_mul(c, q1), q0);
1719nurat_rationalize(
int argc, VALUE *argv, VALUE
self)
1721 VALUE e, a, b, p, q;
1730 if (INT_NEGATIVE_P(dat->num)) {
1731 rat = f_rational_new2(
RBASIC_CLASS(
self), rb_int_uminus(dat->num), dat->den);
1734 a = FIXNUM_ZERO_P(e) ? rat : rb_rational_minus(rat, e);
1735 b = FIXNUM_ZERO_P(e) ? rat : rb_rational_plus(rat, e);
1740 nurat_rationalize_internal(a, b, &p, &q);
1742 RATIONAL_SET_NUM(rat, rb_int_uminus(p));
1743 RATIONAL_SET_DEN(rat, q);
1746 return f_rational_new2(
CLASS_OF(
self), p, q);
1751rb_rational_hash(VALUE
self)
1766nurat_hash(VALUE
self)
1768 return ST2FIX(rb_rational_hash(
self));
1773f_format(VALUE
self, VALUE (*func)(VALUE))
1778 s = (*func)(dat->num);
1796nurat_to_s(VALUE
self)
1798 return f_format(
self, f_to_s);
1812nurat_inspect(VALUE
self)
1825nurat_dumper(VALUE
self)
1832nurat_loader(VALUE
self, VALUE a)
1839 nurat_int_check(num);
1840 nurat_int_check(den);
1841 nurat_canonicalize(&num, &den);
1842 RATIONAL_SET_NUM((VALUE)dat, num);
1843 RATIONAL_SET_DEN((VALUE)dat, den);
1851nurat_marshal_dump(VALUE
self)
1863nurat_marshal_load(VALUE
self, VALUE a)
1871 rb_raise(rb_eArgError,
"marshaled rational must have an array whose length is 2 but %ld",
RARRAY_LEN(a));
1875 nurat_int_check(num);
1876 nurat_int_check(den);
1877 nurat_canonicalize(&num, &den);
1885rb_rational_reciprocal(VALUE x)
1888 return nurat_convert(
CLASS_OF(x), dat->den, dat->num, FALSE);
1904rb_gcd(VALUE
self, VALUE other)
1906 other = nurat_int_value(other);
1907 return f_gcd(
self, other);
1923rb_lcm(VALUE
self, VALUE other)
1925 other = nurat_int_value(other);
1926 return f_lcm(
self, other);
1942rb_gcdlcm(VALUE
self, VALUE other)
1944 other = nurat_int_value(other);
1945 return rb_assoc_new(f_gcd(
self, other), f_lcm(
self, other));
1955 if (INT_NEGATIVE_P(y)) {
1956 x = rb_int_uminus(x);
1957 y = rb_int_uminus(y);
1965 return nurat_s_canonicalize_internal(
rb_cRational, x, y);
1980 return nurat_numerator(rat);
1986 return nurat_denominator(rat);
1989#define id_numerator rb_intern("numerator")
1990#define f_numerator(x) rb_funcall((x), id_numerator, 0)
1992#define id_denominator rb_intern("denominator")
1993#define f_denominator(x) rb_funcall((x), id_denominator, 0)
1995#define id_to_r idTo_r
1996#define f_to_r(x) rb_funcall((x), id_to_r, 0)
2005numeric_numerator(VALUE
self)
2007 return f_numerator(f_to_r(
self));
2017numeric_denominator(VALUE
self)
2019 return f_denominator(f_to_r(
self));
2032rb_numeric_quo(VALUE x, VALUE y)
2043 return rb_rational_div(x, y);
2047rb_rational_canonicalize(VALUE x)
2051 if (f_one_p(dat->den))
return dat->num;
2063integer_numerator(VALUE
self)
2075integer_denominator(VALUE
self)
2093rb_float_numerator(VALUE
self)
2099 r = float_to_r(
self);
2100 return nurat_numerator(r);
2113rb_float_denominator(VALUE
self)
2119 r = float_to_r(
self);
2120 return nurat_denominator(r);
2130nilclass_to_r(VALUE
self)
2143nilclass_rationalize(
int argc, VALUE *argv, VALUE
self)
2146 return nilclass_to_r(
self);
2159integer_to_r(VALUE
self)
2172integer_rationalize(
int argc, VALUE *argv, VALUE
self)
2175 return integer_to_r(
self);
2179float_decode_internal(VALUE
self, VALUE *rf,
int *n)
2184 f = ldexp(f, DBL_MANT_DIG);
2210float_to_r(VALUE
self)
2215 float_decode_internal(
self, &f, &n);
2224 f = rb_int_mul(f, rb_int_pow(
INT2FIX(FLT_RADIX), n));
2234 VALUE e, a, b, p, q;
2241 return float_to_r(flt);
2243 nurat_rationalize_internal(a, b, &p, &q);
2250 VALUE a, b, f, p, q, den;
2253 float_decode_internal(flt, &f, &n);
2254 if (INT_ZERO_P(f) || n >= 0)
2258 VALUE radix_times_f;
2260 radix_times_f = rb_int_mul(
INT2FIX(FLT_RADIX), f);
2261#if FLT_RADIX == 2 && 0
2262 den = rb_int_lshift(ONE,
INT2FIX(1-n));
2267 a = rb_int_minus(radix_times_f,
INT2FIX(FLT_RADIX - 1));
2268 b = rb_int_plus(radix_times_f,
INT2FIX(FLT_RADIX - 1));
2272 return float_to_r(flt);
2276 nurat_rationalize_internal(a, b, &p, &q);
2295float_rationalize(
int argc, VALUE *argv, VALUE
self)
2308 if (neg) RATIONAL_SET_NUM(rat, rb_int_uminus(RRATIONAL(rat)->num));
2315 return (c ==
'-' || c ==
'+');
2319read_sign(
const char **s,
const char *
const e)
2323 if (*s < e && issign(**s)) {
2333 return (c ==
'e' || c ==
'E');
2337negate_num(VALUE num)
2340 return rb_int_uminus(num);
2349read_num(
const char **s,
const char *
const end, VALUE *num, VALUE *nexp)
2351 VALUE fp = ONE, exp, fn = ZERO, n = ZERO;
2352 int expsign = 0, ok = 0;
2357 if (*s < end && **s !=
'.') {
2358 n = rb_int_parse_cstr(*s, end-*s, &e, NULL,
2359 10, RB_INT_PARSE_UNDERSCORE);
2367 if (*s < end && **s ==
'.') {
2371 fp = rb_int_parse_cstr(*s, end-*s, &e, &count,
2372 10, RB_INT_PARSE_UNDERSCORE);
2377 VALUE l = f_expt10(*nexp =
SIZET2NUM(count));
2378 n = n == ZERO ? fp : rb_int_plus(rb_int_mul(*num, l), fp);
2385 if (ok && *s + 1 < end && islettere(**s)) {
2387 expsign = read_sign(s, end);
2388 exp = rb_int_parse_cstr(*s, end-*s, &e, NULL,
2389 10, RB_INT_PARSE_UNDERSCORE);
2394 if (expsign ==
'-') {
2395 if (fn != ZERO) exp = rb_int_plus(exp, fn);
2398 if (fn != ZERO) exp = rb_int_minus(exp, fn);
2399 exp = negate_num(exp);
2408inline static const char *
2409skip_ws(
const char *s,
const char *e)
2411 while (s < e && isspace((
unsigned char)*s))
2417parse_rat(
const char *s,
const char *
const e,
int strict,
int raise)
2420 VALUE num, den, nexp, dexp;
2423 sign = read_sign(&s, e);
2425 if (!read_num(&s, e, &num, &nexp)) {
2426 if (strict)
return Qnil;
2430 if (s < e && *s ==
'/') {
2432 if (!read_num(&s, e, &den, &dexp)) {
2433 if (strict)
return Qnil;
2436 else if (den == ZERO) {
2437 if (!
raise)
return Qnil;
2440 else if (strict && skip_ws(s, e) != e) {
2444 nexp = rb_int_minus(nexp, dexp);
2445 nurat_reduce(&num, &den);
2448 else if (strict && skip_ws(s, e) != e) {
2453 if (INT_NEGATIVE_P(nexp)) {
2458 num = rb_int_mul(num, mul);
2467 div = f_expt10(nexp);
2469 den = rb_int_mul(den, div);
2476 nurat_reduce(&num, &den);
2480 num = negate_num(num);
2487string_to_r_strict(VALUE
self,
int raise)
2495 if (!
raise)
return Qnil;
2496 rb_raise(rb_eArgError,
"invalid value for convert(): %+"PRIsVALUE,
2501 if (!
raise)
return Qnil;
2536string_to_r(VALUE
self)
2550rb_cstr_to_rat(
const char *s,
int strict)
2554 num = parse_rat(s, s + strlen(s), strict, TRUE);
2562to_rational(VALUE val)
2564 return rb_convert_type_with_id(val,
T_RATIONAL,
"Rational", idTo_r);
2568nurat_convert(VALUE klass, VALUE numv, VALUE denv,
int raise)
2570 VALUE a1 = numv, a2 = denv;
2576 if (!
raise)
return Qnil;
2577 rb_raise(rb_eTypeError,
"can't convert nil into Rational");
2581 if (k_exact_zero_p(RCOMPLEX(a1)->imag))
2582 a1 = RCOMPLEX(a1)->real;
2586 if (k_exact_zero_p(RCOMPLEX(a2)->imag))
2587 a2 = RCOMPLEX(a2)->real;
2594 a1 = float_to_r(a1);
2600 a1 = string_to_r_strict(a1,
raise);
2604 VALUE tmp =
rb_protect(rb_check_to_int, a1, NULL);
2615 a2 = float_to_r(a2);
2621 a2 = string_to_r_strict(a2,
raise);
2625 VALUE tmp =
rb_protect(rb_check_to_int, a2, NULL);
2633 if (a2 ==
Qundef || (k_exact_one_p(a2)))
2640 VALUE result =
rb_protect(to_rational, a1, NULL);
2644 return to_rational(a1);
2648 if (!k_numeric_p(a1)) {
2657 a1 = rb_check_convert_type_with_id(a1,
T_RATIONAL,
"Rational", idTo_r);
2660 if (!k_numeric_p(a2)) {
2669 a2 = rb_check_convert_type_with_id(a2,
T_RATIONAL,
"Rational", idTo_r);
2672 if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
2673 (!f_integer_p(a1) || !f_integer_p(a2))) {
2674 VALUE tmp =
rb_protect(to_rational, a1, &state);
2681 return f_div(a1, a2);
2685 a1 = nurat_int_value(a1);
2690 else if (!k_integer_p(a2) && !
raise) {
2694 a2 = nurat_int_value(a2);
2698 return nurat_s_canonicalize_internal(klass, a1, a2);
2702nurat_s_convert(
int argc, VALUE *argv, VALUE klass)
2710 return nurat_convert(klass, a1, a2, TRUE);
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
VALUE rb_float_new(double d)
Converts a C's double into an instance of rb_cFloat.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define NEWOBJ_OF
Old name of RB_NEWOBJ_OF.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define OBJ_FREEZE_RAW
Old name of RB_OBJ_FREEZE_RAW.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define CLASS_OF
Old name of rb_class_of.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define NUM2DBL
Old name of rb_num2dbl.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define DBL2NUM
Old name of rb_float_new.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
VALUE rb_eFloatDomainError
FloatDomainError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports always regardless of runtime -W flag.
VALUE rb_cRational
Rational class.
VALUE rb_convert_type(VALUE val, int type, const char *name, const char *mid)
Converts an object into another type.
VALUE rb_cInteger
Module class.
VALUE rb_cNumeric
Numeric class.
VALUE rb_cFloat
Float class.
VALUE rb_cString
String class.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
VALUE rb_big_new(size_t len, int sign)
Allocates a bignum object.
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
Identical to rb_cstr2inum(), except it takes Ruby's strings instead of C's.
VALUE rb_dbl2big(double d)
Converts a C's double into a bignum.
VALUE rb_big_mul(VALUE x, VALUE y)
Performs multiplication of the passed two objects.
VALUE rb_big_norm(VALUE x)
Normalises the passed bignum.
VALUE rb_complex_div(VALUE x, VALUE y)
Performs division of the passed two objects.
VALUE rb_Complex(VALUE real, VALUE imag)
Converts various values into a Complex.
#define rb_check_frozen
Just another name of rb_check_frozen.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash(VALUE obj)
Calculates a message authentication code of the passed object.
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
void rb_num_zerodiv(void)
Just always raises an exception.
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
VALUE rb_dbl_cmp(double lhs, double rhs)
Compares two doubles.
VALUE rb_num_coerce_cmp(VALUE lhs, VALUE rhs, ID op)
Identical to rb_num_coerce_bin(), except for return values.
VALUE rb_num_coerce_bin(VALUE lhs, VALUE rhs, ID op)
Coerced binary operation.
VALUE rb_protect(VALUE(*func)(VALUE args), VALUE args, int *state)
Protects a function call from potential global escapes from the function.
VALUE rb_rational_raw(VALUE num, VALUE den)
Identical to rb_rational_new(), except it skips argument validations.
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
VALUE rb_Rational(VALUE num, VALUE den)
Converts various values into a Rational.
VALUE rb_rational_num(VALUE rat)
Queries the numerator of the passed Rational.
VALUE rb_flt_rationalize(VALUE flt)
Identical to rb_flt_rationalize_with_prec(), except it auto-detects appropriate precision depending o...
VALUE rb_flt_rationalize_with_prec(VALUE flt, VALUE prec)
Simplified approximation of a float.
#define rb_rational_new2(x, y)
Just another name of rb_rational_new.
#define rb_rational_new1(x)
Shorthand of (x/1)r.
VALUE rb_rational_den(VALUE rat)
Queries the denominator of the passed Rational.
st_index_t rb_memhash(const void *ptr, long len)
This is a universal hash function.
VALUE rb_str_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_int2big(intptr_t i)
Converts a C's intptr_t into an instance of rb_cInteger.
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RGENGC_WB_PROTECTED_RATIONAL
This is a compile-time flag to enable/disable write barrier for struct RRational.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define RTEST
This is an old name of RB_TEST.
Internal header for Rational.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.