8#include "ruby/internal/config.h"
19#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
31#include "internal/array.h"
32#include "internal/complex.h"
33#include "internal/gc.h"
34#include "internal/numeric.h"
35#include "internal/object.h"
36#include "internal/rational.h"
37#include "ruby_assert.h"
39#define ZERO INT2FIX(0)
43#define GMP_GCD_DIGITS 1
45#define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))
49static ID id_abs, id_integer_p,
55#define f_inspect rb_inspect
56#define f_to_s rb_obj_as_string
69 return rb_int_plus(x, y);
79 return rb_int_div(x, y);
89 VALUE r = rb_int_cmp(x, y);
90 if (!
NIL_P(r))
return rb_int_negative_p(r);
101 return rb_int_modulo(x, y);
111 if (y == ONE)
return x;
114 if (x == ONE)
return y;
116 return rb_int_mul(x, y);
123 if (
FIXNUM_P(y) && FIXNUM_ZERO_P(y))
132 return rb_int_abs(x);
147 return rb_str_to_inum(x, 10, 0);
157 return RTEST(rb_int_equal(x, y));
165 return rb_int_idiv(x, y);
169#define f_expt10(x) rb_int_pow(INT2FIX(10), x)
175 return FIXNUM_ZERO_P(x);
178 VALUE num = RRATIONAL(x)->num;
180 return FIXNUM_ZERO_P(num);
185#define f_nonzero_p(x) (!f_zero_p(x))
194 VALUE num = RRATIONAL(x)->num;
195 VALUE den = RRATIONAL(x)->den;
203f_minus_one_p(
VALUE x)
208 else if (RB_BIGNUM_TYPE_P(x)) {
212 VALUE num = RRATIONAL(x)->num;
213 VALUE den = RRATIONAL(x)->den;
250#define k_exact_p(x) (!k_float_p(x))
251#define k_inexact_p(x) k_float_p(x)
253#define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
254#define k_exact_one_p(x) (k_exact_p(x) && f_one_p(x))
260 const size_t nails = (
sizeof(BDIGIT)-SIZEOF_BDIGIT)*CHAR_BIT;
269 mpz_import(mx, BIGNUM_LEN(x), -1,
sizeof(BDIGIT), 0, nails, BIGNUM_DIGITS(x));
270 mpz_import(my, BIGNUM_LEN(y), -1,
sizeof(BDIGIT), 0, nails, BIGNUM_DIGITS(y));
277 zn = (mpz_sizeinbase(mz, 16) + SIZEOF_BDIGIT*2 - 1) / (SIZEOF_BDIGIT*2);
278 z = rb_big_new(zn, 1);
279 mpz_export(BIGNUM_DIGITS(z), &count, -1,
sizeof(BDIGIT), 0, nails, mz);
283 return rb_big_norm(z);
288#define f_gcd f_gcd_orig
294 unsigned long u, v, t;
307 u = (
unsigned long)x;
308 v = (
unsigned long)y;
309 for (shift = 0; ((u | v) & 1) == 0; ++shift) {
329 return (
long)(u << shift);
340 if (INT_NEGATIVE_P(x))
341 x = rb_int_uminus(x);
342 if (INT_NEGATIVE_P(y))
343 y = rb_int_uminus(y);
352 if (FIXNUM_ZERO_P(x))
358 x = rb_int_modulo(y, x);
367 return f_gcd_normal(x, y);
374 if (RB_BIGNUM_TYPE_P(x) && RB_BIGNUM_TYPE_P(y)) {
375 size_t xn = BIGNUM_LEN(x);
376 size_t yn = BIGNUM_LEN(y);
377 if (GMP_GCD_DIGITS <= xn || GMP_GCD_DIGITS <= yn)
378 return rb_gcd_gmp(x, y);
381 return f_gcd_normal(x, y);
390 VALUE r = f_gcd_orig(x, y);
391 if (f_nonzero_p(r)) {
392 assert(f_zero_p(f_mod(x, r)));
393 assert(f_zero_p(f_mod(y, r)));
402 if (INT_ZERO_P(x) || INT_ZERO_P(y))
404 return f_abs(f_mul(f_div(x, f_gcd(x, y)), y));
408 struct RRational *dat = RRATIONAL(x)
410#define get_dat2(x,y) \
411 struct RRational *adat = RRATIONAL(x), *bdat = RRATIONAL(y)
419 RATIONAL_SET_NUM((
VALUE)obj, num);
420 RATIONAL_SET_DEN((
VALUE)obj, den);
427nurat_s_alloc(
VALUE klass)
429 return nurat_s_new_internal(klass, ZERO, ONE);
435 return nurat_s_new_internal(klass, x, ONE);
439nurat_int_check(
VALUE num)
442 if (!k_numeric_p(num) || !f_integer_p(num))
448nurat_int_value(
VALUE num)
450 nurat_int_check(num);
451 if (!k_integer_p(num))
461 if (INT_NEGATIVE_P(*den)) {
462 *num = rb_int_uminus(*num);
463 *den = rb_int_uminus(*den);
465 else if (INT_ZERO_P(*den)) {
474 if (*x == ONE || *y == ONE)
return;
476 *x = f_idiv(*x, gcd);
477 *y = f_idiv(*y, gcd);
483 nurat_canonicalize(&num, &den);
484 nurat_reduce(&num, &den);
486 return nurat_s_new_internal(klass, num, den);
490nurat_s_canonicalize_internal_no_reduce(
VALUE klass,
VALUE num,
VALUE den)
492 nurat_canonicalize(&num, &den);
494 return nurat_s_new_internal(klass, num, den);
500 assert(!k_rational_p(x));
501 assert(!k_rational_p(y));
502 return nurat_s_canonicalize_internal(klass, x, y);
508 assert(!k_rational_p(x));
509 assert(!k_rational_p(y));
510 return nurat_s_canonicalize_internal_no_reduce(klass, x, y);
554nurat_f_rational(
int argc,
VALUE *argv,
VALUE klass)
559 if (
rb_scan_args(argc, argv,
"11:", &a1, &a2, &opts) == 1) {
563 raise = rb_opts_exception_p(opts, raise);
580nurat_numerator(
VALUE self)
598nurat_denominator(
VALUE self)
611rb_rational_uminus(
VALUE self)
613 const int unused = (assert(RB_TYPE_P(self,
T_RATIONAL)), 0);
616 return f_rational_new2(
CLASS_OF(self), rb_int_uminus(dat->num), dat->den);
620#define f_imul f_imul_orig
624f_imul(
long a,
long b)
628 if (a == 0 || b == 0)
635 if (MUL_OVERFLOW_LONG_P(a, b))
636 r = rb_big_mul(rb_int2big(a), rb_int2big(b));
646f_imul(
long x,
long y)
648 VALUE r = f_imul_orig(x, y);
665 long ig = i_gcd(ad, bd);
668 VALUE a = f_imul(an, bd / ig);
669 VALUE b = f_imul(bn, ad / ig);
673 c = rb_int_plus(a, b);
675 c = rb_int_minus(a, b);
677 b = rb_int_idiv(aden, g);
679 num = rb_int_idiv(c, g);
680 a = rb_int_idiv(bden, g);
681 den = rb_int_mul(a, b);
685 VALUE g = f_gcd(aden, bden);
686 VALUE a = rb_int_mul(anum, rb_int_idiv(bden, g));
687 VALUE b = rb_int_mul(bnum, rb_int_idiv(aden, g));
691 c = rb_int_plus(a, b);
693 c = rb_int_minus(a, b);
695 b = rb_int_idiv(aden, g);
697 num = rb_int_idiv(c, g);
698 a = rb_int_idiv(bden, g);
699 den = rb_int_mul(a, b);
704 double c = k ==
'+' ? a + b : a - b;
707 return f_rational_new_no_reduce2(
CLASS_OF(self), num, den);
710static double nurat_to_double(
VALUE self);
730 return f_rational_new_no_reduce2(
CLASS_OF(self),
731 rb_int_plus(dat->num, rb_int_mul(other, dat->den)),
740 get_dat2(self, other);
742 return f_addsub(self,
743 adat->num, adat->den,
744 bdat->num, bdat->den,
'+');
771 return f_rational_new_no_reduce2(
CLASS_OF(self),
772 rb_int_minus(dat->num, rb_int_mul(other, dat->den)),
781 get_dat2(self, other);
783 return f_addsub(self,
784 adat->num, adat->den,
785 bdat->num, bdat->den,
'-');
805 double x = (an * bn) / (ad * bd);
817 if (INT_NEGATIVE_P(bnum)) {
818 anum = rb_int_uminus(anum);
819 bnum = rb_int_uminus(bnum);
832 long g1 = i_gcd(an, bd);
833 long g2 = i_gcd(ad, bn);
835 num = f_imul(an / g1, bn / g2);
836 den = f_imul(ad / g2, bd / g1);
839 VALUE g1 = f_gcd(anum, bden);
840 VALUE g2 = f_gcd(aden, bnum);
842 num = rb_int_mul(rb_int_idiv(anum, g1), rb_int_idiv(bnum, g2));
843 den = rb_int_mul(rb_int_idiv(aden, g2), rb_int_idiv(bden, g1));
845 return f_rational_new_no_reduce2(
CLASS_OF(self), num, den);
867 return f_muldiv(self,
877 get_dat2(self, other);
879 return f_muldiv(self,
880 adat->num, adat->den,
881 bdat->num, bdat->den,
'*');
911 return f_muldiv(self,
917 VALUE v = nurat_to_f(self);
918 return rb_flo_div_flo(v, other);
924 get_dat2(self, other);
927 return f_rational_new_no_reduce2(
CLASS_OF(self),
928 bdat->den, bdat->num);
930 return f_muldiv(self,
931 adat->num, adat->den,
932 bdat->num, bdat->den,
'/');
955 return rb_rational_div(self, rb_float_new(0.0));
957 return nurat_to_f(self);
958 div = rb_rational_div(self, other);
960 return nurat_to_f(div);
982 if (k_numeric_p(other) && k_exact_zero_p(other))
983 return f_rational_new_bang1(
CLASS_OF(self), ONE);
985 if (k_rational_p(other)) {
988 if (f_one_p(dat->den))
993 if (k_numeric_p(other) && k_exact_p(other)) {
995 if (f_one_p(dat->den)) {
996 if (f_one_p(dat->num)) {
997 return f_rational_new_bang1(
CLASS_OF(self), ONE);
1000 return f_rational_new_bang1(
CLASS_OF(self),
INT2FIX(rb_int_odd_p(other) ? -1 : 1));
1002 else if (INT_ZERO_P(dat->num)) {
1003 if (rb_num_negative_p(other)) {
1007 return f_rational_new_bang1(
CLASS_OF(self), ZERO);
1020 if (INT_POSITIVE_P(other)) {
1021 num = rb_int_pow(dat->num, other);
1022 den = rb_int_pow(dat->den, other);
1024 else if (INT_NEGATIVE_P(other)) {
1025 num = rb_int_pow(dat->den, rb_int_uminus(other));
1026 den = rb_int_pow(dat->num, rb_int_uminus(other));
1041 return f_rational_new2(
CLASS_OF(self), num, den);
1044 else if (RB_BIGNUM_TYPE_P(other)) {
1045 rb_warn(
"in a**b, b may be too big");
1046 return rb_float_pow(nurat_to_f(self), other);
1049 return rb_float_pow(nurat_to_f(self), other);
1055#define nurat_expt rb_rational_pow
1077 switch (
TYPE(other)) {
1084 return rb_int_cmp(dat->num, other);
1085 other = f_rational_new_bang1(
CLASS_OF(self), other);
1093 get_dat2(self, other);
1101 num1 = rb_int_mul(adat->num, bdat->den);
1102 num2 = rb_int_mul(bdat->num, adat->den);
1104 return rb_int_cmp(rb_int_minus(num1, num2), ZERO);
1134 if (INT_ZERO_P(dat->num) && INT_ZERO_P(other))
1141 return rb_int_equal(dat->num, other);
1144 const double d = nurat_to_double(self);
1149 const double d = nurat_to_double(self);
1154 get_dat2(self, other);
1156 if (INT_ZERO_P(adat->num) && INT_ZERO_P(bdat->num))
1159 return RBOOL(rb_int_equal(adat->num, bdat->num) &&
1160 rb_int_equal(adat->den, bdat->den));
1173 return rb_assoc_new(f_rational_new_bang1(
CLASS_OF(self), other), self);
1176 return rb_assoc_new(other, nurat_to_f(self));
1179 return rb_assoc_new(other, self);
1182 if (!k_exact_zero_p(RCOMPLEX(other)->imag))
1183 return rb_assoc_new(other, rb_Complex(self,
INT2FIX(0)));
1184 other = RCOMPLEX(other)->real;
1186 other = float_to_r(other);
1187 RBASIC_SET_CLASS(other,
CLASS_OF(self));
1190 other = f_rational_new_bang1(
CLASS_OF(self), other);
1192 return rb_assoc_new(other, self);
1207nurat_positive_p(
VALUE self)
1210 return RBOOL(INT_POSITIVE_P(dat->num));
1220nurat_negative_p(
VALUE self)
1223 return RBOOL(INT_NEGATIVE_P(dat->num));
1239rb_rational_abs(
VALUE self)
1242 if (INT_NEGATIVE_P(dat->num)) {
1243 VALUE num = rb_int_abs(dat->num);
1244 return nurat_s_canonicalize_internal_no_reduce(
CLASS_OF(self), num, dat->den);
1250nurat_floor(
VALUE self)
1253 return rb_int_idiv(dat->num, dat->den);
1257nurat_ceil(
VALUE self)
1260 return rb_int_uminus(rb_int_idiv(rb_int_uminus(dat->num), dat->den));
1278nurat_truncate(
VALUE self)
1281 if (INT_NEGATIVE_P(dat->num))
1282 return rb_int_uminus(rb_int_idiv(rb_int_uminus(dat->num), dat->den));
1283 return rb_int_idiv(dat->num, dat->den);
1287nurat_round_half_up(
VALUE self)
1289 VALUE num, den, neg;
1295 neg = INT_NEGATIVE_P(num);
1298 num = rb_int_uminus(num);
1300 num = rb_int_plus(rb_int_mul(num, TWO), den);
1301 den = rb_int_mul(den, TWO);
1302 num = rb_int_idiv(num, den);
1305 num = rb_int_uminus(num);
1311nurat_round_half_down(
VALUE self)
1313 VALUE num, den, neg;
1319 neg = INT_NEGATIVE_P(num);
1322 num = rb_int_uminus(num);
1324 num = rb_int_plus(rb_int_mul(num, TWO), den);
1325 num = rb_int_minus(num, ONE);
1326 den = rb_int_mul(den, TWO);
1327 num = rb_int_idiv(num, den);
1330 num = rb_int_uminus(num);
1336nurat_round_half_even(
VALUE self)
1338 VALUE num, den, neg, qr;
1344 neg = INT_NEGATIVE_P(num);
1347 num = rb_int_uminus(num);
1349 num = rb_int_plus(rb_int_mul(num, TWO), den);
1350 den = rb_int_mul(den, TWO);
1351 qr = rb_int_divmod(num, den);
1354 num = rb_int_and(num,
LONG2FIX(((
int)~1)));
1357 num = rb_int_uminus(num);
1368 return (*func)(self);
1372 if (!k_integer_p(n))
1376 s = rb_rational_mul(self, b);
1379 if (INT_NEGATIVE_P(n))
1384 if (!k_rational_p(s)) {
1385 s = f_rational_new_bang1(
CLASS_OF(self), s);
1390 s = rb_rational_div(f_rational_new_bang1(
CLASS_OF(self), s), b);
1393 s = nurat_truncate(s);
1399rb_rational_floor(
VALUE self,
int ndigits)
1402 return nurat_floor(self);
1406 return f_round_common(1, &n, self, nurat_floor);
1435nurat_floor_n(
int argc,
VALUE *argv,
VALUE self)
1437 return f_round_common(argc, argv, self, nurat_floor);
1465nurat_ceil_n(
int argc,
VALUE *argv,
VALUE self)
1467 return f_round_common(argc, argv, self, nurat_ceil);
1495nurat_truncate_n(
int argc,
VALUE *argv,
VALUE self)
1497 return f_round_common(argc, argv, self, nurat_truncate);
1538nurat_round_n(
int argc,
VALUE *argv,
VALUE self)
1541 enum ruby_num_rounding_mode mode = (
1543 rb_num_get_rounding_option(opt));
1544 VALUE (*round_func)(
VALUE) = ROUND_FUNC(mode, nurat_round);
1545 return f_round_common(argc, argv, self, round_func);
1549rb_flo_round_by_rational(
int argc,
VALUE *argv,
VALUE num)
1551 return nurat_to_f(nurat_round_n(argc, argv, float_to_r(num)));
1555nurat_to_double(
VALUE self)
1561 return rb_int_fdiv_double(dat->num, dat->den);
1576nurat_to_f(
VALUE self)
1578 return DBL2NUM(nurat_to_double(self));
1591nurat_to_r(
VALUE self)
1596#define id_ceil rb_intern("ceil")
1603 return rb_float_ceil(x, 0);
1613 return rb_int_div(x, y);
1617 return rb_funcallv(x, id_quo, 1, &y);
1620#define f_reciprocal(x) f_quo(ONE, (x))
1684 VALUE c, k, t, p0, p1, p2, q0, q1, q2;
1696 p2 = f_add(f_mul(k, p1), p0);
1697 q2 = f_add(f_mul(k, q1), q0);
1698 t = f_reciprocal(f_sub(b, k));
1699 b = f_reciprocal(f_sub(a, k));
1706 *p = f_add(f_mul(c, p1), p0);
1707 *q = f_add(f_mul(c, q1), q0);
1725nurat_rationalize(
int argc,
VALUE *argv,
VALUE self)
1727 VALUE e, a, b, p, q;
1736 if (INT_NEGATIVE_P(dat->num)) {
1737 rat = f_rational_new2(
RBASIC_CLASS(self), rb_int_uminus(dat->num), dat->den);
1740 a = FIXNUM_ZERO_P(e) ? rat : rb_rational_minus(rat, e);
1741 b = FIXNUM_ZERO_P(e) ? rat : rb_rational_plus(rat, e);
1746 nurat_rationalize_internal(a, b, &p, &q);
1748 RATIONAL_SET_NUM(rat, rb_int_uminus(p));
1749 RATIONAL_SET_DEN(rat, q);
1752 return f_rational_new2(
CLASS_OF(self), p, q);
1757rb_rational_hash(
VALUE self)
1763 n = rb_hash(dat->num);
1765 n = rb_hash(dat->den);
1772nurat_hash(
VALUE self)
1774 return ST2FIX(rb_rational_hash(self));
1784 s = (*func)(dat->num);
1802nurat_to_s(
VALUE self)
1804 return f_format(self, f_to_s);
1818nurat_inspect(
VALUE self)
1831nurat_dumper(
VALUE self)
1845 nurat_int_check(num);
1846 nurat_int_check(den);
1847 nurat_canonicalize(&num, &den);
1848 RATIONAL_SET_NUM((
VALUE)dat, num);
1849 RATIONAL_SET_DEN((
VALUE)dat, den);
1857nurat_marshal_dump(
VALUE self)
1862 a = rb_assoc_new(dat->num, dat->den);
1877 rb_raise(rb_eArgError,
"marshaled rational must have an array whose length is 2 but %ld",
RARRAY_LEN(a));
1881 nurat_int_check(num);
1882 nurat_int_check(den);
1883 nurat_canonicalize(&num, &den);
1891rb_rational_reciprocal(
VALUE x)
1894 return nurat_convert(
CLASS_OF(x), dat->den, dat->num, FALSE);
1912 other = nurat_int_value(other);
1913 return f_gcd(self, other);
1931 other = nurat_int_value(other);
1932 return f_lcm(self, other);
1950 other = nurat_int_value(other);
1951 return rb_assoc_new(f_gcd(self, other), f_lcm(self, other));
1961 if (INT_NEGATIVE_P(y)) {
1962 x = rb_int_uminus(x);
1963 y = rb_int_uminus(y);
1971 return nurat_s_canonicalize_internal(
rb_cRational, x, y);
1986 return nurat_numerator(rat);
1992 return nurat_denominator(rat);
1995#define id_numerator rb_intern("numerator")
1996#define f_numerator(x) rb_funcall((x), id_numerator, 0)
1998#define id_denominator rb_intern("denominator")
1999#define f_denominator(x) rb_funcall((x), id_denominator, 0)
2001#define id_to_r idTo_r
2002#define f_to_r(x) rb_funcall((x), id_to_r, 0)
2011numeric_numerator(
VALUE self)
2013 return f_numerator(f_to_r(self));
2023numeric_denominator(
VALUE self)
2025 return f_denominator(f_to_r(self));
2041 return rb_complex_div(x, y);
2045 return rb_funcallv(x, idFdiv, 1, &y);
2049 return rb_rational_div(x, y);
2053rb_rational_canonicalize(
VALUE x)
2057 if (f_one_p(dat->den))
return dat->num;
2075rb_float_numerator(
VALUE self)
2081 r = float_to_r(self);
2082 return nurat_numerator(r);
2095rb_float_denominator(
VALUE self)
2101 r = float_to_r(self);
2102 return nurat_denominator(r);
2115nilclass_to_r(
VALUE self)
2132nilclass_rationalize(
int argc,
VALUE *argv,
VALUE self)
2135 return nilclass_to_r(self);
2148integer_to_r(
VALUE self)
2161integer_rationalize(
int argc,
VALUE *argv,
VALUE self)
2164 return integer_to_r(self);
2168float_decode_internal(
VALUE self,
VALUE *rf,
int *n)
2173 f = ldexp(f, DBL_MANT_DIG);
2175 *rf = rb_dbl2big(f);
2199float_to_r(
VALUE self)
2204 float_decode_internal(self, &f, &n);
2213 f = rb_int_mul(f, rb_int_pow(
INT2FIX(FLT_RADIX), n));
2223 VALUE e, a, b, p, q;
2230 return float_to_r(flt);
2232 nurat_rationalize_internal(a, b, &p, &q);
2239 VALUE a, b, f, p, q, den;
2242 float_decode_internal(flt, &f, &n);
2243 if (INT_ZERO_P(f) || n >= 0)
2247 VALUE radix_times_f;
2249 radix_times_f = rb_int_mul(
INT2FIX(FLT_RADIX), f);
2250#if FLT_RADIX == 2 && 0
2251 den = rb_int_lshift(ONE,
INT2FIX(1-n));
2253 den = rb_int_positive_pow(FLT_RADIX, 1-n);
2256 a = rb_int_minus(radix_times_f,
INT2FIX(FLT_RADIX - 1));
2257 b = rb_int_plus(radix_times_f,
INT2FIX(FLT_RADIX - 1));
2261 return float_to_r(flt);
2265 nurat_rationalize_internal(a, b, &p, &q);
2284float_rationalize(
int argc,
VALUE *argv,
VALUE self)
2297 if (neg) RATIONAL_SET_NUM(rat, rb_int_uminus(RRATIONAL(rat)->num));
2304 return (c ==
'-' || c ==
'+');
2308read_sign(
const char **s,
const char *
const e)
2312 if (*s < e && issign(**s)) {
2322 return (c ==
'e' || c ==
'E');
2326negate_num(
VALUE num)
2329 return rb_int_uminus(num);
2333 return rb_big_norm(num);
2338read_num(
const char **s,
const char *
const end,
VALUE *num,
VALUE *nexp)
2340 VALUE fp = ONE, exp, fn = ZERO, n = ZERO;
2341 int expsign = 0, ok = 0;
2346 if (*s < end && **s !=
'.') {
2347 n = rb_int_parse_cstr(*s, end-*s, &e, NULL,
2348 10, RB_INT_PARSE_UNDERSCORE);
2356 if (*s < end && **s ==
'.') {
2360 fp = rb_int_parse_cstr(*s, end-*s, &e, &count,
2361 10, RB_INT_PARSE_UNDERSCORE);
2367 n = n == ZERO ? fp : rb_int_plus(rb_int_mul(*num, l), fp);
2374 if (ok && *s + 1 < end && islettere(**s)) {
2376 expsign = read_sign(s, end);
2377 exp = rb_int_parse_cstr(*s, end-*s, &e, NULL,
2378 10, RB_INT_PARSE_UNDERSCORE);
2383 if (expsign ==
'-') {
2384 if (fn != ZERO) exp = rb_int_plus(exp, fn);
2387 if (fn != ZERO) exp = rb_int_minus(exp, fn);
2388 exp = negate_num(exp);
2397inline static const char *
2398skip_ws(
const char *s,
const char *e)
2400 while (s < e && isspace((
unsigned char)*s))
2406parse_rat(
const char *s,
const char *
const e,
int strict,
int raise)
2409 VALUE num, den, nexp, dexp;
2412 sign = read_sign(&s, e);
2414 if (!read_num(&s, e, &num, &nexp)) {
2415 if (strict)
return Qnil;
2419 if (s < e && *s ==
'/') {
2421 if (!read_num(&s, e, &den, &dexp)) {
2422 if (strict)
return Qnil;
2425 else if (den == ZERO) {
2426 if (!raise)
return Qnil;
2429 else if (strict && skip_ws(s, e) != e) {
2433 nexp = rb_int_minus(nexp, dexp);
2434 nurat_reduce(&num, &den);
2437 else if (strict && skip_ws(s, e) != e) {
2442 if (INT_NEGATIVE_P(nexp)) {
2447 num = rb_int_mul(num, mul);
2456 div = f_expt10(nexp);
2458 den = rb_int_mul(den, div);
2465 nurat_reduce(&num, &den);
2469 num = negate_num(num);
2476string_to_r_strict(
VALUE self,
int raise)
2482 num = parse_rat(RSTRING_PTR(self), RSTRING_END(self), 1, raise);
2484 if (!raise)
return Qnil;
2485 rb_raise(rb_eArgError,
"invalid value for convert(): %+"PRIsVALUE,
2490 if (!raise)
return Qnil;
2525string_to_r(
VALUE self)
2531 num = parse_rat(RSTRING_PTR(self), RSTRING_END(self), 0, TRUE);
2539rb_cstr_to_rat(
const char *s,
int strict)
2543 num = parse_rat(s, s + strlen(s), strict, TRUE);
2551to_rational(
VALUE val)
2553 return rb_convert_type_with_id(val,
T_RATIONAL,
"Rational", idTo_r);
2559 VALUE a1 = numv, a2 = denv;
2562 assert(!UNDEF_P(a1));
2565 if (!raise)
return Qnil;
2570 if (k_exact_zero_p(RCOMPLEX(a1)->imag))
2571 a1 = RCOMPLEX(a1)->real;
2575 if (k_exact_zero_p(RCOMPLEX(a2)->imag))
2576 a2 = RCOMPLEX(a2)->real;
2583 a1 = float_to_r(a1);
2588 else if (RB_TYPE_P(a1,
T_STRING)) {
2589 a1 = string_to_r_strict(a1, raise);
2594 rb_set_errinfo(
Qnil);
2604 a2 = float_to_r(a2);
2609 else if (RB_TYPE_P(a2,
T_STRING)) {
2610 a2 = string_to_r_strict(a2, raise);
2615 rb_set_errinfo(
Qnil);
2622 if (UNDEF_P(a2) || (k_exact_one_p(a2)))
2629 VALUE result = rb_protect(to_rational, a1, NULL);
2630 rb_set_errinfo(
Qnil);
2633 return to_rational(a1);
2637 if (!k_numeric_p(a1)) {
2639 a1 = rb_protect(to_rational, a1, &state);
2641 rb_set_errinfo(
Qnil);
2646 a1 = rb_check_convert_type_with_id(a1,
T_RATIONAL,
"Rational", idTo_r);
2649 if (!k_numeric_p(a2)) {
2651 a2 = rb_protect(to_rational, a2, &state);
2653 rb_set_errinfo(
Qnil);
2658 a2 = rb_check_convert_type_with_id(a2,
T_RATIONAL,
"Rational", idTo_r);
2661 if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
2662 (!f_integer_p(a1) || !f_integer_p(a2))) {
2663 VALUE tmp = rb_protect(to_rational, a1, &state);
2668 rb_set_errinfo(
Qnil);
2670 return f_div(a1, a2);
2674 a1 = nurat_int_value(a1);
2679 else if (!k_integer_p(a2) && !raise) {
2683 a2 = nurat_int_value(a2);
2687 return nurat_s_canonicalize_internal(klass, a1, a2);
2691nurat_s_convert(
int argc,
VALUE *argv,
VALUE klass)
2699 return nurat_convert(klass, a1, a2, TRUE);
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
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.
#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 rb_str_cat2
Old name of rb_str_cat_cstr.
#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.
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_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.
#define RGENGC_WB_PROTECTED_RATIONAL
This is a compile-time flag to enable/disable write barrier for struct RRational.
#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.
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_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_rational_raw(VALUE num, VALUE den)
Identical to rb_rational_new(), except it skips argument validations.
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.
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().
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.
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.