diff options
Diffstat (limited to 'mrbgems/mruby-complex/mrblib/complex.rb')
| -rw-r--r-- | mrbgems/mruby-complex/mrblib/complex.rb | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/mrbgems/mruby-complex/mrblib/complex.rb b/mrbgems/mruby-complex/mrblib/complex.rb index 0299e7675..f32b84c8b 100644 --- a/mrbgems/mruby-complex/mrblib/complex.rb +++ b/mrbgems/mruby-complex/mrblib/complex.rb @@ -45,8 +45,7 @@ class Complex < Numeric def /(rhs) if rhs.is_a? Complex - div = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary - Complex((real * rhs.real + imaginary * rhs.imaginary) / div, (rhs.real * imaginary - real * rhs.imaginary) / div) + __div__(rhs) elsif rhs.is_a? Numeric Complex(real / rhs, imaginary / rhs) end @@ -62,7 +61,7 @@ class Complex < Numeric end def abs - Math.sqrt(abs2) + Math.hypot imaginary, real end alias_method :magnitude, :abs @@ -104,18 +103,18 @@ class Complex < Numeric end alias_method :imag, :imaginary -end -[Fixnum, Float].each do |cls| - [:+, :-, :*, :/, :==].each do |op| - cls.instance_exec do - original_operator_name = "__original_operator_#{op}_complex" - alias_method original_operator_name, op - define_method op do |rhs| - if rhs.is_a? Complex - Complex(self).send(op, rhs) - else - send(original_operator_name, rhs) + [Fixnum, Float].each do |cls| + [:+, :-, :*, :/, :==].each do |op| + cls.instance_exec do + original_operator_name = "__original_operator_#{op}_complex" + alias_method original_operator_name, op + define_method op do |rhs| + if rhs.is_a? Complex + Complex(self).send(op, rhs) + else + send(original_operator_name, rhs) + end end end end |
