Skip to content

Commit 504ea87

Browse files
committed
Add specs for edge cases.
1 parent 57fa750 commit 504ea87

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

spec/ruby/core/float/round_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,38 @@
109109
-5.55.round(1, half: :even).should eql(-5.6)
110110
end
111111

112+
it "preserves cases where neighbouring floating pointer number increase the decimal places" do
113+
4.8100000000000005.round(5, half: nil).should eql(4.81)
114+
4.8100000000000005.round(5, half: :up).should eql(4.81)
115+
4.8100000000000005.round(5, half: :down).should eql(4.81)
116+
4.8100000000000005.round(5, half: :even).should eql(4.81)
117+
-4.8100000000000005.round(5, half: nil).should eql(-4.81)
118+
-4.8100000000000005.round(5, half: :up).should eql(-4.81)
119+
-4.8100000000000005.round(5, half: :down).should eql(-4.81)
120+
-4.8100000000000005.round(5, half: :even).should eql(-4.81)
121+
4.81.round(5, half: nil).should eql(4.81)
122+
4.81.round(5, half: :up).should eql(4.81)
123+
4.81.round(5, half: :down).should eql(4.81)
124+
4.81.round(5, half: :even).should eql(4.81)
125+
-4.81.round(5, half: nil).should eql(-4.81)
126+
-4.81.round(5, half: :up).should eql(-4.81)
127+
-4.81.round(5, half: :down).should eql(-4.81)
128+
-4.81.round(5, half: :even).should eql(-4.81)
129+
4.809999999999999.round(5, half: nil).should eql(4.81)
130+
4.809999999999999.round(5, half: :up).should eql(4.81)
131+
4.809999999999999.round(5, half: :down).should eql(4.81)
132+
4.809999999999999.round(5, half: :even).should eql(4.81)
133+
-4.809999999999999.round(5, half: nil).should eql(-4.81)
134+
-4.809999999999999.round(5, half: :up).should eql(-4.81)
135+
-4.809999999999999.round(5, half: :down).should eql(-4.81)
136+
-4.809999999999999.round(5, half: :even).should eql(-4.81)
137+
end
138+
112139
ruby_bug "", ""..."3.2" do
140+
# These numbers are neighbouring floating point numbers round a
141+
# precise value. They test that the rounding modes work correctly
142+
# round that value and precision is not lost which might cause
143+
# incorrect results.
113144
it "does not lose precision during the rounding process" do
114145
767573.1875850001.round(5, half: nil).should eql(767573.18759)
115146
767573.1875850001.round(5, half: :up).should eql(767573.18759)
@@ -119,6 +150,14 @@
119150
-767573.1875850001.round(5, half: :up).should eql(-767573.18759)
120151
-767573.1875850001.round(5, half: :down).should eql(-767573.18759)
121152
-767573.1875850001.round(5, half: :even).should eql(-767573.18759)
153+
767573.187585.round(5, half: nil).should eql(767573.18759)
154+
767573.187585.round(5, half: :up).should eql(767573.18759)
155+
767573.187585.round(5, half: :down).should eql(767573.18758)
156+
767573.187585.round(5, half: :even).should eql(767573.18758)
157+
-767573.187585.round(5, half: nil).should eql(-767573.18759)
158+
-767573.187585.round(5, half: :up).should eql(-767573.18759)
159+
-767573.187585.round(5, half: :down).should eql(-767573.18758)
160+
-767573.187585.round(5, half: :even).should eql(-767573.18758)
122161
767573.1875849998.round(5, half: nil).should eql(767573.18758)
123162
767573.1875849998.round(5, half: :up).should eql(767573.18758)
124163
767573.1875849998.round(5, half: :down).should eql(767573.18758)

0 commit comments

Comments
 (0)