Skip to content

Commit

Permalink
Port Float#next_float & Float#prev_float
Browse files Browse the repository at this point in the history
Somehow rubyspec/rubyspec#297 didn't made it into rubinius.
Ported over @jemc pr into rubinius.
  • Loading branch information
tak1n committed May 26, 2015
1 parent 1a596e9 commit 952991e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
44 changes: 44 additions & 0 deletions spec/ruby/core/float/next_float_spec.rb
@@ -0,0 +1,44 @@
require File.expand_path('../../../spec_helper', __FILE__)

describe "Float" do
it "returns a float the smallest possible step greater than the receiver" do
barely_positive = 0.0.next_float
barely_positive.should eql 0.0.next_float

barely_positive.should > 0.0
barely_positive.should < barely_positive.next_float

midpoint = barely_positive / 2
[0.0, barely_positive].should include midpoint
end

it "steps directly between MAX and INFINITY" do
(-Float::INFINITY).next_float.should eql -Float::MAX
Float::MAX.next_float.should eql Float::INFINITY
end

it "steps directly between 1.0 and EPSILON + 1.0" do
1.0.next_float.should eql Float::EPSILON + 1.0
end

it "steps directly between -1.0 and EPSILON/2 - 1.0" do
(-1.0).next_float.should eql Float::EPSILON/2 - 1.0
end

it "reverses the effect of prev_float" do
num = rand
num.prev_float.next_float.should eql num
end

it "returns negative zero when stepping upward from just below zero" do
x = 0.0.prev_float.next_float
(1/x).should eql -Float::INFINITY
x = (-0.0).prev_float.next_float
(1/x).should eql -Float::INFINITY
x.next_float.should > 0
end

it "returns NAN if NAN was the receiver" do
Float::NAN.next_float.nan?.should eql true
end
end
44 changes: 44 additions & 0 deletions spec/ruby/core/float/prev_float_spec.rb
@@ -0,0 +1,44 @@
require File.expand_path('../../../spec_helper', __FILE__)

describe "Float" do
it "returns a float the smallest possible step greater than the receiver" do
barely_negative = 0.0.prev_float
barely_negative.should eql 0.0.prev_float

barely_negative.should < 0.0
barely_negative.should > barely_negative.prev_float

midpoint = barely_negative / 2
[0.0, barely_negative].should include midpoint
end

it "steps directly between MAX and INFINITY" do
Float::INFINITY.prev_float.should eql Float::MAX
(-Float::MAX).prev_float.should eql -Float::INFINITY
end

it "steps directly between 1.0 and -EPSILON/2 + 1.0" do
1.0.prev_float.should eql -Float::EPSILON/2 + 1.0
end

it "steps directly between -1.0 and -EPSILON - 1.0" do
(-1.0).prev_float.should eql -Float::EPSILON - 1.0
end

it "reverses the effect of next_float" do
num = rand
num.next_float.prev_float.should eql num
end

it "returns positive zero when stepping downward from just above zero" do
x = 0.0.next_float.prev_float
(1/x).should eql Float::INFINITY
x = (-0.0).next_float.prev_float
(1/x).should eql Float::INFINITY
x.prev_float.should < 0
end

it "returns NAN if NAN was the receiver" do
Float::NAN.prev_float.nan?.should eql true
end
end
7 changes: 7 additions & 0 deletions spec/tags/ruby/core/float/next_float_tags.txt
@@ -0,0 +1,7 @@
fails:returns a float the smallest possible step greater than the receiver
fails:steps directly between MAX and INFINITY
fails:steps directly between 1.0 and EPSILON + 1.0
fails:steps directly between -1.0 and EPSILON/2 - 1.0
fails:reverses the effect of prev_float
fails:returns negative zero when stepping upward from just below zero
fails:returns NAN if NAN was the receiver
7 changes: 7 additions & 0 deletions spec/tags/ruby/core/float/prev_float_tags.txt
@@ -0,0 +1,7 @@
fails:returns a float the smallest possible step greater than the receiver
fails:steps directly between MAX and INFINITY
fails:steps directly between 1.0 and -EPSILON/2 + 1.0
fails:steps directly between -1.0 and -EPSILON - 1.0
fails:reverses the effect of next_float
fails:returns positive zero when stepping downward from just above zero
fails:returns NAN if NAN was the receiver

0 comments on commit 952991e

Please sign in to comment.