Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 952991e

Browse files
committedMay 26, 2015
Port Float#next_float & Float#prev_float
Somehow rubyspec/rubyspec#297 didn't made it into rubinius. Ported over @jemc pr into rubinius.
1 parent 1a596e9 commit 952991e

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
 

Diff for: ‎spec/ruby/core/float/next_float_spec.rb

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require File.expand_path('../../../spec_helper', __FILE__)
2+
3+
describe "Float" do
4+
it "returns a float the smallest possible step greater than the receiver" do
5+
barely_positive = 0.0.next_float
6+
barely_positive.should eql 0.0.next_float
7+
8+
barely_positive.should > 0.0
9+
barely_positive.should < barely_positive.next_float
10+
11+
midpoint = barely_positive / 2
12+
[0.0, barely_positive].should include midpoint
13+
end
14+
15+
it "steps directly between MAX and INFINITY" do
16+
(-Float::INFINITY).next_float.should eql -Float::MAX
17+
Float::MAX.next_float.should eql Float::INFINITY
18+
end
19+
20+
it "steps directly between 1.0 and EPSILON + 1.0" do
21+
1.0.next_float.should eql Float::EPSILON + 1.0
22+
end
23+
24+
it "steps directly between -1.0 and EPSILON/2 - 1.0" do
25+
(-1.0).next_float.should eql Float::EPSILON/2 - 1.0
26+
end
27+
28+
it "reverses the effect of prev_float" do
29+
num = rand
30+
num.prev_float.next_float.should eql num
31+
end
32+
33+
it "returns negative zero when stepping upward from just below zero" do
34+
x = 0.0.prev_float.next_float
35+
(1/x).should eql -Float::INFINITY
36+
x = (-0.0).prev_float.next_float
37+
(1/x).should eql -Float::INFINITY
38+
x.next_float.should > 0
39+
end
40+
41+
it "returns NAN if NAN was the receiver" do
42+
Float::NAN.next_float.nan?.should eql true
43+
end
44+
end

Diff for: ‎spec/ruby/core/float/prev_float_spec.rb

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require File.expand_path('../../../spec_helper', __FILE__)
2+
3+
describe "Float" do
4+
it "returns a float the smallest possible step greater than the receiver" do
5+
barely_negative = 0.0.prev_float
6+
barely_negative.should eql 0.0.prev_float
7+
8+
barely_negative.should < 0.0
9+
barely_negative.should > barely_negative.prev_float
10+
11+
midpoint = barely_negative / 2
12+
[0.0, barely_negative].should include midpoint
13+
end
14+
15+
it "steps directly between MAX and INFINITY" do
16+
Float::INFINITY.prev_float.should eql Float::MAX
17+
(-Float::MAX).prev_float.should eql -Float::INFINITY
18+
end
19+
20+
it "steps directly between 1.0 and -EPSILON/2 + 1.0" do
21+
1.0.prev_float.should eql -Float::EPSILON/2 + 1.0
22+
end
23+
24+
it "steps directly between -1.0 and -EPSILON - 1.0" do
25+
(-1.0).prev_float.should eql -Float::EPSILON - 1.0
26+
end
27+
28+
it "reverses the effect of next_float" do
29+
num = rand
30+
num.next_float.prev_float.should eql num
31+
end
32+
33+
it "returns positive zero when stepping downward from just above zero" do
34+
x = 0.0.next_float.prev_float
35+
(1/x).should eql Float::INFINITY
36+
x = (-0.0).next_float.prev_float
37+
(1/x).should eql Float::INFINITY
38+
x.prev_float.should < 0
39+
end
40+
41+
it "returns NAN if NAN was the receiver" do
42+
Float::NAN.prev_float.nan?.should eql true
43+
end
44+
end

Diff for: ‎spec/tags/ruby/core/float/next_float_tags.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fails:returns a float the smallest possible step greater than the receiver
2+
fails:steps directly between MAX and INFINITY
3+
fails:steps directly between 1.0 and EPSILON + 1.0
4+
fails:steps directly between -1.0 and EPSILON/2 - 1.0
5+
fails:reverses the effect of prev_float
6+
fails:returns negative zero when stepping upward from just below zero
7+
fails:returns NAN if NAN was the receiver

Diff for: ‎spec/tags/ruby/core/float/prev_float_tags.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fails:returns a float the smallest possible step greater than the receiver
2+
fails:steps directly between MAX and INFINITY
3+
fails:steps directly between 1.0 and -EPSILON/2 + 1.0
4+
fails:steps directly between -1.0 and -EPSILON - 1.0
5+
fails:reverses the effect of next_float
6+
fails:returns positive zero when stepping downward from just above zero
7+
fails:returns NAN if NAN was the receiver

0 commit comments

Comments
 (0)
Please sign in to comment.