aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mathn.rb
diff options
context:
space:
mode:
authortadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-27 23:41:21 +0000
committertadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-27 23:41:21 +0000
commitf43d70f73e0d0d947afb290ba6b7ac9eba4637cc (patch)
tree8cd96bb9026547d36e85ecaf4464d4747f975eef /lib/mathn.rb
parent95f9f98ac54c3ada20c049d71f8e2c0b79541ba8 (diff)
downloadruby-f43d70f73e0d0d947afb290ba6b7ac9eba4637cc.tar.gz
* lib/mathn.rb: a hack to provide canonicalization. This must be
temporary, but this seems to be not bad for the time being. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19600 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/mathn.rb')
-rw-r--r--lib/mathn.rb90
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/mathn.rb b/lib/mathn.rb
index e918608b0d..401b162b4c 100644
--- a/lib/mathn.rb
+++ b/lib/mathn.rb
@@ -18,19 +18,76 @@ unless defined?(Math.exp!)
Math = CMath
end
+class Object
+
+ def canon
+ if Rational === self
+ if denominator == 1
+ return numerator
+ end
+ elsif Complex === self
+ if Integer === imag && imag == 0
+ return real
+ end
+ end
+ self
+ end
+
+ private :canon
+
+end
+
+class Numeric
+
+ class << self
+
+ def def_canon(*ids)
+ for id in ids
+ module_eval <<-"end;"
+ alias_method :__#{id.object_id}__, :#{id.to_s}
+ private :__#{id.object_id}__
+ def #{id.to_s}(*args, &block)
+ __#{id.object_id}__(*args, &block).__send__(:canon)
+ end
+ end;
+ end
+ end
+
+ end
+
+end
+
class Fixnum
remove_method :/
alias / quo
+
+ def_canon *(instance_methods - Object.methods - [:canon])
+
end
class Bignum
remove_method :/
alias / quo
+
+ def_canon *(instance_methods - Object.methods - [:canon])
+
end
+alias RationalOrig Rational
+private :RationalOrig
+def Rational(*args) RationalOrig(*args).__send__(:canon) end
+
class Rational
Unify = true
+ class << self
+ alias convert_orig convert
+ private :convert_orig
+ def convert(*args) convert_orig(*args).__send__(:canon) end
+ end
+
+ def_canon *(instance_methods - Object.methods - [:canon])
+
alias power! **
def ** (other)
@@ -169,6 +226,39 @@ module Math
module_function :rsqrt
end
+alias ComplexOrig Complex
+private :ComplexOrig
+def Complex(*args) ComplexOrig(*args).__send__(:canon) end
+
class Complex
Unify = true
+
+ class << self
+ alias convert_orig convert
+ private :convert_orig
+ def convert(*args) convert_orig(*args).__send__(:canon) end
+ end
+
+ def_canon *(instance_methods - Object.methods - [:canon])
+
+end
+
+class NilClass
+
+ def to_r() 0 end
+ def to_c() 0 end
+
+end
+
+class Integer
+
+ def to_r() self end
+ def to_c() self end
+
+end
+
+class Float
+
+ def_canon *(instance_methods - Object.methods - [:canon])
+
end