Skip to content

Instantly share code, notes, and snippets.

@mrkn
Created June 14, 2011 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkn/1024322 to your computer and use it in GitHub Desktop.
Save mrkn/1024322 to your computer and use it in GitHub Desktop.
diff --git a/lib/cmath.rb b/lib/cmath.rb
index e21f9d8..16e0c49 100644
--- a/lib/cmath.rb
+++ b/lib/cmath.rb
@@ -305,14 +305,14 @@ module CMath
module_function :atanh!
module_function :atanh
- module_function :frexp
- module_function :ldexp
- module_function :hypot
module_function :erf
module_function :erfc
module_function :gamma
module_function :lgamma
+ undef :frexp
+ undef :ldexp
+ undef :hypot
end
class Object
diff --git a/lib/complex.rb b/lib/complex.rb
index 9c57ecd..056111c 100644
--- a/lib/complex.rb
+++ b/lib/complex.rb
@@ -5,7 +5,16 @@ warn('lib/complex.rb is deprecated') if $VERBOSE
require 'cmath'
unless defined?(Math.exp!)
- Object.instance_eval{remove_const :Math}
+ module CMath
+ math = Object.instance_eval{remove_const :Math}
+ define_method(:frexp) {|x| math.frexp(x) }
+ define_method(:ldexp) {|x, e| math.ldexp(x, e) }
+ define_method(:hypot) {|x, y| math.hypot(x, y) }
+
+ module_function :frexp
+ module_function :ldexp
+ module_function :hypot
+ end
Math = CMath
end
diff --git a/test/test_cmath.rb b/test/test_cmath.rb
index 4f85ec3..17bec09 100644
--- a/test/test_cmath.rb
+++ b/test/test_cmath.rb
@@ -9,4 +9,16 @@ class TestCMath < Test::Unit::TestCase
def test_cbrt_returns_principal_value_of_cube_root
assert_equal CMath.cbrt(-8), (-8)**(1.0/3), '#3676'
end
+
+ def test_frexp_is_undefined
+ assert(!CMath.methods.include?(:frexp))
+ end
+
+ def test_ldexp_is_undefined
+ assert(!CMath.methods.include?(:ldexp))
+ end
+
+ def test_hypot_is_undefined
+ assert(!CMath.methods.include?(:hypot))
+ end
end
diff --git a/test/test_complex.rb b/test/test_complex.rb
new file mode 100644
index 0000000..5b384b9
--- /dev/null
+++ b/test/test_complex.rb
@@ -0,0 +1,72 @@
+require 'test/unit'
+require_relative 'ruby/envutil'
+
+class TestComplex < Test::Unit::TestCase
+ def test_Math_respond_to_frexp
+ assert_in_out_err([], <<-INPUT, %w(:ok), [])
+ require 'complex'
+ exit unless Math.respond_to?(:frexp)
+ p :ok
+ INPUT
+ end
+
+ def test_Math_frexp
+ assert_in_out_err([], <<-INPUT, %w(:ok), [])
+ require 'complex'
+ [ [ 0.0, 0.0, 0 ],
+ [ 0.5, 0.5, 0 ],
+ [ 2.0, 0.5, 2 ],
+ ].each_with_index do |(x, y, e), i|
+ unless (Math.frexp(x).first - y).abs < 1e-6
+ p "invalid y - %d" % [i]
+ exit
+ end
+ unless Math.frexp(x).last == e
+ p "invalid e - %d" % [i]
+ exit
+ end
+ end
+ p :ok
+ INPUT
+ end
+
+ def test_Math_respond_to_ldexp
+ assert_in_out_err([], <<-INPUT, %w(:ok), [])
+ require 'complex'
+ exit unless Math.respond_to?(:ldexp)
+ p :ok
+ INPUT
+ end
+
+ def test_Math_ldexp
+ assert_in_out_err([], <<-INPUT, %w(:ok), [])
+ require 'complex'
+ [ [ 0.0, 0.0, 0.0 ],
+ [ 0.5, 0.0, 0.5 ],
+ [ 0.5, 2.0, 2.0 ],
+ ].each_with_index do |(x, e, rv), i|
+ unless (Math.ldexp(x, e) - rv).abs < 1e-6
+ p "fail %d" % [i]
+ exit
+ end
+ end
+ p :ok
+ INPUT
+ end
+
+ def test_Math_respond_to_hypot
+ assert_in_out_err([], <<-INPUT, %w(:ok), [])
+ require 'complex'
+ exit unless Math.respond_to?(:hypot)
+ p :ok
+ INPUT
+ end
+
+ def test_hypot
+ assert_in_out_err([], <<-INPUT, %w(:ok), [])
+ require 'complex'
+ exit unless Math.hypot(3, 4) == 5
+ p :ok
+ INPUT
+ end
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment