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/error.h"
31#include "internal/gc.h"
32#include "internal/numeric.h"
33#include "internal/object.h"
34#include "internal/rational.h"
35#include "ruby_assert.h"
46#define ZERO INT2FIX(0)
50#define GMP_GCD_DIGITS 1
52#define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))
56static ID id_abs, id_integer_p,
62#define f_inspect rb_inspect
63#define f_to_s rb_obj_as_string
76 return rb_int_plus(x, y);
86 return rb_int_div(x, y);
96 VALUE r = rb_int_cmp(x, y);
97 if (!
NIL_P(r))
return rb_int_negative_p(r);
108 return rb_int_modulo(x, y);
118 if (y == ONE)
return x;
121 if (x == ONE)
return y;
123 return rb_int_mul(x, y);
130 if (
FIXNUM_P(y) && FIXNUM_ZERO_P(y))
139 return rb_int_abs(x);
154 return rb_str_to_inum(x, 10, 0);
164 return RTEST(rb_int_equal(x, y));
172 return rb_int_idiv(x, y);
176#define f_expt10(x) rb_int_pow(INT2FIX(10), x)
185 VALUE num = RRATIONAL(x)->num;
186 VALUE den = RRATIONAL(x)->den;
194f_minus_one_p(
VALUE x)
199 else if (RB_BIGNUM_TYPE_P(x)) {
203 VALUE num = RRATIONAL(x)->num;
204 VALUE den = RRATIONAL(x)->den;
241#define k_exact_p(x) (!k_float_p(x))
242#define k_inexact_p(x) k_float_p(x)
244#define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
245#define k_exact_one_p(x) (k_exact_p(x) && f_one_p(x))
251 const size_t nails = (
sizeof(BDIGIT)-SIZEOF_BDIGIT)*CHAR_BIT;
260 mpz_import(mx, BIGNUM_LEN(x), -1,
sizeof(BDIGIT), 0, nails, BIGNUM_DIGITS(x));
261 mpz_import(my, BIGNUM_LEN(y), -1,
sizeof(BDIGIT), 0, nails, BIGNUM_DIGITS(y));
268 zn = (mpz_sizeinbase(mz, 16) + SIZEOF_BDIGIT*2 - 1) / (SIZEOF_BDIGIT*2);
269 z = rb_big_new(zn, 1);
270 mpz_export(BIGNUM_DIGITS(z), &count, -1,
sizeof(BDIGIT), 0, nails, mz);
274 return rb_big_norm(z);
279#define f_gcd f_gcd_orig
285 unsigned long u, v, t;
298 u = (
unsigned long)x;
299 v = (
unsigned long)y;
300 for (shift = 0; ((u | v) & 1) == 0; ++shift) {
320 return (
long)(u << shift);
331 if (INT_NEGATIVE_P(x))
332 x = rb_int_uminus(x);
333 if (INT_NEGATIVE_P(y))
334 y = rb_int_uminus(y);
343 if (FIXNUM_ZERO_P(x))
349 x = rb_int_modulo(y, x);
358 return f_gcd_normal(x, y);
365 if (RB_BIGNUM_TYPE_P(x) && RB_BIGNUM_TYPE_P(y)) {
366 size_t xn = BIGNUM_LEN(x);
367 size_t yn = BIGNUM_LEN(y);
368 if (GMP_GCD_DIGITS <= xn || GMP_GCD_DIGITS <= yn)
369 return rb_gcd_gmp(x, y);
372 return f_gcd_normal(x, y);
381 VALUE r = f_gcd_orig(x, y);
382 if (f_nonzero_p(r)) {
393 if (INT_ZERO_P(x) || INT_ZERO_P(y))
395 return f_abs(f_mul(f_div(x, f_gcd(x, y)), y));
399 struct RRational *dat = RRATIONAL(x)
401#define get_dat2(x,y) \
402 struct RRational *adat = RRATIONAL(x), *bdat = RRATIONAL(y)
409 RATIONAL_SET_NUM((
VALUE)obj, num);
410 RATIONAL_SET_DEN((
VALUE)obj, den);
417nurat_s_alloc(
VALUE klass)
419 return nurat_s_new_internal(klass, ZERO, ONE);
425 return nurat_s_new_internal(klass, x, ONE);
429nurat_int_check(
VALUE num)
432 if (!k_numeric_p(num) || !f_integer_p(num))
438nurat_int_value(
VALUE num)
440 nurat_int_check(num);
441 if (!k_integer_p(num))
451 if (INT_NEGATIVE_P(*den)) {
452 *num = rb_int_uminus(*num);
453 *den = rb_int_uminus(*den);
455 else if (INT_ZERO_P(*den)) {
464 if (*x == ONE || *y == ONE)
return;
466 *x = f_idiv(*x, gcd);
467 *y = f_idiv(*y, gcd);
473 nurat_canonicalize(&num, &den);
474 nurat_reduce(&num, &den);
476 return nurat_s_new_internal(klass, num, den);
480nurat_s_canonicalize_internal_no_reduce(
VALUE klass,
VALUE num,
VALUE den)
482 nurat_canonicalize(&num, &den);
484 return nurat_s_new_internal(klass, num, den);
492 return nurat_s_canonicalize_internal(klass, x, y);
500 return nurat_s_canonicalize_internal_no_reduce(klass, x, y);
544nurat_f_rational(
int argc,
VALUE *argv,
VALUE klass)
549 if (
rb_scan_args(argc, argv,
"11:", &a1, &a2, &opts) == 1) {
553 raise = rb_opts_exception_p(opts, raise);
570nurat_numerator(
VALUE self)
588nurat_denominator(
VALUE self)
605rb_rational_uminus(
VALUE self)
610 return f_rational_new2(
CLASS_OF(self), rb_int_uminus(dat->num), dat->den);
614#define f_imul f_imul_orig
618f_imul(
long a,
long b)
622 if (a == 0 || b == 0)
629 if (MUL_OVERFLOW_LONG_P(a, b))
630 r = rb_big_mul(rb_int2big(a), rb_int2big(b));
640f_imul(
long x,
long y)
642 VALUE r = f_imul_orig(x, y);
659 long ig = i_gcd(ad, bd);
662 VALUE a = f_imul(an, bd / ig);
663 VALUE b = f_imul(bn, ad / ig);
667 c = rb_int_plus(a, b);
669 c = rb_int_minus(a, b);
671 b = rb_int_idiv(aden, g);
673 num = rb_int_idiv(c, g);
674 a = rb_int_idiv(bden, g);
675 den = rb_int_mul(a, b);
679 VALUE g = f_gcd(aden, bden);
680 VALUE a = rb_int_mul(anum, rb_int_idiv(bden, g));
681 VALUE b = rb_int_mul(bnum, rb_int_idiv(aden, g));
685 c = rb_int_plus(a, b);
687 c = rb_int_minus(a, b);
689 b = rb_int_idiv(aden, g);
691 num = rb_int_idiv(c, g);
692 a = rb_int_idiv(bden, g);
693 den = rb_int_mul(a, b);
698 double c = k ==
'+' ? a + b : a - b;
701 return f_rational_new_no_reduce2(
CLASS_OF(self), num, den);
704static double nurat_to_double(
VALUE self);
735 return f_rational_new_no_reduce2(
CLASS_OF(self),
736 rb_int_plus(dat->num, rb_int_mul(other, dat->den)),
745 get_dat2(self, other);
747 return f_addsub(self,
748 adat->num, adat->den,
749 bdat->num, bdat->den,
'+');
776 return f_rational_new_no_reduce2(
CLASS_OF(self),
777 rb_int_minus(dat->num, rb_int_mul(other, dat->den)),
786 get_dat2(self, other);
788 return f_addsub(self,
789 adat->num, adat->den,
790 bdat->num, bdat->den,
'-');
810 double x = (an * bn) / (ad * bd);
822 if (INT_NEGATIVE_P(bnum)) {
823 anum = rb_int_uminus(anum);
824 bnum = rb_int_uminus(bnum);
837 long g1 = i_gcd(an, bd);
838 long g2 = i_gcd(ad, bn);
840 num = f_imul(an / g1, bn / g2);
841 den = f_imul(ad / g2, bd / g1);
844 VALUE g1 = f_gcd(anum, bden);
845 VALUE g2 = f_gcd(aden, bnum);
847 num = rb_int_mul(rb_int_idiv(anum, g1), rb_int_idiv(bnum, g2));
848 den = rb_int_mul(rb_int_idiv(aden, g2), rb_int_idiv(bden, g1));
850 return f_rational_new_no_reduce2(
CLASS_OF(self), num, den);
874 return f_muldiv(self,
884 get_dat2(self, other);
886 return f_muldiv(self,
887 adat->num, adat->den,
888 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,
'/');
961 return rb_rational_div(self, rb_float_new(0.0));
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_raise(rb_eArgError,
"exponent is too large");
1054 return rb_float_pow(nurat_to_f(self), other);
1060#define nurat_expt rb_rational_pow
1092 switch (
TYPE(other)) {
1099 return rb_int_cmp(dat->num, other);
1100 other = f_rational_new_bang1(
CLASS_OF(self), other);
1108 get_dat2(self, other);
1116 num1 = rb_int_mul(adat->num, bdat->den);
1117 num2 = rb_int_mul(bdat->num, adat->den);
1119 return rb_int_cmp(rb_int_minus(num1, num2), ZERO);
1149 if (INT_ZERO_P(dat->num) && INT_ZERO_P(other))
1156 return rb_int_equal(dat->num, other);
1159 const double d = nurat_to_double(self);
1164 const double d = nurat_to_double(self);
1169 get_dat2(self, other);
1171 if (INT_ZERO_P(adat->num) && INT_ZERO_P(bdat->num))
1174 return RBOOL(rb_int_equal(adat->num, bdat->num) &&
1175 rb_int_equal(adat->den, bdat->den));
1197 if (!k_exact_zero_p(RCOMPLEX(other)->imag))
1199 other = RCOMPLEX(other)->real;
1201 other = float_to_r(other);
1202 RBASIC_SET_CLASS(other,
CLASS_OF(self));
1205 other = f_rational_new_bang1(
CLASS_OF(self), other);
1222nurat_positive_p(
VALUE self)
1225 return RBOOL(INT_POSITIVE_P(dat->num));
1235nurat_negative_p(
VALUE self)
1238 return RBOOL(INT_NEGATIVE_P(dat->num));
1254rb_rational_abs(
VALUE self)
1257 if (INT_NEGATIVE_P(dat->num)) {
1258 VALUE num = rb_int_abs(dat->num);
1259 return nurat_s_canonicalize_internal_no_reduce(
CLASS_OF(self), num, dat->den);
1265nurat_floor(
VALUE self)
1268 return rb_int_idiv(dat->num, dat->den);
1272nurat_ceil(
VALUE self)
1275 return rb_int_uminus(rb_int_idiv(rb_int_uminus(dat->num), dat->den));
1293nurat_truncate(
VALUE self)
1296 if (INT_NEGATIVE_P(dat->num))
1297 return rb_int_uminus(rb_int_idiv(rb_int_uminus(dat->num), dat->den));
1298 return rb_int_idiv(dat->num, dat->den);
1302nurat_round_half_up(
VALUE self)
1304 VALUE num, den, neg;
1310 neg = INT_NEGATIVE_P(num);
1313 num = rb_int_uminus(num);
1315 num = rb_int_plus(rb_int_mul(num, TWO), den);
1316 den = rb_int_mul(den, TWO);
1317 num = rb_int_idiv(num, den);
1320 num = rb_int_uminus(num);
1326nurat_round_half_down(
VALUE self)
1328 VALUE num, den, neg;
1334 neg = INT_NEGATIVE_P(num);
1337 num = rb_int_uminus(num);
1339 num = rb_int_plus(rb_int_mul(num, TWO), den);
1340 num = rb_int_minus(num, ONE);
1341 den = rb_int_mul(den, TWO);
1342 num = rb_int_idiv(num, den);
1345 num = rb_int_uminus(num);
1351nurat_round_half_even(
VALUE self)
1353 VALUE num, den, neg, qr;
1359 neg = INT_NEGATIVE_P(num);
1362 num = rb_int_uminus(num);
1364 num = rb_int_plus(rb_int_mul(num, TWO), den);
1365 den = rb_int_mul(den, TWO);
1366 qr = rb_int_divmod(num, den);
1369 num = rb_int_and(num,
LONG2FIX(((
int)~1)));
1372 num = rb_int_uminus(num);
1383 return (*func)(self);
1387 if (!k_integer_p(n))
1391 s = rb_rational_mul(self, b);
1394 if (INT_NEGATIVE_P(n))
1399 if (!k_rational_p(s)) {
1400 s = f_rational_new_bang1(
CLASS_OF(self), s);
1405 s = rb_rational_div(f_rational_new_bang1(
CLASS_OF(self), s), b);
1408 s = nurat_truncate(s);
1414rb_rational_floor(
VALUE self,
int ndigits)
1417 return nurat_floor(self);
1421 return f_round_common(1, &n, self, nurat_floor);
1450nurat_floor_n(
int argc,
VALUE *argv,
VALUE self)
1452 return f_round_common(argc, argv, self, nurat_floor);
1480nurat_ceil_n(
int argc,
VALUE *argv,
VALUE self)
1482 return f_round_common(argc, argv, self, nurat_ceil);
1510nurat_truncate_n(
int argc,
VALUE *argv,
VALUE self)
1512 return f_round_common(argc, argv, self, nurat_truncate);
1553nurat_round_n(
int argc,
VALUE *argv,
VALUE self)
1556 enum ruby_num_rounding_mode mode = (
1558 rb_num_get_rounding_option(opt));
1559 VALUE (*round_func)(
VALUE) = ROUND_FUNC(mode, nurat_round);
1560 return f_round_common(argc, argv, self, round_func);
1564rb_flo_round_by_rational(
int argc,
VALUE *argv,
VALUE num)
1566 return nurat_to_f(nurat_round_n(argc, argv, float_to_r(num)));
1570nurat_to_double(
VALUE self)
1576 return rb_int_fdiv_double(dat->num, dat->den);
1591nurat_to_f(
VALUE self)
1593 return DBL2NUM(nurat_to_double(self));
1606nurat_to_r(
VALUE self)
1611#define id_ceil rb_intern("ceil")
1618 return rb_float_ceil(x, 0);
1628 return rb_int_div(x, y);
1632 return rb_funcallv(x, id_quo, 1, &y);
1635#define f_reciprocal(x) f_quo(ONE, (x))
1699 VALUE c, k, t, p0, p1, p2, q0, q1, q2;
1711 p2 = f_add(f_mul(k, p1), p0);
1712 q2 = f_add(f_mul(k, q1), q0);
1713 t = f_reciprocal(f_sub(b, k));
1714 b = f_reciprocal(f_sub(a, k));
1721 *p = f_add(f_mul(c, p1), p0);
1722 *q = f_add(f_mul(c, q1), q0);
1740nurat_rationalize(
int argc,
VALUE *argv,
VALUE self)
1742 VALUE e, a, b, p, q;
1751 if (INT_NEGATIVE_P(dat->num)) {
1752 rat = f_rational_new2(
RBASIC_CLASS(self), rb_int_uminus(dat->num), dat->den);
1755 a = FIXNUM_ZERO_P(e) ? rat : rb_rational_minus(rat, e);
1756 b = FIXNUM_ZERO_P(e) ? rat : rb_rational_plus(rat, e);
1761 nurat_rationalize_internal(a, b, &p, &q);
1763 RATIONAL_SET_NUM(rat, rb_int_uminus(p));
1764 RATIONAL_SET_DEN(rat, q);
1767 return f_rational_new2(
CLASS_OF(self), p, q);
1772rb_rational_hash(
VALUE self)
1778 n = rb_hash(dat->num);
1780 n = rb_hash(dat->den);
1787nurat_hash(
VALUE self)
1789 return ST2FIX(rb_rational_hash(self));
1799 s = (*func)(dat->num);
1817nurat_to_s(
VALUE self)
1819 return f_format(self, f_to_s);
1833nurat_inspect(
VALUE self)
1846nurat_dumper(
VALUE self)
1860 nurat_int_check(num);
1861 nurat_int_check(den);
1862 nurat_canonicalize(&num, &den);
1863 RATIONAL_SET_NUM((
VALUE)dat, num);
1864 RATIONAL_SET_DEN((
VALUE)dat, den);
1872nurat_marshal_dump(
VALUE self)
1888 rb_check_frozen(self);
1892 rb_raise(rb_eArgError,
"marshaled rational must have an array whose length is 2 but %ld",
RARRAY_LEN(a));
1896 nurat_int_check(num);
1897 nurat_int_check(den);
1898 nurat_canonicalize(&num, &den);
1906rb_rational_reciprocal(
VALUE x)
1909 return nurat_convert(
CLASS_OF(x), dat->den, dat->num, FALSE);
1927 other = nurat_int_value(other);
1928 return f_gcd(self, other);
1946 other = nurat_int_value(other);
1947 return f_lcm(self, other);
1965 other = nurat_int_value(other);
1966 return rb_assoc_new(f_gcd(self, other), f_lcm(self, other));
1976 if (INT_NEGATIVE_P(y)) {
1977 x = rb_int_uminus(x);
1978 y = rb_int_uminus(y);
1986 return nurat_s_canonicalize_internal(
rb_cRational, x, y);
2001 return nurat_numerator(rat);
2007 return nurat_denominator(rat);
2010#define id_numerator rb_intern("numerator")
2011#define f_numerator(x) rb_funcall((x), id_numerator, 0)
2013#define id_denominator rb_intern("denominator")
2014#define f_denominator(x) rb_funcall((x), id_denominator, 0)
2016#define id_to_r idTo_r
2017#define f_to_r(x) rb_funcall((x), id_to_r, 0)
2026numeric_numerator(
VALUE self)
2028 return f_numerator(f_to_r(self));
2038numeric_denominator(
VALUE self)
2040 return f_denominator(f_to_r(self));
2056 return rb_complex_div(x, y);
2060 return rb_funcallv(x, idFdiv, 1, &y);
2064 return rb_rational_div(x, y);
2068rb_rational_canonicalize(
VALUE x)
2072 if (f_one_p(dat->den))
return dat->num;
2090rb_float_numerator(
VALUE self)
2096 r = float_to_r(self);
2097 return nurat_numerator(r);
2110rb_float_denominator(
VALUE self)
2116 r = float_to_r(self);
2117 return nurat_denominator(r);
2130integer_to_r(
VALUE self)
2143integer_rationalize(
int argc,
VALUE *argv,
VALUE self)
2146 return integer_to_r(self);
2150float_decode_internal(
VALUE self,
VALUE *rf,
int *n)
2155 f = ldexp(f, DBL_MANT_DIG);
2157 *rf = rb_dbl2big(f);
2181float_to_r(
VALUE self)
2186 float_decode_internal(self, &f, &n);
2195 f = rb_int_mul(f, rb_int_pow(
INT2FIX(FLT_RADIX), n));
2205 VALUE e, a, b, p, q;
2212 return float_to_r(flt);
2214 nurat_rationalize_internal(a, b, &p, &q);
2221 VALUE a, b, f, p, q, den;
2224 float_decode_internal(flt, &f, &n);
2225 if (INT_ZERO_P(f) || n >= 0)
2229 VALUE radix_times_f;
2231 radix_times_f = rb_int_mul(
INT2FIX(FLT_RADIX), f);
2232#if FLT_RADIX == 2 && 0
2233 den = rb_int_lshift(ONE,
INT2FIX(1-n));
2238 a = rb_int_minus(radix_times_f,
INT2FIX(FLT_RADIX - 1));
2239 b = rb_int_plus(radix_times_f,
INT2FIX(FLT_RADIX - 1));
2243 return float_to_r(flt);
2247 nurat_rationalize_internal(a, b, &p, &q);
2266float_rationalize(
int argc,
VALUE *argv,
VALUE self)
2279 if (neg) RATIONAL_SET_NUM(rat, rb_int_uminus(RRATIONAL(rat)->num));
2286 return (c ==
'-' || c ==
'+');
2290read_sign(
const char **s,
const char *
const e)
2294 if (*s < e && issign(**s)) {
2304 return (c ==
'e' || c ==
'E');
2310 return (c ==
'r' || c ==
'R');
2314negate_num(
VALUE num)
2317 return rb_int_uminus(num);
2321 return rb_big_norm(num);
2326read_num(
const char **s,
const char *
const end,
VALUE *num,
VALUE *nexp)
2328 VALUE fp = ONE, exp, fn = ZERO, n = ZERO;
2329 int expsign = 0, ok = 0;
2334 if (*s < end && **s !=
'.') {
2335 n = rb_int_parse_cstr(*s, end-*s, &e, NULL,
2344 if (*s < end && **s ==
'.') {
2348 fp = rb_int_parse_cstr(*s, end-*s, &e, &count,
2355 n = n == ZERO ? fp : rb_int_plus(rb_int_mul(*num, l), fp);
2362 if (!ok || *s >= end) {
2365 else if (isletterr(**s)) {
2368 else if (*s + 1 < end && islettere(**s)) {
2370 expsign = read_sign(s, end);
2371 exp = rb_int_parse_cstr(*s, end-*s, &e, NULL,
2377 if (expsign ==
'-') {
2378 if (fn != ZERO) exp = rb_int_plus(exp, fn);
2381 if (fn != ZERO) exp = rb_int_minus(exp, fn);
2382 exp = negate_num(exp);
2391inline static const char *
2392skip_ws(
const char *s,
const char *e)
2394 while (s < e && isspace((
unsigned char)*s))
2400parse_rat(
const char *s,
const char *
const e,
int strict,
int raise)
2403 VALUE num, den, nexp, dexp;
2406 sign = read_sign(&s, e);
2408 if (!read_num(&s, e, &num, &nexp)) {
2409 if (strict)
return Qnil;
2413 if (s < e && *s ==
'/') {
2415 if (!read_num(&s, e, &den, &dexp)) {
2416 if (strict)
return Qnil;
2419 else if (den == ZERO) {
2420 if (!raise)
return Qnil;
2423 else if (strict && skip_ws(s, e) != e) {
2427 nexp = rb_int_minus(nexp, dexp);
2428 nurat_reduce(&num, &den);
2431 else if (strict && skip_ws(s, e) != e) {
2436 if (INT_NEGATIVE_P(nexp)) {
2441 num = rb_int_mul(num, mul);
2450 div = f_expt10(nexp);
2452 den = rb_int_mul(den, div);
2459 nurat_reduce(&num, &den);
2463 num = negate_num(num);
2470string_to_r_strict(
VALUE self,
int raise)
2476 num = parse_rat(RSTRING_PTR(self),
RSTRING_END(self), 1, raise);
2478 if (!raise)
return Qnil;
2479 rb_raise(rb_eArgError,
"invalid value for convert(): %+"PRIsVALUE,
2484 if (!raise)
return Qnil;
2520string_to_r(
VALUE self)
2526 num = parse_rat(RSTRING_PTR(self),
RSTRING_END(self), 0, TRUE);
2534rb_cstr_to_rat(
const char *s,
int strict)
2538 num = parse_rat(s, s + strlen(s), strict, TRUE);
2546to_rational(
VALUE val)
2548 return rb_convert_type_with_id(val,
T_RATIONAL,
"Rational", idTo_r);
2554 VALUE a1 = numv, a2 = denv;
2560 if (!raise)
return Qnil;
2561 rb_cant_convert(
Qnil,
"Rational");
2565 if (k_exact_zero_p(RCOMPLEX(a1)->imag))
2566 a1 = RCOMPLEX(a1)->real;
2570 if (k_exact_zero_p(RCOMPLEX(a2)->imag))
2571 a2 = RCOMPLEX(a2)->real;
2578 a1 = float_to_r(a1);
2584 a1 = string_to_r_strict(a1, raise);
2589 rb_set_errinfo(
Qnil);
2599 a2 = float_to_r(a2);
2605 a2 = string_to_r_strict(a2, raise);
2610 rb_set_errinfo(
Qnil);
2617 if (UNDEF_P(a2) || (k_exact_one_p(a2)))
2624 VALUE result = rb_protect(to_rational, a1, NULL);
2625 rb_set_errinfo(
Qnil);
2628 return to_rational(a1);
2632 if (!k_numeric_p(a1)) {
2634 a1 = rb_protect(to_rational, a1, &state);
2636 rb_set_errinfo(
Qnil);
2641 a1 = rb_check_convert_type_with_id(a1,
T_RATIONAL,
"Rational", idTo_r);
2644 if (!k_numeric_p(a2)) {
2646 a2 = rb_protect(to_rational, a2, &state);
2648 rb_set_errinfo(
Qnil);
2653 a2 = rb_check_convert_type_with_id(a2,
T_RATIONAL,
"Rational", idTo_r);
2656 if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
2657 (!f_integer_p(a1) || !f_integer_p(a2))) {
2658 VALUE tmp = rb_protect(to_rational, a1, &state);
2663 rb_set_errinfo(
Qnil);
2665 return f_div(a1, a2);
2669 a1 = nurat_int_value(a1);
2674 else if (!k_integer_p(a2) && !raise) {
2678 a2 = nurat_int_value(a2);
2682 return nurat_s_canonicalize_internal(klass, a1, a2);
2686nurat_s_convert(
int argc,
VALUE *argv,
VALUE klass)
2694 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_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 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
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 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.
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_cObject
Object class.
VALUE rb_cInteger
Module 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_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
#define RB_INT_PARSE_UNDERSCORE
Allows underscores between digits.
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_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_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.
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.
static char * RSTRING_END(VALUE str)
Queries the end of 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.