summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-complex/mrblib/complex.rb
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-08-09 21:47:00 +0900
committerGitHub <[email protected]>2019-08-09 21:47:00 +0900
commit496a2c3264aa3a2dbc50bb01e545ed4803718213 (patch)
treece1ecd89ddb7d67171dfda216e6e82ac79c0b373 /mrbgems/mruby-complex/mrblib/complex.rb
parent438638b42e13297d38198b15f2fc007e0f5a44a8 (diff)
parent1cd0ff0d42ea4fb385288d3bd2c440a1f46e08af (diff)
downloadmruby-496a2c3264aa3a2dbc50bb01e545ed4803718213.tar.gz
mruby-496a2c3264aa3a2dbc50bb01e545ed4803718213.zip
Merge pull request #4623 from chasonr/complex-fixes
Avoid premature overflow in Complex#abs and Complex#/
Diffstat (limited to 'mrbgems/mruby-complex/mrblib/complex.rb')
-rw-r--r--mrbgems/mruby-complex/mrblib/complex.rb5
1 files changed, 2 insertions, 3 deletions
diff --git a/mrbgems/mruby-complex/mrblib/complex.rb b/mrbgems/mruby-complex/mrblib/complex.rb
index 1a6f7e92e..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