20 #define ZERO INT2FIX(0)
21 #define ONE INT2FIX(1)
22 #define TWO INT2FIX(2)
30 #define f_boolcast(x) ((x) ? Qtrue : Qfalse)
34 f_##n(VALUE x, VALUE y)\
36 return rb_funcall(x, (op), 1, y);\
43 return rb_funcall(x, id_##n, 0);\
48 f_##n(VALUE x, VALUE y)\
50 return rb_funcall(x, id_##n, 1, y);\
171 #define f_expt10(x) f_expt(INT2FIX(10), x)
181 #define f_positive_p(x) (!f_negative_p(x))
201 #define f_nonzero_p(x) (!f_zero_p(x))
273 #define k_exact_p(x) (!k_float_p(x))
274 #define k_inexact_p(x) k_float_p(x)
276 #define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
277 #define k_exact_one_p(x) (k_exact_p(x) && f_one_p(x))
280 #define f_gcd f_gcd_orig
342 VALUE r = f_gcd_orig(x, y);
359 #define get_dat1(x) \
360 struct RRational *dat;\
361 dat = ((struct RRational *)(x))
363 #define get_dat2(x,y) \
364 struct RRational *adat, *bdat;\
365 adat = ((struct RRational *)(x));\
366 bdat = ((struct RRational *)(y))
385 #define rb_raise_zerodiv() rb_raise(rb_eZeroDivError, "divided by 0")
435 #ifdef CANONICALIZATION_FOR_MATHN
440 static int canonicalization = 0;
445 canonicalization =
f;
486 gcd =
f_gcd(num, den);
491 if (
f_one_p(den) && canonicalization)
511 if (
f_one_p(den) && canonicalization)
637 #define f_imul f_imul_orig
645 if (a == 0 || b == 0)
665 VALUE r = f_imul_orig(x, y);
682 long ig =
i_gcd(ad, bd);
735 switch (
TYPE(other)) {
752 adat->num, adat->den,
753 bdat->num, bdat->den,
'+');
775 switch (
TYPE(other)) {
792 adat->num, adat->den,
793 bdat->num, bdat->den,
'-');
823 long g1 =
i_gcd(an, bd);
824 long g2 =
i_gcd(ad, bn);
826 num =
f_imul(an / g1, bn / g2);
827 den =
f_imul(ad / g2, bd / g1);
854 switch (
TYPE(other)) {
871 adat->num, adat->den,
872 bdat->num, bdat->den,
'*');
895 switch (
TYPE(other)) {
914 if (x != 0.0 && modf(x, &den) == 0.0) {
927 bdat->den, bdat->num);
930 adat->num, adat->den,
931 bdat->num, bdat->den,
'/');
1008 switch (
TYPE(other)) {
1017 num =
f_expt(dat->num, other);
1018 den =
f_expt(dat->den, other);
1032 rb_warn(
"in a**b, b may be too big");
1059 switch (
TYPE(other)) {
1066 return f_cmp(dat->num, other);
1083 num1 =
f_mul(adat->num, bdat->den);
1084 num2 =
f_mul(bdat->num, adat->den);
1108 switch (
TYPE(other)) {
1146 switch (
TYPE(other)) {
1172 return f_idiv(
self, other);
1194 nurat_true(
VALUE self)
1204 return f_idiv(dat->num, dat->den);
1235 return f_idiv(dat->num, dat->den);
1268 return (*
func)(
self);
1410 return f_fdiv(dat->num, dat->den);
1428 #define id_ceil rb_intern("ceil")
1429 #define f_ceil(x) rb_funcall((x), id_ceil, 0)
1431 #define id_quo rb_intern("quo")
1432 #define f_quo(x,y) rb_funcall((x), id_quo, 1, (y))
1434 #define f_reciprocal(x) f_quo(ONE, (x))
1498 VALUE c, k, t, p0, p1, p2, q0, q1, q2;
1583 s = (*func)(dat->num);
1702 return f_gcd(
self, other);
1720 return f_lcm(
self, other);
1763 #define id_numerator rb_intern("numerator")
1764 #define f_numerator(x) rb_funcall((x), id_numerator, 0)
1766 #define id_denominator rb_intern("denominator")
1767 #define f_denominator(x) rb_funcall((x), id_denominator, 0)
1769 #define id_to_r rb_intern("to_r")
1770 #define f_to_r(x) rb_funcall((x), id_to_r, 0)
1927 float_decode(
VALUE self)
1936 #define id_lshift rb_intern("<<")
1937 #define f_lshift(x,n) rb_funcall((x), id_lshift, 1, (n))
2015 VALUE two_times_f, den;
2025 VALUE radix_times_f, den;
2048 return (c ==
'-' || c ==
'+');
2066 return isdigit((
unsigned char)c);
2074 int us = 1, ret = 1;
2105 }
while (**s ==
'_');
2116 return (c ==
'e' || c ==
'E');
2142 *num =
f_mul(*num, l);
2143 *num =
f_add(*num, fp);
2144 *num =
f_div(*num, l);
2163 *num =
f_mul(*num, l);
2183 if (!
read_num(s, sign, strict, num))
2190 *num =
f_div(*num, den);
2210 while (isspace((
unsigned char)**s))
2326 VALUE a1, a2, backref;
2386 (!f_integer_p(a1) || !f_integer_p(a2)))
2387 return f_div(a1, a2);
2443 #define rb_intern(str) rb_intern_const(str)
2445 assert(fprintf(stderr,
"assert() is now active\n"));
RUBY_EXTERN VALUE rb_cString
static VALUE numeric_denominator(VALUE self)
static VALUE string_to_r_strict(VALUE self)
static VALUE f_sub(VALUE x, VALUE y)
static VALUE f_odd_p(VALUE integer)
#define rb_rational_new2(x, y)
RUBY_EXTERN VALUE rb_cFloat
VALUE rb_num_coerce_bin(VALUE, VALUE, ID)
static VALUE nurat_mul(VALUE self, VALUE other)
static VALUE k_rational_p(VALUE x)
size_t strlen(const char *)
static VALUE f_rational_new_no_reduce1(VALUE klass, VALUE x)
void rb_backref_set(VALUE)
static VALUE f_addsub(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
static int read_sign(const char **s)
static VALUE nurat_expt(VALUE self, VALUE other)
VALUE rb_lcm(VALUE self, VALUE other)
static VALUE nurat_floor(VALUE self)
static VALUE float_rationalize(int argc, VALUE *argv, VALUE self)
static void nurat_int_check(VALUE num)
static VALUE nurat_s_alloc(VALUE klass)
static VALUE nurat_coerce(VALUE self, VALUE other)
VALUE rb_gcd(VALUE self, VALUE other)
static VALUE nurat_s_new(int argc, VALUE *argv, VALUE klass)
static VALUE nurat_loader(VALUE self, VALUE a)
static VALUE nurat_fdiv(VALUE self, VALUE other)
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
void rb_must_asciicompat(VALUE)
SSL_METHOD *(* func)(void)
#define k_exact_zero_p(x)
VALUE rb_str_concat(VALUE, VALUE)
st_index_t rb_memhash(const void *ptr, long len)
static VALUE f_lt_p(VALUE x, VALUE y)
#define rb_check_trusted(obj)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
VALUE rb_backref_get(void)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_ivar_get(VALUE, ID)
static VALUE k_numeric_p(VALUE x)
#define MUL_OVERFLOW_LONG_P(a, b)
VALUE rb_convert_type(VALUE, int, const char *, const char *)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
static VALUE float_denominator(VALUE self)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
static VALUE f_one_p(VALUE x)
static VALUE integer_rationalize(int argc, VALUE *argv, VALUE self)
double rb_str_to_dbl(VALUE, int)
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
static VALUE nurat_floor_n(int argc, VALUE *argv, VALUE self)
static VALUE f_negative_p(VALUE x)
void rb_undef_method(VALUE klass, const char *name)
static VALUE nurat_s_canonicalize_internal(VALUE klass, VALUE num, VALUE den)
int truncate(const char *path, off_t new_size)
static VALUE f_rational_new_no_reduce2(VALUE klass, VALUE x, VALUE y)
const char * rb_obj_classname(VALUE)
static VALUE nurat_marshal_load(VALUE self, VALUE a)
#define NEWOBJ_OF(obj, type, klass, flags)
static VALUE numeric_numerator(VALUE self)
static VALUE nurat_truncate_n(int argc, VALUE *argv, VALUE self)
#define RB_TYPE_P(obj, type)
static VALUE f_add(VALUE x, VALUE y)
VALUE rb_rational_raw(VALUE x, VALUE y)
static long i_gcd(long x, long y)
#define rb_raise_zerodiv()
static VALUE nurat_ceil(VALUE self)
static VALUE integer_numerator(VALUE self)
static VALUE k_integer_p(VALUE x)
static VALUE f_round_common(int argc, VALUE *argv, VALUE self, VALUE(*func)(VALUE))
VALUE rb_dbl2big(double d)
void nurat_canonicalization(int)
RUBY_EXTERN VALUE rb_cObject
static VALUE f_imul(long a, long b)
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
static int read_den(const char **s, int strict, VALUE *num)
VALUE rb_str_cat2(VALUE, const char *)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
static VALUE nurat_dumper(VALUE self)
static int read_digits(const char **s, int strict, VALUE *num, int *count)
VALUE rb_gcdlcm(VALUE self, VALUE other)
static VALUE nurat_to_s(VALUE self)
VALUE rb_cstr_to_rat(const char *s, int strict)
static int parse_rat(const char *s, int strict, VALUE *num)
static VALUE nurat_s_convert(int argc, VALUE *argv, VALUE klass)
#define ALLOCV_N(type, v, n)
static VALUE nurat_int_value(VALUE num)
#define RUBY_FUNC_EXPORTED
RUBY_EXTERN int isinf(double)
static int isdecimal(int c)
static VALUE nurat_eqeq_p(VALUE self, VALUE other)
static VALUE nurat_sub(VALUE self, VALUE other)
static int read_rat_nos(const char **s, int sign, int strict, VALUE *num)
#define rb_rational_new1(x)
static VALUE nurat_cmp(VALUE self, VALUE other)
static VALUE f_kind_of_p(VALUE x, VALUE c)
static void float_decode_internal(VALUE self, VALUE *rf, VALUE *rn)
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
static VALUE f_minus_one_p(VALUE x)
static VALUE float_numerator(VALUE self)
static VALUE nilclass_to_r(VALUE self)
static void skip_ws(const char **s)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
VALUE rb_ivar_set(VALUE, ID, VALUE)
VALUE rb_assoc_new(VALUE car, VALUE cdr)
static VALUE f_lcm(VALUE x, VALUE y)
static VALUE nurat_truncate(VALUE self)
static VALUE string_to_r(VALUE self)
VALUE rb_big_mul(VALUE x, VALUE y)
static VALUE f_rational_new_bang1(VALUE klass, VALUE x)
static VALUE nurat_div(VALUE self, VALUE other)
#define rb_rational_raw2(x, y)
RUBY_EXTERN VALUE rb_cInteger
void rb_match_busy(VALUE)
VALUE rb_call_super(int, const VALUE *)
VALUE rb_Complex(VALUE x, VALUE y)
static VALUE nurat_round_n(int argc, VALUE *argv, VALUE self)
static int read_rat(const char **s, int strict, VALUE *num)
RUBY_EXTERN VALUE rb_cNumeric
static VALUE nurat_to_r(VALUE self)
static VALUE f_gt_p(VALUE x, VALUE y)
static VALUE f_rational_new1(VALUE klass, VALUE x)
static VALUE integer_denominator(VALUE self)
static VALUE f_cmp(VALUE x, VALUE y)
static VALUE nurat_f_rational(int argc, VALUE *argv, VALUE klass)
static VALUE f_to_f(VALUE x)
void rb_str_modify(VALUE)
static VALUE nurat_s_canonicalize_internal_no_reduce(VALUE klass, VALUE num, VALUE den)
VALUE rb_usascii_str_new2(const char *)
static VALUE nurat_to_f(VALUE self)
static int islettere(int c)
static VALUE f_muldiv(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
static int read_num(const char **s, int numsign, int strict, VALUE *num)
static VALUE f_rational_new_bang2(VALUE klass, VALUE x, VALUE y)
VALUE rb_rational_new(VALUE x, VALUE y)
static VALUE f_format(VALUE self, VALUE(*func)(VALUE))
static VALUE nurat_ceil_n(int argc, VALUE *argv, VALUE self)
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
static VALUE nilclass_rationalize(int argc, VALUE *argv, VALUE self)
static VALUE f_div(VALUE x, VALUE y)
static VALUE f_eqeq_p(VALUE x, VALUE y)
VALUE rb_Rational(VALUE x, VALUE y)
static VALUE nurat_add(VALUE self, VALUE other)
static VALUE nurat_round(VALUE self)
VALUE rb_int2big(SIGNED_VALUE n)
static VALUE nurat_denominator(VALUE self)
static VALUE nurat_numerator(VALUE self)
#define assert(condition)
static VALUE nurat_marshal_dump(VALUE self)
#define StringValuePtr(v)
RUBY_EXTERN VALUE rb_eFloatDomainError
#define rb_check_frozen(obj)
static void nurat_rationalize_internal(VALUE a, VALUE b, VALUE *p, VALUE *q)
void rb_copy_generic_ivar(VALUE, VALUE)
static VALUE integer_to_r(VALUE self)
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck)
static VALUE nurat_hash(VALUE self)
static VALUE f_zero_p(VALUE x)
static VALUE nurat_rationalize(int argc, VALUE *argv, VALUE self)
static VALUE float_to_r(VALUE self)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
void rb_warn(const char *fmt,...)
RUBY_EXTERN VALUE rb_cNilClass
static VALUE f_rational_new2(VALUE klass, VALUE x, VALUE y)
static VALUE f_gcd(VALUE x, VALUE y)
static VALUE k_float_p(VALUE x)
static VALUE nurat_inspect(VALUE self)
VALUE rb_rational_reciprocal(VALUE x)
static VALUE nurat_s_new_internal(VALUE klass, VALUE num, VALUE den)
VALUE rb_num_coerce_cmp(VALUE, VALUE, ID)