summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-complex
diff options
context:
space:
mode:
authorRay Chason <[email protected]>2019-08-08 23:59:25 -0400
committerRay Chason <[email protected]>2019-08-08 23:59:25 -0400
commitc181d1e7fa80b1cc0551f8fbd85ab6f7419b7887 (patch)
tree53cfe089a8a9505d0d1bb2c1ee6b67fe5cf7e254 /mrbgems/mruby-complex
parent438638b42e13297d38198b15f2fc007e0f5a44a8 (diff)
downloadmruby-c181d1e7fa80b1cc0551f8fbd85ab6f7419b7887.tar.gz
mruby-c181d1e7fa80b1cc0551f8fbd85ab6f7419b7887.zip
Implement Complex#abs in terms of Math.hypot
Math.hypot avoids premature overflow and underflow
Diffstat (limited to 'mrbgems/mruby-complex')
-rw-r--r--mrbgems/mruby-complex/mrblib/complex.rb2
-rw-r--r--mrbgems/mruby-complex/test/complex.rb8
2 files changed, 9 insertions, 1 deletions
diff --git a/mrbgems/mruby-complex/mrblib/complex.rb b/mrbgems/mruby-complex/mrblib/complex.rb
index 1a6f7e92e..1025e975e 100644
--- a/mrbgems/mruby-complex/mrblib/complex.rb
+++ b/mrbgems/mruby-complex/mrblib/complex.rb
@@ -62,7 +62,7 @@ class Complex < Numeric
end
def abs
- Math.sqrt(abs2)
+ Math.hypot imaginary, real
end
alias_method :magnitude, :abs
diff --git a/mrbgems/mruby-complex/test/complex.rb b/mrbgems/mruby-complex/test/complex.rb
index 4ae80515b..dcc0f3bef 100644
--- a/mrbgems/mruby-complex/test/complex.rb
+++ b/mrbgems/mruby-complex/test/complex.rb
@@ -70,6 +70,14 @@ end
assert 'Complex#abs' do
assert_float Complex(-1).abs, 1
assert_float Complex(3.0, -4.0).abs, 5.0
+ if 1e39.infinite? then
+ # MRB_USE_FLOAT in effect
+ exp = 125
+ else
+ exp = 1021
+ end
+ assert_true Complex(3.0*2**exp, 4.0*2**exp).abs.finite?
+ assert_float Complex(3.0*2**exp, 4.0*2**exp).abs, 5.0*2**exp
end
assert 'Complex#abs2' do