Skip to content
Browse files

Fix tests: for n in 2..23 should the gamma(n) give exact results, for…

… n in 24..30 the tolerance must be much bigger.
  • Loading branch information...
1 parent 1c1b7a3 commit 2d5a833bd9e8c9ec07eb4c88da285531b2538a66 @r-stu31 committed May 3, 2012
Showing with 14 additions and 3 deletions.
  1. +14 −3 spec/ruby/core/math/gamma_spec.rb
View
17 spec/ruby/core/math/gamma_spec.rb
@@ -14,10 +14,21 @@
Math.gamma(0.5).should be_close(Math.sqrt(Math::PI), TOLERANCE)
end
+ # stop at n == 23 because 23! cannot be exactly represented by IEEE 754 double
f = 1
- 2.upto(30) do |n|
- it "returns #{n-1}! given #{n}" do
- Math.gamma(n).should be_close(f*=(n-1), TOLERANCE)
+ 2.upto(23) do |n|
+ it "returns exactly #{n-1}! given #{n}" do
+ f *= n - 1
+ Math.gamma(n).should == f
+ end
+ end
+
+ f = 1124000727777607680000 # 22!
+ 24.upto(30) do |n|
+ it "returns approximately #{n-1}! given #{n}" do
+ f *= n - 1
+ # compare only the first 12 places, tolerate the rest
+ Math.gamma(n).should be_close(f, f.to_s[12..-1].to_i)
end
end

0 comments on commit 2d5a833

Please sign in to comment.
Something went wrong with that request. Please try again.