diff options
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | bignum.c | 5 | ||||
-rw-r--r-- | complex.c | 18 | ||||
-rw-r--r-- | lib/complex.rb | 6 | ||||
-rw-r--r-- | lib/mathn.rb | 8 | ||||
-rw-r--r-- | rational.c | 9 |
6 files changed, 40 insertions, 19 deletions
@@ -1,3 +1,16 @@ +Fri Sep 19 22:37:25 2008 Tadayoshi Funaba <tadf@dotrb.org> + + * complex.c: uses f_(in)?exact_p macro. + + * rational.c: ditto. + + * bignum.c (rb_big_pow): bignum**bignum - should calculate without + rational. + + * lib/complex.rb: should override Math module at most once. + + * lib/mathn.rb: requires 'cmath' directly. + Fri Sep 19 20:48:06 2008 Yuki Sonoda <yugui@yugui.jp> * prec.c: removed. Precision will be redesigned and be back again. @@ -2102,9 +2102,6 @@ rb_big_pow(VALUE x, VALUE y) break; case T_BIGNUM: - if (rb_funcall(y, '<', 1, INT2FIX(0))) - return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y); - rb_warn("in a**b, b may be too big"); d = rb_big2dbl(y); break; @@ -2113,7 +2110,7 @@ rb_big_pow(VALUE x, VALUE y) yy = FIX2LONG(y); if (yy < 0) - return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y); + return rb_funcall(rb_rational_raw1(x), rb_intern("**"), 1, y); else { VALUE z = 0; SIGNED_VALUE mask; @@ -247,6 +247,9 @@ k_complex_p(VALUE x) return f_kind_of_p(x, rb_cComplex); } +#define k_exact_p(x) (!k_float_p(x)) +#define k_inexact_p(x) k_float_p(x) + #define get_dat1(x) \ struct RComplex *dat;\ dat = ((struct RComplex *)(x)) @@ -334,7 +337,7 @@ nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE image) #define CL_CANON #ifdef CL_CANON if (f_zero_p(image) && f_unify_p(klass) && - !k_float_p(real) && !k_float_p(image)) + k_exact_p(real) && k_exact_p(image)) return real; #else if (f_zero_p(image) && f_unify_p(klass)) @@ -986,7 +989,7 @@ nucomp_to_i(VALUE self) { get_dat1(self); - if (k_float_p(dat->image) || !f_zero_p(dat->image)) { + if (k_inexact_p(dat->image) || !f_zero_p(dat->image)) { VALUE s = f_to_s(self); rb_raise(rb_eRangeError, "can't convert %s into Integer", StringValuePtr(s)); @@ -999,7 +1002,7 @@ nucomp_to_f(VALUE self) { get_dat1(self); - if (k_float_p(dat->image) || !f_zero_p(dat->image)) { + if (k_inexact_p(dat->image) || !f_zero_p(dat->image)) { VALUE s = f_to_s(self); rb_raise(rb_eRangeError, "can't convert %s into Float", StringValuePtr(s)); @@ -1012,7 +1015,7 @@ nucomp_to_r(VALUE self) { get_dat1(self); - if (k_float_p(dat->image) || !f_zero_p(dat->image)) { + if (k_inexact_p(dat->image) || !f_zero_p(dat->image)) { VALUE s = f_to_s(self); rb_raise(rb_eRangeError, "can't convert %s into Rational", StringValuePtr(s)); @@ -1183,7 +1186,6 @@ string_to_c_internal(VALUE self) return rb_assoc_new(rb_complex_polar(r, i), re); else return rb_assoc_new(rb_complex_new2(r, i), re); - } } @@ -1257,7 +1259,7 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass) { get_dat1(a1); - if (!k_float_p(dat->image) && f_zero_p(dat->image)) + if (k_exact_p(dat->image) && f_zero_p(dat->image)) a1 = dat->real; } } @@ -1267,7 +1269,7 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass) { get_dat1(a2); - if (!k_float_p(dat->image) && f_zero_p(dat->image)) + if (k_exact_p(dat->image) && f_zero_p(dat->image)) a2 = dat->real; } } @@ -1338,7 +1340,7 @@ numeric_arg(VALUE self) static VALUE numeric_rect(VALUE self) { - return rb_assoc_new(self, ZERO); + return rb_assoc_new(self, INT2FIX(0)); } static VALUE diff --git a/lib/complex.rb b/lib/complex.rb index 1845f30b1..70e168e91 100644 --- a/lib/complex.rb +++ b/lib/complex.rb @@ -1,7 +1,9 @@ require 'cmath' -Object.instance_eval{remove_const :Math} -Math = CMath +unless defined?(Math.exp!) + Object.instance_eval{remove_const :Math} + Math = CMath +end def Complex.generic? (other) other.kind_of?(Integer) || diff --git a/lib/mathn.rb b/lib/mathn.rb index 2af2b83da..b29f994ac 100644 --- a/lib/mathn.rb +++ b/lib/mathn.rb @@ -9,11 +9,15 @@ # # -require "complex.rb" -require "rational.rb" +require "cmath.rb" require "matrix.rb" require "prime.rb" +unless defined?(Math.exp!) + Object.instance_eval{remove_const :Math} + Math = CMath +end + class Fixnum remove_method :/ alias / quo diff --git a/rational.c b/rational.c index c6c9b91ea..d8af04c61 100644 --- a/rational.c +++ b/rational.c @@ -213,6 +213,9 @@ k_rational_p(VALUE x) return f_kind_of_p(x, rb_cRational); } +#define k_exact_p(x) (!k_float_p(x)) +#define k_inexact_p(x) k_float_p(x) + #ifndef NDEBUG #define f_gcd f_gcd_orig #endif @@ -773,7 +776,7 @@ nurat_fdiv(VALUE self, VALUE other) static VALUE nurat_expt(VALUE self, VALUE other) { - if (f_zero_p(other)) + if (k_exact_p(other) && f_zero_p(other)) return f_rational_new_bang1(CLASS_OF(self), ONE); if (k_rational_p(other)) { @@ -1403,7 +1406,7 @@ nurat_s_convert(int argc, VALUE *argv, VALUE klass) switch (TYPE(a1)) { case T_COMPLEX: - if (k_float_p(RCOMPLEX(a1)->image) || !f_zero_p(RCOMPLEX(a1)->image)) { + if (k_inexact_p(RCOMPLEX(a1)->image) || !f_zero_p(RCOMPLEX(a1)->image)) { VALUE s = f_to_s(a1); rb_raise(rb_eRangeError, "can't accept %s", StringValuePtr(s)); @@ -1413,7 +1416,7 @@ nurat_s_convert(int argc, VALUE *argv, VALUE klass) switch (TYPE(a2)) { case T_COMPLEX: - if (k_float_p(RCOMPLEX(a2)->image) || !f_zero_p(RCOMPLEX(a2)->image)) { + if (k_inexact_p(RCOMPLEX(a2)->image) || !f_zero_p(RCOMPLEX(a2)->image)) { VALUE s = f_to_s(a2); rb_raise(rb_eRangeError, "can't accept %s", StringValuePtr(s)); |