diff options
Diffstat (limited to 'spec/ruby/core/float/round_spec.rb')
| -rw-r--r-- | spec/ruby/core/float/round_spec.rb | 223 |
1 files changed, 114 insertions, 109 deletions
diff --git a/spec/ruby/core/float/round_spec.rb b/spec/ruby/core/float/round_spec.rb index 7e8c792051..63c1d5689c 100644 --- a/spec/ruby/core/float/round_spec.rb +++ b/spec/ruby/core/float/round_spec.rb @@ -16,17 +16,17 @@ describe "Float#round" do end it "raises FloatDomainError for exceptional values" do - -> { (+infinity_value).round }.should raise_error(FloatDomainError) - -> { (-infinity_value).round }.should raise_error(FloatDomainError) - -> { nan_value.round }.should raise_error(FloatDomainError) + -> { (+infinity_value).round }.should.raise(FloatDomainError) + -> { (-infinity_value).round }.should.raise(FloatDomainError) + -> { nan_value.round }.should.raise(FloatDomainError) end it "rounds self to an optionally given precision" do - 5.5.round(0).should eql(6) - 5.7.round(1).should eql(5.7) + 5.5.round(0).should.eql?(6) + 5.7.round(1).should.eql?(5.7) 1.2345678.round(2).should == 1.23 - 123456.78.round(-2).should eql(123500) # rounded up - -123456.78.round(-2).should eql(-123500) + 123456.78.round(-2).should.eql?(123500) # rounded up + -123456.78.round(-2).should.eql?(-123500) 12.345678.round(3.999).should == 12.346 end @@ -36,36 +36,37 @@ describe "Float#round" do end it "returns zero when passed a negative argument with magnitude greater than magnitude of the whole number portion of the Float" do - 0.8346268.round(-1).should eql(0) + 0.8346268.round(-1).should.eql?(0) end it "raises a TypeError when its argument can not be converted to an Integer" do - -> { 1.0.round("4") }.should raise_error(TypeError) - -> { 1.0.round(nil) }.should raise_error(TypeError) + -> { 1.0.round("4") }.should.raise(TypeError) + -> { 1.0.round(nil) }.should.raise(TypeError) end it "raises FloatDomainError for exceptional values when passed a non-positive precision" do - -> { Float::INFINITY.round( 0) }.should raise_error(FloatDomainError) - -> { Float::INFINITY.round(-2) }.should raise_error(FloatDomainError) - -> { (-Float::INFINITY).round( 0) }.should raise_error(FloatDomainError) - -> { (-Float::INFINITY).round(-2) }.should raise_error(FloatDomainError) + -> { Float::INFINITY.round( 0) }.should.raise(FloatDomainError) + -> { Float::INFINITY.round(-2) }.should.raise(FloatDomainError) + -> { (-Float::INFINITY).round( 0) }.should.raise(FloatDomainError) + -> { (-Float::INFINITY).round(-2) }.should.raise(FloatDomainError) end it "raises RangeError for NAN when passed a non-positive precision" do - -> { Float::NAN.round(0) }.should raise_error(RangeError) - -> { Float::NAN.round(-2) }.should raise_error(RangeError) + -> { Float::NAN.round(0) }.should.raise(RangeError) + -> { Float::NAN.round(-2) }.should.raise(RangeError) end it "returns self for exceptional values when passed a non-negative precision" do Float::INFINITY.round(2).should == Float::INFINITY (-Float::INFINITY).round(2).should == -Float::INFINITY - Float::NAN.round(2).should be_nan + Float::NAN.round(2).should.nan? end # redmine:5227 it "works for corner cases" do - 42.0.round(308).should eql(42.0) - 1.0e307.round(2).should eql(1.0e307) + 42.0.round(308).should.eql?(42.0) + 1.0e307.round(2).should.eql?(1.0e307) + 120.0.round(-1).should.eql?(120) end # redmine:5271 @@ -78,114 +79,112 @@ describe "Float#round" do end it "returns big values rounded to nearest" do - +2.5e20.round(-20).should eql( +3 * 10 ** 20 ) - -2.5e20.round(-20).should eql( -3 * 10 ** 20 ) + +2.5e20.round(-20).should.eql?( +3 * 10 ** 20 ) + -2.5e20.round(-20).should.eql?( -3 * 10 ** 20 ) end # redmine #5272 it "returns rounded values for big values" do - +2.4e20.round(-20).should eql( +2 * 10 ** 20 ) - -2.4e20.round(-20).should eql( -2 * 10 ** 20 ) - +2.5e200.round(-200).should eql( +3 * 10 ** 200 ) - +2.4e200.round(-200).should eql( +2 * 10 ** 200 ) - -2.5e200.round(-200).should eql( -3 * 10 ** 200 ) - -2.4e200.round(-200).should eql( -2 * 10 ** 200 ) + +2.4e20.round(-20).should.eql?( +2 * 10 ** 20 ) + -2.4e20.round(-20).should.eql?( -2 * 10 ** 20 ) + +2.5e200.round(-200).should.eql?( +3 * 10 ** 200 ) + +2.4e200.round(-200).should.eql?( +2 * 10 ** 200 ) + -2.5e200.round(-200).should.eql?( -3 * 10 ** 200 ) + -2.4e200.round(-200).should.eql?( -2 * 10 ** 200 ) end it "returns different rounded values depending on the half option" do - 2.5.round(half: nil).should eql(3) - 2.5.round(half: :up).should eql(3) - 2.5.round(half: :down).should eql(2) - 2.5.round(half: :even).should eql(2) - 3.5.round(half: nil).should eql(4) - 3.5.round(half: :up).should eql(4) - 3.5.round(half: :down).should eql(3) - 3.5.round(half: :even).should eql(4) - (-2.5).round(half: nil).should eql(-3) - (-2.5).round(half: :up).should eql(-3) - (-2.5).round(half: :down).should eql(-2) - (-2.5).round(half: :even).should eql(-2) + 2.5.round(half: nil).should.eql?(3) + 2.5.round(half: :up).should.eql?(3) + 2.5.round(half: :down).should.eql?(2) + 2.5.round(half: :even).should.eql?(2) + 3.5.round(half: nil).should.eql?(4) + 3.5.round(half: :up).should.eql?(4) + 3.5.round(half: :down).should.eql?(3) + 3.5.round(half: :even).should.eql?(4) + (-2.5).round(half: nil).should.eql?(-3) + (-2.5).round(half: :up).should.eql?(-3) + (-2.5).round(half: :down).should.eql?(-2) + (-2.5).round(half: :even).should.eql?(-2) end it "rounds self to an optionally given precision with a half option" do - 5.55.round(1, half: nil).should eql(5.6) - 5.55.round(1, half: :up).should eql(5.6) - 5.55.round(1, half: :down).should eql(5.5) - 5.55.round(1, half: :even).should eql(5.6) - -5.55.round(1, half: nil).should eql(-5.6) - -5.55.round(1, half: :up).should eql(-5.6) - -5.55.round(1, half: :down).should eql(-5.5) - -5.55.round(1, half: :even).should eql(-5.6) + 5.55.round(1, half: nil).should.eql?(5.6) + 5.55.round(1, half: :up).should.eql?(5.6) + 5.55.round(1, half: :down).should.eql?(5.5) + 5.55.round(1, half: :even).should.eql?(5.6) + -5.55.round(1, half: nil).should.eql?(-5.6) + -5.55.round(1, half: :up).should.eql?(-5.6) + -5.55.round(1, half: :down).should.eql?(-5.5) + -5.55.round(1, half: :even).should.eql?(-5.6) end it "preserves cases where neighbouring floating pointer number increase the decimal places" do - 4.8100000000000005.round(5, half: nil).should eql(4.81) - 4.8100000000000005.round(5, half: :up).should eql(4.81) - 4.8100000000000005.round(5, half: :down).should eql(4.81) - 4.8100000000000005.round(5, half: :even).should eql(4.81) - -4.8100000000000005.round(5, half: nil).should eql(-4.81) - -4.8100000000000005.round(5, half: :up).should eql(-4.81) - -4.8100000000000005.round(5, half: :down).should eql(-4.81) - -4.8100000000000005.round(5, half: :even).should eql(-4.81) - 4.81.round(5, half: nil).should eql(4.81) - 4.81.round(5, half: :up).should eql(4.81) - 4.81.round(5, half: :down).should eql(4.81) - 4.81.round(5, half: :even).should eql(4.81) - -4.81.round(5, half: nil).should eql(-4.81) - -4.81.round(5, half: :up).should eql(-4.81) - -4.81.round(5, half: :down).should eql(-4.81) - -4.81.round(5, half: :even).should eql(-4.81) - 4.809999999999999.round(5, half: nil).should eql(4.81) - 4.809999999999999.round(5, half: :up).should eql(4.81) - 4.809999999999999.round(5, half: :down).should eql(4.81) - 4.809999999999999.round(5, half: :even).should eql(4.81) - -4.809999999999999.round(5, half: nil).should eql(-4.81) - -4.809999999999999.round(5, half: :up).should eql(-4.81) - -4.809999999999999.round(5, half: :down).should eql(-4.81) - -4.809999999999999.round(5, half: :even).should eql(-4.81) - end - - ruby_bug "#19318", ""..."3.3" do - # These numbers are neighbouring floating point numbers round a - # precise value. They test that the rounding modes work correctly - # round that value and precision is not lost which might cause - # incorrect results. - it "does not lose precision during the rounding process" do - 767573.1875850001.round(5, half: nil).should eql(767573.18759) - 767573.1875850001.round(5, half: :up).should eql(767573.18759) - 767573.1875850001.round(5, half: :down).should eql(767573.18759) - 767573.1875850001.round(5, half: :even).should eql(767573.18759) - -767573.1875850001.round(5, half: nil).should eql(-767573.18759) - -767573.1875850001.round(5, half: :up).should eql(-767573.18759) - -767573.1875850001.round(5, half: :down).should eql(-767573.18759) - -767573.1875850001.round(5, half: :even).should eql(-767573.18759) - 767573.187585.round(5, half: nil).should eql(767573.18759) - 767573.187585.round(5, half: :up).should eql(767573.18759) - 767573.187585.round(5, half: :down).should eql(767573.18758) - 767573.187585.round(5, half: :even).should eql(767573.18758) - -767573.187585.round(5, half: nil).should eql(-767573.18759) - -767573.187585.round(5, half: :up).should eql(-767573.18759) - -767573.187585.round(5, half: :down).should eql(-767573.18758) - -767573.187585.round(5, half: :even).should eql(-767573.18758) - 767573.1875849998.round(5, half: nil).should eql(767573.18758) - 767573.1875849998.round(5, half: :up).should eql(767573.18758) - 767573.1875849998.round(5, half: :down).should eql(767573.18758) - 767573.1875849998.round(5, half: :even).should eql(767573.18758) - -767573.1875849998.round(5, half: nil).should eql(-767573.18758) - -767573.1875849998.round(5, half: :up).should eql(-767573.18758) - -767573.1875849998.round(5, half: :down).should eql(-767573.18758) - -767573.1875849998.round(5, half: :even).should eql(-767573.18758) - end + 4.8100000000000005.round(5, half: nil).should.eql?(4.81) + 4.8100000000000005.round(5, half: :up).should.eql?(4.81) + 4.8100000000000005.round(5, half: :down).should.eql?(4.81) + 4.8100000000000005.round(5, half: :even).should.eql?(4.81) + -4.8100000000000005.round(5, half: nil).should.eql?(-4.81) + -4.8100000000000005.round(5, half: :up).should.eql?(-4.81) + -4.8100000000000005.round(5, half: :down).should.eql?(-4.81) + -4.8100000000000005.round(5, half: :even).should.eql?(-4.81) + 4.81.round(5, half: nil).should.eql?(4.81) + 4.81.round(5, half: :up).should.eql?(4.81) + 4.81.round(5, half: :down).should.eql?(4.81) + 4.81.round(5, half: :even).should.eql?(4.81) + -4.81.round(5, half: nil).should.eql?(-4.81) + -4.81.round(5, half: :up).should.eql?(-4.81) + -4.81.round(5, half: :down).should.eql?(-4.81) + -4.81.round(5, half: :even).should.eql?(-4.81) + 4.809999999999999.round(5, half: nil).should.eql?(4.81) + 4.809999999999999.round(5, half: :up).should.eql?(4.81) + 4.809999999999999.round(5, half: :down).should.eql?(4.81) + 4.809999999999999.round(5, half: :even).should.eql?(4.81) + -4.809999999999999.round(5, half: nil).should.eql?(-4.81) + -4.809999999999999.round(5, half: :up).should.eql?(-4.81) + -4.809999999999999.round(5, half: :down).should.eql?(-4.81) + -4.809999999999999.round(5, half: :even).should.eql?(-4.81) + end + + # These numbers are neighbouring floating point numbers round a + # precise value. They test that the rounding modes work correctly + # round that value and precision is not lost which might cause + # incorrect results. + it "does not lose precision during the rounding process" do + 767573.1875850001.round(5, half: nil).should.eql?(767573.18759) + 767573.1875850001.round(5, half: :up).should.eql?(767573.18759) + 767573.1875850001.round(5, half: :down).should.eql?(767573.18759) + 767573.1875850001.round(5, half: :even).should.eql?(767573.18759) + -767573.1875850001.round(5, half: nil).should.eql?(-767573.18759) + -767573.1875850001.round(5, half: :up).should.eql?(-767573.18759) + -767573.1875850001.round(5, half: :down).should.eql?(-767573.18759) + -767573.1875850001.round(5, half: :even).should.eql?(-767573.18759) + 767573.187585.round(5, half: nil).should.eql?(767573.18759) + 767573.187585.round(5, half: :up).should.eql?(767573.18759) + 767573.187585.round(5, half: :down).should.eql?(767573.18758) + 767573.187585.round(5, half: :even).should.eql?(767573.18758) + -767573.187585.round(5, half: nil).should.eql?(-767573.18759) + -767573.187585.round(5, half: :up).should.eql?(-767573.18759) + -767573.187585.round(5, half: :down).should.eql?(-767573.18758) + -767573.187585.round(5, half: :even).should.eql?(-767573.18758) + 767573.1875849998.round(5, half: nil).should.eql?(767573.18758) + 767573.1875849998.round(5, half: :up).should.eql?(767573.18758) + 767573.1875849998.round(5, half: :down).should.eql?(767573.18758) + 767573.1875849998.round(5, half: :even).should.eql?(767573.18758) + -767573.1875849998.round(5, half: nil).should.eql?(-767573.18758) + -767573.1875849998.round(5, half: :up).should.eql?(-767573.18758) + -767573.1875849998.round(5, half: :down).should.eql?(-767573.18758) + -767573.1875849998.round(5, half: :even).should.eql?(-767573.18758) end it "raises FloatDomainError for exceptional values with a half option" do - -> { (+infinity_value).round(half: :up) }.should raise_error(FloatDomainError) - -> { (-infinity_value).round(half: :down) }.should raise_error(FloatDomainError) - -> { nan_value.round(half: :even) }.should raise_error(FloatDomainError) + -> { (+infinity_value).round(half: :up) }.should.raise(FloatDomainError) + -> { (-infinity_value).round(half: :down) }.should.raise(FloatDomainError) + -> { nan_value.round(half: :even) }.should.raise(FloatDomainError) end it "raise for a non-existent round mode" do - -> { 14.2.round(half: :nonsense) }.should raise_error(ArgumentError, "invalid rounding mode: nonsense") + -> { 14.2.round(half: :nonsense) }.should.raise(ArgumentError, "invalid rounding mode: nonsense") end describe "when 0.0 is given" do @@ -197,7 +196,13 @@ describe "Float#round" do it "returns 0 for 0 or undefined ndigits" do (0.0).round.should == 0 (-0.0).round(0).should == 0 - (0.0).round(half: :up) == 0 + (0.0).round(half: :up).should == 0 + end + + it "returns 0 for negative ndigits" do + (0.0).round(-1).should == 0 + (-0.0).round(-1).should == 0 + (0.0).round(-1, half: :up).should == 0 end end end |
