8 #include "ruby/internal/config.h"
19 #if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
28 #include "internal/array.h"
29 #include "internal/complex.h"
30 #include "internal/gc.h"
31 #include "internal/numeric.h"
32 #include "internal/object.h"
33 #include "internal/rational.h"
34 #include "ruby_assert.h"
45 #define ZERO INT2FIX(0)
46 #define ONE INT2FIX(1)
47 #define TWO INT2FIX(2)
49 #define GMP_GCD_DIGITS 1
51 #define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))
55 static ID id_abs, id_integer_p,
59 #define id_to_i idTo_i
61 #define f_inspect rb_inspect
62 #define f_to_s rb_obj_as_string
75 return rb_int_plus(x, y);
85 return rb_int_div(x, y);
95 VALUE r = rb_int_cmp(x, y);
96 if (!
NIL_P(r))
return rb_int_negative_p(r);
107 return rb_int_modulo(x, y);
117 if (y == ONE)
return x;
120 if (x == ONE)
return y;
122 return rb_int_mul(x, y);
129 if (
FIXNUM_P(y) && FIXNUM_ZERO_P(y))
138 return rb_int_abs(x);
163 return RTEST(rb_int_equal(x, y));
171 return rb_int_idiv(x, y);
175 #define f_expt10(x) rb_int_pow(INT2FIX(10), x)
181 return FIXNUM_ZERO_P(x);
184 VALUE num = RRATIONAL(x)->num;
186 return FIXNUM_ZERO_P(num);
191 #define f_nonzero_p(x) (!f_zero_p(x))
200 VALUE num = RRATIONAL(x)->num;
201 VALUE den = RRATIONAL(x)->den;
209 f_minus_one_p(
VALUE x)
214 else if (RB_BIGNUM_TYPE_P(x)) {
218 VALUE num = RRATIONAL(x)->num;
219 VALUE den = RRATIONAL(x)->den;
251 k_rational_p(
VALUE x)
256 #define k_exact_p(x) (!k_float_p(x))
257 #define k_inexact_p(x) k_float_p(x)
259 #define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
260 #define k_exact_one_p(x) (k_exact_p(x) && f_one_p(x))
266 const size_t nails = (
sizeof(BDIGIT)-SIZEOF_BDIGIT)*CHAR_BIT;
275 mpz_import(mx, BIGNUM_LEN(x), -1,
sizeof(BDIGIT), 0, nails, BIGNUM_DIGITS(x));
276 mpz_import(my, BIGNUM_LEN(y), -1,
sizeof(BDIGIT), 0, nails, BIGNUM_DIGITS(y));
283 zn = (mpz_sizeinbase(mz, 16) + SIZEOF_BDIGIT*2 - 1) / (SIZEOF_BDIGIT*2);
285 mpz_export(BIGNUM_DIGITS(z), &count, -1,
sizeof(BDIGIT), 0, nails, mz);
294 #define f_gcd f_gcd_orig
298 i_gcd(
long x,
long y)
300 unsigned long u, v, t;
313 u = (
unsigned long)x;
314 v = (
unsigned long)y;
315 for (shift = 0; ((u | v) & 1) == 0; ++shift) {
335 return (
long)(u << shift);
346 if (INT_NEGATIVE_P(x))
347 x = rb_int_uminus(x);
348 if (INT_NEGATIVE_P(y))
349 y = rb_int_uminus(y);
358 if (FIXNUM_ZERO_P(x))
364 x = rb_int_modulo(y, x);
373 return f_gcd_normal(x, y);
380 if (RB_BIGNUM_TYPE_P(x) && RB_BIGNUM_TYPE_P(y)) {
381 size_t xn = BIGNUM_LEN(x);
382 size_t yn = BIGNUM_LEN(y);
383 if (GMP_GCD_DIGITS <= xn || GMP_GCD_DIGITS <= yn)
384 return rb_gcd_gmp(x, y);
387 return f_gcd_normal(x, y);
396 VALUE r = f_gcd_orig(x, y);
397 if (f_nonzero_p(r)) {
408 if (INT_ZERO_P(x) || INT_ZERO_P(y))
410 return f_abs(f_mul(f_div(x, f_gcd(x, y)), y));
413 #define get_dat1(x) \
414 struct RRational *dat = RRATIONAL(x)
416 #define get_dat2(x,y) \
417 struct RRational *adat = RRATIONAL(x), *bdat = RRATIONAL(y)
425 RATIONAL_SET_NUM((
VALUE)obj, num);
426 RATIONAL_SET_DEN((
VALUE)obj, den);
433 nurat_s_alloc(
VALUE klass)
435 return nurat_s_new_internal(klass, ZERO, ONE);
441 return nurat_s_new_internal(klass, x, ONE);
445 nurat_int_check(
VALUE num)
448 if (!k_numeric_p(num) || !f_integer_p(num))
454 nurat_int_value(
VALUE num)
456 nurat_int_check(num);
457 if (!k_integer_p(num))
467 if (INT_NEGATIVE_P(*den)) {
468 *num = rb_int_uminus(*num);
469 *den = rb_int_uminus(*den);
471 else if (INT_ZERO_P(*den)) {
480 if (*x == ONE || *y == ONE)
return;
482 *x = f_idiv(*x, gcd);
483 *y = f_idiv(*y, gcd);
489 nurat_canonicalize(&num, &den);
490 nurat_reduce(&num, &den);
492 return nurat_s_new_internal(klass, num, den);
496 nurat_s_canonicalize_internal_no_reduce(
VALUE klass,
VALUE num,
VALUE den)
498 nurat_canonicalize(&num, &den);
500 return nurat_s_new_internal(klass, num, den);
508 return nurat_s_canonicalize_internal(klass, x, y);
516 return nurat_s_canonicalize_internal_no_reduce(klass, x, y);
560 nurat_f_rational(
int argc,
VALUE *argv,
VALUE klass)
565 if (
rb_scan_args(argc, argv,
"11:", &a1, &a2, &opts) == 1) {
569 raise = rb_opts_exception_p(opts,
raise);
586 nurat_numerator(
VALUE self)
604 nurat_denominator(
VALUE self)
617 rb_rational_uminus(
VALUE self)
622 return f_rational_new2(
CLASS_OF(
self), rb_int_uminus(dat->num), dat->den);
626 #define f_imul f_imul_orig
630 f_imul(
long a,
long b)
634 if (a == 0 || b == 0)
641 if (MUL_OVERFLOW_LONG_P(a, b))
652 f_imul(
long x,
long y)
654 VALUE r = f_imul_orig(x, y);
671 long ig = i_gcd(ad, bd);
674 VALUE a = f_imul(an, bd / ig);
675 VALUE b = f_imul(bn, ad / ig);
679 c = rb_int_plus(a, b);
681 c = rb_int_minus(a, b);
683 b = rb_int_idiv(aden, g);
685 num = rb_int_idiv(c, g);
686 a = rb_int_idiv(bden, g);
687 den = rb_int_mul(a, b);
691 VALUE g = f_gcd(aden, bden);
692 VALUE a = rb_int_mul(anum, rb_int_idiv(bden, g));
693 VALUE b = rb_int_mul(bnum, rb_int_idiv(aden, g));
697 c = rb_int_plus(a, b);
699 c = rb_int_minus(a, b);
701 b = rb_int_idiv(aden, g);
703 num = rb_int_idiv(c, g);
704 a = rb_int_idiv(bden, g);
705 den = rb_int_mul(a, b);
710 double c = k ==
'+' ? a + b : a - b;
713 return f_rational_new_no_reduce2(
CLASS_OF(
self), num, den);
716 static double nurat_to_double(
VALUE self);
736 return f_rational_new_no_reduce2(
CLASS_OF(
self),
737 rb_int_plus(dat->num, rb_int_mul(other, dat->den)),
746 get_dat2(
self, other);
748 return f_addsub(
self,
749 adat->num, adat->den,
750 bdat->num, bdat->den,
'+');
777 return f_rational_new_no_reduce2(
CLASS_OF(
self),
778 rb_int_minus(dat->num, rb_int_mul(other, dat->den)),
787 get_dat2(
self, other);
789 return f_addsub(
self,
790 adat->num, adat->den,
791 bdat->num, bdat->den,
'-');
811 double x = (an * bn) / (ad * bd);
823 if (INT_NEGATIVE_P(bnum)) {
824 anum = rb_int_uminus(anum);
825 bnum = rb_int_uminus(bnum);
838 long g1 = i_gcd(an, bd);
839 long g2 = i_gcd(ad, bn);
841 num = f_imul(an / g1, bn / g2);
842 den = f_imul(ad / g2, bd / g1);
845 VALUE g1 = f_gcd(anum, bden);
846 VALUE g2 = f_gcd(aden, bnum);
848 num = rb_int_mul(rb_int_idiv(anum, g1), rb_int_idiv(bnum, g2));
849 den = rb_int_mul(rb_int_idiv(aden, g2), rb_int_idiv(bden, g1));
851 return f_rational_new_no_reduce2(
CLASS_OF(
self), num, den);
873 return f_muldiv(
self,
883 get_dat2(
self, other);
885 return f_muldiv(
self,
886 adat->num, adat->den,
887 bdat->num, bdat->den,
'*');
917 return f_muldiv(
self,
923 VALUE v = nurat_to_f(
self);
924 return rb_flo_div_flo(v, other);
930 get_dat2(
self, other);
933 return f_rational_new_no_reduce2(
CLASS_OF(
self),
934 bdat->den, bdat->num);
936 return f_muldiv(
self,
937 adat->num, adat->den,
938 bdat->num, bdat->den,
'/');
963 return nurat_to_f(
self);
964 div = rb_rational_div(
self, other);
966 return nurat_to_f(div);
988 if (k_numeric_p(other) && k_exact_zero_p(other))
989 return f_rational_new_bang1(
CLASS_OF(
self), ONE);
991 if (k_rational_p(other)) {
994 if (f_one_p(dat->den))
999 if (k_numeric_p(other) && k_exact_p(other)) {
1001 if (f_one_p(dat->den)) {
1002 if (f_one_p(dat->num)) {
1003 return f_rational_new_bang1(
CLASS_OF(
self), ONE);
1006 return f_rational_new_bang1(
CLASS_OF(
self),
INT2FIX(rb_int_odd_p(other) ? -1 : 1));
1008 else if (INT_ZERO_P(dat->num)) {
1009 if (rb_num_negative_p(other)) {
1013 return f_rational_new_bang1(
CLASS_OF(
self), ZERO);
1026 if (INT_POSITIVE_P(other)) {
1027 num = rb_int_pow(dat->num, other);
1028 den = rb_int_pow(dat->den, other);
1030 else if (INT_NEGATIVE_P(other)) {
1031 num = rb_int_pow(dat->den, rb_int_uminus(other));
1032 den = rb_int_pow(dat->num, rb_int_uminus(other));
1047 return f_rational_new2(
CLASS_OF(
self), num, den);
1050 else if (RB_BIGNUM_TYPE_P(other)) {
1051 rb_warn(
"in a**b, b may be too big");
1052 return rb_float_pow(nurat_to_f(
self), other);
1055 return rb_float_pow(nurat_to_f(
self), other);
1061 #define nurat_expt rb_rational_pow
1083 switch (
TYPE(other)) {
1090 return rb_int_cmp(dat->num, other);
1091 other = f_rational_new_bang1(
CLASS_OF(
self), other);
1099 get_dat2(
self, other);
1107 num1 = rb_int_mul(adat->num, bdat->den);
1108 num2 = rb_int_mul(bdat->num, adat->den);
1110 return rb_int_cmp(rb_int_minus(num1, num2), ZERO);
1140 if (INT_ZERO_P(dat->num) && INT_ZERO_P(other))
1147 return rb_int_equal(dat->num, other);
1150 const double d = nurat_to_double(
self);
1155 const double d = nurat_to_double(
self);
1160 get_dat2(
self, other);
1162 if (INT_ZERO_P(adat->num) && INT_ZERO_P(bdat->num))
1165 return RBOOL(rb_int_equal(adat->num, bdat->num) &&
1166 rb_int_equal(adat->den, bdat->den));
1188 if (!k_exact_zero_p(RCOMPLEX(other)->imag))
1190 other = RCOMPLEX(other)->real;
1192 other = float_to_r(other);
1193 RBASIC_SET_CLASS(other,
CLASS_OF(
self));
1196 other = f_rational_new_bang1(
CLASS_OF(
self), other);
1213 nurat_positive_p(
VALUE self)
1216 return RBOOL(INT_POSITIVE_P(dat->num));
1226 nurat_negative_p(
VALUE self)
1229 return RBOOL(INT_NEGATIVE_P(dat->num));
1245 rb_rational_abs(
VALUE self)
1248 if (INT_NEGATIVE_P(dat->num)) {
1249 VALUE num = rb_int_abs(dat->num);
1250 return nurat_s_canonicalize_internal_no_reduce(
CLASS_OF(
self), num, dat->den);
1256 nurat_floor(
VALUE self)
1259 return rb_int_idiv(dat->num, dat->den);
1263 nurat_ceil(
VALUE self)
1266 return rb_int_uminus(rb_int_idiv(rb_int_uminus(dat->num), dat->den));
1284 nurat_truncate(
VALUE self)
1287 if (INT_NEGATIVE_P(dat->num))
1288 return rb_int_uminus(rb_int_idiv(rb_int_uminus(dat->num), dat->den));
1289 return rb_int_idiv(dat->num, dat->den);
1293 nurat_round_half_up(
VALUE self)
1295 VALUE num, den, neg;
1301 neg = INT_NEGATIVE_P(num);
1304 num = rb_int_uminus(num);
1306 num = rb_int_plus(rb_int_mul(num, TWO), den);
1307 den = rb_int_mul(den, TWO);
1308 num = rb_int_idiv(num, den);
1311 num = rb_int_uminus(num);
1317 nurat_round_half_down(
VALUE self)
1319 VALUE num, den, neg;
1325 neg = INT_NEGATIVE_P(num);
1328 num = rb_int_uminus(num);
1330 num = rb_int_plus(rb_int_mul(num, TWO), den);
1331 num = rb_int_minus(num, ONE);
1332 den = rb_int_mul(den, TWO);
1333 num = rb_int_idiv(num, den);
1336 num = rb_int_uminus(num);
1342 nurat_round_half_even(
VALUE self)
1344 VALUE num, den, neg, qr;
1350 neg = INT_NEGATIVE_P(num);
1353 num = rb_int_uminus(num);
1355 num = rb_int_plus(rb_int_mul(num, TWO), den);
1356 den = rb_int_mul(den, TWO);
1357 qr = rb_int_divmod(num, den);
1360 num = rb_int_and(num,
LONG2FIX(((
int)~1)));
1363 num = rb_int_uminus(num);
1374 return (*func)(
self);
1378 if (!k_integer_p(n))
1382 s = rb_rational_mul(
self, b);
1385 if (INT_NEGATIVE_P(n))
1390 if (!k_rational_p(s)) {
1391 s = f_rational_new_bang1(
CLASS_OF(
self), s);
1396 s = rb_rational_div(f_rational_new_bang1(
CLASS_OF(
self), s), b);
1399 s = nurat_truncate(s);
1405 rb_rational_floor(
VALUE self,
int ndigits)
1408 return nurat_floor(
self);
1412 return f_round_common(1, &n,
self, nurat_floor);
1441 nurat_floor_n(
int argc,
VALUE *argv,
VALUE self)
1443 return f_round_common(argc, argv,
self, nurat_floor);
1471 nurat_ceil_n(
int argc,
VALUE *argv,
VALUE self)
1473 return f_round_common(argc, argv,
self, nurat_ceil);
1501 nurat_truncate_n(
int argc,
VALUE *argv,
VALUE self)
1503 return f_round_common(argc, argv,
self, nurat_truncate);
1544 nurat_round_n(
int argc,
VALUE *argv,
VALUE self)
1547 enum ruby_num_rounding_mode mode = (
1549 rb_num_get_rounding_option(opt));
1550 VALUE (*round_func)(
VALUE) = ROUND_FUNC(mode, nurat_round);
1551 return f_round_common(argc, argv,
self, round_func);
1555 rb_flo_round_by_rational(
int argc,
VALUE *argv,
VALUE num)
1557 return nurat_to_f(nurat_round_n(argc, argv, float_to_r(num)));
1561 nurat_to_double(
VALUE self)
1567 return rb_int_fdiv_double(dat->num, dat->den);
1582 nurat_to_f(
VALUE self)
1584 return DBL2NUM(nurat_to_double(
self));
1597 nurat_to_r(
VALUE self)
1602 #define id_ceil rb_intern("ceil")
1609 return rb_float_ceil(x, 0);
1614 #define id_quo idQuo
1619 return rb_int_div(x, y);
1626 #define f_reciprocal(x) f_quo(ONE, (x))
1690 VALUE c, k, t, p0, p1, p2, q0, q1, q2;
1702 p2 = f_add(f_mul(k, p1), p0);
1703 q2 = f_add(f_mul(k, q1), q0);
1704 t = f_reciprocal(f_sub(b, k));
1705 b = f_reciprocal(f_sub(a, k));
1712 *p = f_add(f_mul(c, p1), p0);
1713 *q = f_add(f_mul(c, q1), q0);
1731 nurat_rationalize(
int argc,
VALUE *argv,
VALUE self)
1733 VALUE e, a, b, p, q;
1742 if (INT_NEGATIVE_P(dat->num)) {
1743 rat = f_rational_new2(
RBASIC_CLASS(
self), rb_int_uminus(dat->num), dat->den);
1746 a = FIXNUM_ZERO_P(e) ? rat : rb_rational_minus(rat, e);
1747 b = FIXNUM_ZERO_P(e) ? rat : rb_rational_plus(rat, e);
1752 nurat_rationalize_internal(a, b, &p, &q);
1754 RATIONAL_SET_NUM(rat, rb_int_uminus(p));
1755 RATIONAL_SET_DEN(rat, q);
1758 return f_rational_new2(
CLASS_OF(
self), p, q);
1763 rb_rational_hash(
VALUE self)
1778 nurat_hash(
VALUE self)
1780 return ST2FIX(rb_rational_hash(
self));
1790 s = (*func)(dat->num);
1808 nurat_to_s(
VALUE self)
1810 return f_format(
self, f_to_s);
1824 nurat_inspect(
VALUE self)
1837 nurat_dumper(
VALUE self)
1851 nurat_int_check(num);
1852 nurat_int_check(den);
1853 nurat_canonicalize(&num, &den);
1854 RATIONAL_SET_NUM((
VALUE)dat, num);
1855 RATIONAL_SET_DEN((
VALUE)dat, den);
1863 nurat_marshal_dump(
VALUE self)
1879 rb_check_frozen(
self);
1887 nurat_int_check(num);
1888 nurat_int_check(den);
1889 nurat_canonicalize(&num, &den);
1897 rb_rational_reciprocal(
VALUE x)
1900 return nurat_convert(
CLASS_OF(x), dat->den, dat->num, FALSE);
1918 other = nurat_int_value(other);
1919 return f_gcd(
self, other);
1937 other = nurat_int_value(other);
1938 return f_lcm(
self, other);
1956 other = nurat_int_value(other);
1957 return rb_assoc_new(f_gcd(
self, other), f_lcm(
self, other));
1967 if (INT_NEGATIVE_P(y)) {
1968 x = rb_int_uminus(x);
1969 y = rb_int_uminus(y);
1977 return nurat_s_canonicalize_internal(
rb_cRational, x, y);
1992 return nurat_numerator(rat);
1998 return nurat_denominator(rat);
2001 #define id_numerator rb_intern("numerator")
2002 #define f_numerator(x) rb_funcall((x), id_numerator, 0)
2004 #define id_denominator rb_intern("denominator")
2005 #define f_denominator(x) rb_funcall((x), id_denominator, 0)
2007 #define id_to_r idTo_r
2008 #define f_to_r(x) rb_funcall((x), id_to_r, 0)
2017 numeric_numerator(
VALUE self)
2019 return f_numerator(f_to_r(
self));
2029 numeric_denominator(
VALUE self)
2031 return f_denominator(f_to_r(
self));
2055 return rb_rational_div(x, y);
2059 rb_rational_canonicalize(
VALUE x)
2063 if (f_one_p(dat->den))
return dat->num;
2081 rb_float_numerator(
VALUE self)
2087 r = float_to_r(
self);
2088 return nurat_numerator(r);
2101 rb_float_denominator(
VALUE self)
2107 r = float_to_r(
self);
2108 return nurat_denominator(r);
2121 nilclass_to_r(
VALUE self)
2138 nilclass_rationalize(
int argc,
VALUE *argv,
VALUE self)
2141 return nilclass_to_r(
self);
2154 integer_to_r(
VALUE self)
2167 integer_rationalize(
int argc,
VALUE *argv,
VALUE self)
2170 return integer_to_r(
self);
2174 float_decode_internal(
VALUE self,
VALUE *rf,
int *n)
2179 f = ldexp(f, DBL_MANT_DIG);
2205 float_to_r(
VALUE self)
2210 float_decode_internal(
self, &f, &n);
2219 f = rb_int_mul(f, rb_int_pow(
INT2FIX(FLT_RADIX), n));
2229 VALUE e, a, b, p, q;
2236 return float_to_r(flt);
2238 nurat_rationalize_internal(a, b, &p, &q);
2245 VALUE a, b, f, p, q, den;
2248 float_decode_internal(flt, &f, &n);
2249 if (INT_ZERO_P(f) || n >= 0)
2253 VALUE radix_times_f;
2255 radix_times_f = rb_int_mul(
INT2FIX(FLT_RADIX), f);
2256 #if FLT_RADIX == 2 && 0
2257 den = rb_int_lshift(ONE,
INT2FIX(1-n));
2262 a = rb_int_minus(radix_times_f,
INT2FIX(FLT_RADIX - 1));
2263 b = rb_int_plus(radix_times_f,
INT2FIX(FLT_RADIX - 1));
2267 return float_to_r(flt);
2271 nurat_rationalize_internal(a, b, &p, &q);
2290 float_rationalize(
int argc,
VALUE *argv,
VALUE self)
2303 if (neg) RATIONAL_SET_NUM(rat, rb_int_uminus(RRATIONAL(rat)->num));
2310 return (c ==
'-' || c ==
'+');
2314 read_sign(
const char **s,
const char *
const e)
2318 if (*s < e && issign(**s)) {
2328 return (c ==
'e' || c ==
'E');
2332 negate_num(
VALUE num)
2335 return rb_int_uminus(num);
2344 read_num(
const char **s,
const char *
const end,
VALUE *num,
VALUE *nexp)
2346 VALUE fp = ONE, exp, fn = ZERO, n = ZERO;
2347 int expsign = 0, ok = 0;
2352 if (*s < end && **s !=
'.') {
2353 n = rb_int_parse_cstr(*s, end-*s, &e, NULL,
2354 10, RB_INT_PARSE_UNDERSCORE);
2362 if (*s < end && **s ==
'.') {
2366 fp = rb_int_parse_cstr(*s, end-*s, &e, &count,
2367 10, RB_INT_PARSE_UNDERSCORE);
2373 n = n == ZERO ? fp : rb_int_plus(rb_int_mul(*num, l), fp);
2380 if (ok && *s + 1 < end && islettere(**s)) {
2382 expsign = read_sign(s, end);
2383 exp = rb_int_parse_cstr(*s, end-*s, &e, NULL,
2384 10, RB_INT_PARSE_UNDERSCORE);
2389 if (expsign ==
'-') {
2390 if (fn != ZERO) exp = rb_int_plus(exp, fn);
2393 if (fn != ZERO) exp = rb_int_minus(exp, fn);
2394 exp = negate_num(exp);
2403 inline static const char *
2404 skip_ws(
const char *s,
const char *e)
2406 while (s < e && isspace((
unsigned char)*s))
2412 parse_rat(
const char *s,
const char *
const e,
int strict,
int raise)
2415 VALUE num, den, nexp, dexp;
2418 sign = read_sign(&s, e);
2420 if (!read_num(&s, e, &num, &nexp)) {
2421 if (strict)
return Qnil;
2425 if (s < e && *s ==
'/') {
2427 if (!read_num(&s, e, &den, &dexp)) {
2428 if (strict)
return Qnil;
2431 else if (den == ZERO) {
2432 if (!
raise)
return Qnil;
2435 else if (strict && skip_ws(s, e) != e) {
2439 nexp = rb_int_minus(nexp, dexp);
2440 nurat_reduce(&num, &den);
2443 else if (strict && skip_ws(s, e) != e) {
2448 if (INT_NEGATIVE_P(nexp)) {
2453 num = rb_int_mul(num, mul);
2462 div = f_expt10(nexp);
2464 den = rb_int_mul(den, div);
2471 nurat_reduce(&num, &den);
2475 num = negate_num(num);
2482 string_to_r_strict(
VALUE self,
int raise)
2490 if (!
raise)
return Qnil;
2496 if (!
raise)
return Qnil;
2531 string_to_r(
VALUE self)
2545 rb_cstr_to_rat(
const char *s,
int strict)
2549 num = parse_rat(s, s + strlen(s), strict, TRUE);
2557 to_rational(
VALUE val)
2559 return rb_convert_type_with_id(val,
T_RATIONAL,
"Rational", idTo_r);
2565 VALUE a1 = numv, a2 = denv;
2571 if (!
raise)
return Qnil;
2576 if (k_exact_zero_p(RCOMPLEX(a1)->imag))
2577 a1 = RCOMPLEX(a1)->real;
2581 if (k_exact_zero_p(RCOMPLEX(a2)->imag))
2582 a2 = RCOMPLEX(a2)->real;
2589 a1 = float_to_r(a1);
2595 a1 = string_to_r_strict(a1,
raise);
2610 a2 = float_to_r(a2);
2616 a2 = string_to_r_strict(a2,
raise);
2628 if (UNDEF_P(a2) || (k_exact_one_p(a2)))
2639 return to_rational(a1);
2643 if (!k_numeric_p(a1)) {
2652 a1 = rb_check_convert_type_with_id(a1,
T_RATIONAL,
"Rational", idTo_r);
2655 if (!k_numeric_p(a2)) {
2664 a2 = rb_check_convert_type_with_id(a2,
T_RATIONAL,
"Rational", idTo_r);
2667 if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
2668 (!f_integer_p(a1) || !f_integer_p(a2))) {
2676 return f_div(a1, a2);
2680 a1 = nurat_int_value(a1);
2685 else if (!k_integer_p(a2) && !
raise) {
2689 a2 = nurat_int_value(a2);
2693 return nurat_s_canonicalize_internal(klass, a1, a2);
2697 nurat_s_convert(
int argc,
VALUE *argv,
VALUE klass)
2705 return nurat_convert(klass, a1, a2, TRUE);
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#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 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
Old name of RB_OBJ_FREEZE.
#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_eTypeError
TypeError exception.
VALUE rb_eFloatDomainError
FloatDomainError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eArgError
ArgumentError exception.
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_check_to_int(VALUE val)
Identical to rb_check_to_integer(), except it uses #to_int for conversion.
VALUE rb_cInteger
Module class.
VALUE rb_cNilClass
NilClass class.
VALUE rb_cNumeric
Numeric class.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_cFloat
Float class.
VALUE rb_cString
String class.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
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.
#define RGENGC_WB_PROTECTED_RATIONAL
This is a compile-time flag to enable/disable write barrier for struct RRational.
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.
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.
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 ID
Type that represents a Ruby identifier such as a variable name.
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.
#define RBIMPL_WARNING_IGNORED(flag)
Suppresses a warning.
#define RBIMPL_WARNING_PUSH()
Pushes compiler warning state.
#define RBIMPL_WARNING_POP()
Pops compiler warning state.