Skip to content

Commit caa4e5c

Browse files
herwinweregon
authored andcommitted
Ruby 4.0: Add specs for Math.expm1 and Math.log1p
The example values are based on the original issue (https://bugs.ruby-lang.org/issues/21527), the corner cases are inspired by the specs of Math.exp and Math.log.
1 parent dc31673 commit caa4e5c

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

core/math/expm1_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
3+
4+
ruby_version_is "4.0" do
5+
describe "Math.expm1" do
6+
it "calculates Math.exp(arg) - 1" do
7+
Math.expm1(3).should == Math.exp(3) - 1
8+
end
9+
10+
it "preserves precision that can be lost otherwise" do
11+
Math.expm1(1.0e-16).should be_close(1.0e-16, TOLERANCE)
12+
Math.expm1(1.0e-16).should != 0.0
13+
end
14+
15+
it "raises a TypeError if the argument cannot be coerced with Float()" do
16+
-> { Math.expm1("test") }.should raise_error(TypeError, "can't convert String into Float")
17+
end
18+
19+
it "returns NaN given NaN" do
20+
Math.expm1(nan_value).nan?.should be_true
21+
end
22+
23+
it "raises a TypeError if the argument is nil" do
24+
-> { Math.expm1(nil) }.should raise_error(TypeError, "can't convert nil into Float")
25+
end
26+
27+
it "accepts any argument that can be coerced with Float()" do
28+
Math.expm1(MathSpecs::Float.new).should be_close(Math::E - 1, TOLERANCE)
29+
end
30+
end
31+
32+
describe "Math#expm1" do
33+
it "is accessible as a private instance method" do
34+
IncludesMath.new.send(:expm1, 23.1415).should be_close(11226018483.0012, TOLERANCE)
35+
end
36+
end
37+
end

core/math/log1p_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
3+
4+
ruby_version_is "4.0" do
5+
describe "Math.log1p" do
6+
it "calculates Math.log(1 + arg)" do
7+
Math.log1p(3).should == Math.log(1 + 3)
8+
end
9+
10+
it "preserves precision that can be lost otherwise" do
11+
Math.log1p(1e-16).should be_close(1.0e-16, TOLERANCE)
12+
Math.log1p(1e-16).should != 0.0
13+
end
14+
15+
it "raises an Math::DomainError if the argument is less than 1" do
16+
-> { Math.log1p(-1-1e-15) }.should raise_error(Math::DomainError, "Numerical argument is out of domain - log1p")
17+
end
18+
19+
it "raises a TypeError if the argument cannot be coerced with Float()" do
20+
-> { Math.log1p("test") }.should raise_error(TypeError, "can't convert String into Float")
21+
end
22+
23+
it "raises a TypeError for numerical values passed as string" do
24+
-> { Math.log1p("10") }.should raise_error(TypeError, "can't convert String into Float")
25+
end
26+
27+
it "does not accept a second argument for the base" do
28+
-> { Math.log1p(9, 3) }.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 1)")
29+
end
30+
31+
it "returns NaN given NaN" do
32+
Math.log1p(nan_value).nan?.should be_true
33+
end
34+
35+
it "raises a TypeError if the argument is nil" do
36+
-> { Math.log1p(nil) }.should raise_error(TypeError, "can't convert nil into Float")
37+
end
38+
39+
it "accepts any argument that can be coerced with Float()" do
40+
Math.log1p(MathSpecs::Float.new).should be_close(0.6931471805599453, TOLERANCE)
41+
end
42+
end
43+
44+
describe "Math#log1p" do
45+
it "is accessible as a private instance method" do
46+
IncludesMath.new.send(:log1p, 4.21).should be_close(1.65057985576528, TOLERANCE)
47+
end
48+
end
49+
end

0 commit comments

Comments
 (0)