mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
95f9f98ac5
commit
f43d70f73e
2 changed files with 95 additions and 0 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Sun Sep 28 08:37:12 2008 Tadayoshi Funaba <tadf@dotrb.org>
|
||||||
|
|
||||||
|
* lib/mathn.rb: a hack to provide canonicalization. This must be
|
||||||
|
temporary, but this seems to be not bad for the time being.
|
||||||
|
|
||||||
Sat Sep 27 06:22:18 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sat Sep 27 06:22:18 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* lib/rake.rb (Module#rake_extension, String#ext, String#pathmap): use
|
* lib/rake.rb (Module#rake_extension, String#ext, String#pathmap): use
|
||||||
|
|
90
lib/mathn.rb
90
lib/mathn.rb
|
@ -18,19 +18,76 @@ unless defined?(Math.exp!)
|
||||||
Math = CMath
|
Math = CMath
|
||||||
end
|
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
|
class Fixnum
|
||||||
remove_method :/
|
remove_method :/
|
||||||
alias / quo
|
alias / quo
|
||||||
|
|
||||||
|
def_canon *(instance_methods - Object.methods - [:canon])
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Bignum
|
class Bignum
|
||||||
remove_method :/
|
remove_method :/
|
||||||
alias / quo
|
alias / quo
|
||||||
|
|
||||||
|
def_canon *(instance_methods - Object.methods - [:canon])
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias RationalOrig Rational
|
||||||
|
private :RationalOrig
|
||||||
|
def Rational(*args) RationalOrig(*args).__send__(:canon) end
|
||||||
|
|
||||||
class Rational
|
class Rational
|
||||||
Unify = true
|
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! **
|
alias power! **
|
||||||
|
|
||||||
def ** (other)
|
def ** (other)
|
||||||
|
@ -169,6 +226,39 @@ module Math
|
||||||
module_function :rsqrt
|
module_function :rsqrt
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias ComplexOrig Complex
|
||||||
|
private :ComplexOrig
|
||||||
|
def Complex(*args) ComplexOrig(*args).__send__(:canon) end
|
||||||
|
|
||||||
class Complex
|
class Complex
|
||||||
Unify = true
|
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
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue