2018-04-28 15:50:06 -04:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require 'bigdecimal'
|
|
|
|
|
2019-02-21 10:38:59 -05:00
|
|
|
describe "BigDecimal" do
|
|
|
|
it "is not defined unless it is required" do
|
|
|
|
ruby_exe('puts Object.const_defined?(:BigDecimal)').should == "false\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-28 15:50:06 -04:00
|
|
|
describe "Kernel#BigDecimal" do
|
|
|
|
|
|
|
|
it "creates a new object of class BigDecimal" do
|
|
|
|
BigDecimal("3.14159").should be_kind_of(BigDecimal)
|
|
|
|
(0..9).each {|i|
|
|
|
|
BigDecimal("1#{i}").should == 10 + i
|
|
|
|
BigDecimal("-1#{i}").should == -10 - i
|
|
|
|
BigDecimal("1E#{i}").should == 10**i
|
|
|
|
BigDecimal("1000000E-#{i}").should == 10**(6-i).to_f
|
|
|
|
# ^ to_f to avoid Rational type
|
|
|
|
}
|
|
|
|
(1..9).each {|i|
|
|
|
|
BigDecimal("100.#{i}").to_s.should =~ /\A0\.100#{i}E3\z/i
|
|
|
|
BigDecimal("-100.#{i}").to_s.should =~ /\A-0\.100#{i}E3\z/i
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-11-30 15:26:52 -05:00
|
|
|
it "BigDecimal(Rational) with bigger-than-double numerator" do
|
|
|
|
rational = 99999999999999999999/100r
|
|
|
|
rational.numerator.should > 2**64
|
|
|
|
BigDecimal(rational, 100).to_s.should == "0.99999999999999999999e18"
|
|
|
|
end
|
|
|
|
|
2020-12-27 11:35:32 -05:00
|
|
|
it "accepts significant digits >= given precision" do
|
|
|
|
suppress_warning do
|
2020-12-19 13:14:58 -05:00
|
|
|
BigDecimal("3.1415923", 10).precs[1].should >= 10
|
|
|
|
end
|
2020-12-27 11:35:32 -05:00
|
|
|
end
|
2018-04-28 15:50:06 -04:00
|
|
|
|
2020-12-27 11:35:32 -05:00
|
|
|
it "determines precision from initial value" do
|
|
|
|
pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043"
|
|
|
|
suppress_warning {
|
|
|
|
BigDecimal(pi_string).precs[1]
|
|
|
|
}.should >= pi_string.size-1
|
2018-04-28 15:50:06 -04:00
|
|
|
end
|
|
|
|
|
2019-11-30 15:26:52 -05:00
|
|
|
it "ignores leading and trailing whitespace" do
|
|
|
|
BigDecimal(" \t\n \r1234\t\r\n ").should == BigDecimal("1234")
|
2020-05-03 06:28:29 -04:00
|
|
|
BigDecimal(" \t\n \rNaN \n").should.nan?
|
2018-04-28 15:50:06 -04:00
|
|
|
BigDecimal(" \t\n \rInfinity \n").infinite?.should == 1
|
|
|
|
BigDecimal(" \t\n \r-Infinity \n").infinite?.should == -1
|
|
|
|
end
|
|
|
|
|
2019-12-27 10:46:08 -05:00
|
|
|
it "coerces the value argument with #to_str" do
|
|
|
|
initial = mock("value")
|
|
|
|
initial.should_receive(:to_str).and_return("123")
|
|
|
|
BigDecimal(initial).should == BigDecimal("123")
|
|
|
|
end
|
|
|
|
|
2018-12-05 06:30:24 -05:00
|
|
|
ruby_version_is ""..."2.6" do
|
|
|
|
it "ignores trailing garbage" do
|
|
|
|
BigDecimal("123E45ruby").should == BigDecimal("123E45")
|
|
|
|
BigDecimal("123x45").should == BigDecimal("123")
|
|
|
|
BigDecimal("123.4%E5").should == BigDecimal("123.4")
|
|
|
|
BigDecimal("1E2E3E4E5E").should == BigDecimal("100")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ruby_version_is "2.6" do
|
|
|
|
it "does not ignores trailing garbage" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { BigDecimal("123E45ruby") }.should raise_error(ArgumentError)
|
|
|
|
-> { BigDecimal("123x45") }.should raise_error(ArgumentError)
|
|
|
|
-> { BigDecimal("123.4%E5") }.should raise_error(ArgumentError)
|
|
|
|
-> { BigDecimal("1E2E3E4E5E") }.should raise_error(ArgumentError)
|
2018-12-05 06:30:24 -05:00
|
|
|
end
|
2018-04-28 15:50:06 -04:00
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "raises ArgumentError for invalid strings" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { BigDecimal("ruby") }.should raise_error(ArgumentError)
|
|
|
|
-> { BigDecimal(" \t\n \r-\t\t\tInfinity \n") }.should raise_error(ArgumentError)
|
2018-04-28 15:50:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows omitting the integer part" do
|
|
|
|
BigDecimal(".123").should == BigDecimal("0.123")
|
|
|
|
end
|
|
|
|
|
2018-12-05 06:30:24 -05:00
|
|
|
ruby_version_is ""..."2.6" do
|
|
|
|
it "allows for underscores in all parts" do
|
|
|
|
reference = BigDecimal("12345.67E89")
|
2018-04-28 15:50:06 -04:00
|
|
|
|
2018-12-05 06:30:24 -05:00
|
|
|
BigDecimal("12_345.67E89").should == reference
|
|
|
|
BigDecimal("1_2_3_4_5_._6____7_E89").should == reference
|
|
|
|
BigDecimal("12345_.67E_8__9_").should == reference
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ruby_version_is "2.6" do
|
|
|
|
it "process underscores as Float()" do
|
|
|
|
reference = BigDecimal("12345.67E89")
|
|
|
|
|
|
|
|
BigDecimal("12_345.67E89").should == reference
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { BigDecimal("1_2_3_4_5_._6____7_E89") }.should raise_error(ArgumentError)
|
|
|
|
-> { BigDecimal("12345_.67E_8__9_") }.should raise_error(ArgumentError)
|
2018-12-05 06:30:24 -05:00
|
|
|
end
|
2018-04-28 15:50:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts NaN and [+-]Infinity" do
|
2020-05-03 06:28:29 -04:00
|
|
|
BigDecimal("NaN").should.nan?
|
2018-04-28 15:50:06 -04:00
|
|
|
|
|
|
|
pos_inf = BigDecimal("Infinity")
|
2020-05-03 06:28:29 -04:00
|
|
|
pos_inf.should_not.finite?
|
2018-04-28 15:50:06 -04:00
|
|
|
pos_inf.should > 0
|
|
|
|
pos_inf.should == BigDecimal("+Infinity")
|
|
|
|
|
|
|
|
neg_inf = BigDecimal("-Infinity")
|
2020-05-03 06:28:29 -04:00
|
|
|
neg_inf.should_not.finite?
|
2018-04-28 15:50:06 -04:00
|
|
|
neg_inf.should < 0
|
|
|
|
end
|
|
|
|
|
2020-05-31 12:22:49 -04:00
|
|
|
ruby_version_is "2.6" do
|
|
|
|
describe "with exception: false" do
|
|
|
|
it "returns nil for invalid strings" do
|
|
|
|
BigDecimal("invalid", exception: false).should be_nil
|
|
|
|
BigDecimal("0invalid", exception: false).should be_nil
|
|
|
|
BigDecimal("invalid0", exception: false).should be_nil
|
|
|
|
BigDecimal("0.", exception: false).should be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-28 16:41:48 -04:00
|
|
|
describe "accepts NaN and [+-]Infinity as Float values" do
|
|
|
|
it "works without an explicit precision" do
|
2020-05-03 06:28:29 -04:00
|
|
|
BigDecimal(Float::NAN).should.nan?
|
2019-05-28 16:41:48 -04:00
|
|
|
|
|
|
|
pos_inf = BigDecimal(Float::INFINITY)
|
2020-05-03 06:28:29 -04:00
|
|
|
pos_inf.should_not.finite?
|
2019-05-28 16:41:48 -04:00
|
|
|
pos_inf.should > 0
|
|
|
|
pos_inf.should == BigDecimal("+Infinity")
|
|
|
|
|
|
|
|
neg_inf = BigDecimal(-Float::INFINITY)
|
2020-05-03 06:28:29 -04:00
|
|
|
neg_inf.should_not.finite?
|
2019-05-28 16:41:48 -04:00
|
|
|
neg_inf.should < 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works with an explicit precision" do
|
2020-05-03 06:28:29 -04:00
|
|
|
BigDecimal(Float::NAN, Float::DIG).should.nan?
|
2019-05-28 16:41:48 -04:00
|
|
|
|
|
|
|
pos_inf = BigDecimal(Float::INFINITY, Float::DIG)
|
2020-05-03 06:28:29 -04:00
|
|
|
pos_inf.should_not.finite?
|
2019-05-28 16:41:48 -04:00
|
|
|
pos_inf.should > 0
|
|
|
|
pos_inf.should == BigDecimal("+Infinity")
|
|
|
|
|
|
|
|
neg_inf = BigDecimal(-Float::INFINITY, Float::DIG)
|
2020-05-03 06:28:29 -04:00
|
|
|
neg_inf.should_not.finite?
|
2019-05-28 16:41:48 -04:00
|
|
|
neg_inf.should < 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-28 15:50:06 -04:00
|
|
|
it "allows for [eEdD] as exponent separator" do
|
|
|
|
reference = BigDecimal("12345.67E89")
|
|
|
|
|
|
|
|
BigDecimal("12345.67e89").should == reference
|
|
|
|
BigDecimal("12345.67E89").should == reference
|
|
|
|
BigDecimal("12345.67d89").should == reference
|
|
|
|
BigDecimal("12345.67D89").should == reference
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows for varying signs" do
|
|
|
|
reference = BigDecimal("123.456E1")
|
|
|
|
|
|
|
|
BigDecimal("+123.456E1").should == reference
|
|
|
|
BigDecimal("-123.456E1").should == -reference
|
|
|
|
BigDecimal("123.456E+1").should == reference
|
|
|
|
BigDecimal("12345.6E-1").should == reference
|
|
|
|
BigDecimal("+123.456E+1").should == reference
|
|
|
|
BigDecimal("+12345.6E-1").should == reference
|
|
|
|
BigDecimal("-123.456E+1").should == -reference
|
|
|
|
BigDecimal("-12345.6E-1").should == -reference
|
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "raises ArgumentError when Float is used without precision" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { BigDecimal(1.0) }.should raise_error(ArgumentError)
|
2018-04-28 15:50:06 -04:00
|
|
|
end
|
|
|
|
|
2019-06-27 15:02:36 -04:00
|
|
|
it "returns appropriate BigDecimal zero for signed zero" do
|
|
|
|
BigDecimal(-0.0, Float::DIG).sign.should == -1
|
|
|
|
BigDecimal(0.0, Float::DIG).sign.should == 1
|
|
|
|
end
|
|
|
|
|
2019-11-30 15:26:52 -05:00
|
|
|
it "pre-coerces long integers" do
|
|
|
|
BigDecimal(3).add(1 << 50, 3).should == BigDecimal('0.113e16')
|
|
|
|
end
|
|
|
|
|
2020-03-27 19:22:51 -04:00
|
|
|
it "does not call to_s when calling inspect" do
|
|
|
|
value = BigDecimal('44.44')
|
|
|
|
value.to_s.should == '0.4444e2'
|
|
|
|
value.inspect.should == '0.4444e2'
|
|
|
|
|
|
|
|
ruby_exe( <<-'EOF').should == "cheese 0.4444e2"
|
|
|
|
require 'bigdecimal'
|
|
|
|
module BigDecimalOverride
|
|
|
|
def to_s; "cheese"; end
|
|
|
|
end
|
|
|
|
BigDecimal.prepend BigDecimalOverride
|
|
|
|
value = BigDecimal('44.44')
|
|
|
|
print "#{value.to_s} #{value.inspect}"
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2019-11-30 15:26:52 -05:00
|
|
|
describe "when interacting with Rational" do
|
|
|
|
before :each do
|
|
|
|
@a = BigDecimal('166.666666666')
|
|
|
|
@b = Rational(500, 3)
|
|
|
|
@c = @a - @b
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check the input is as we understand it
|
|
|
|
|
|
|
|
it "has the LHS print as expected" do
|
|
|
|
@a.to_s.should == "0.166666666666e3"
|
|
|
|
@a.to_f.to_s.should == "166.666666666"
|
|
|
|
Float(@a).to_s.should == "166.666666666"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has the RHS print as expected" do
|
|
|
|
@b.to_s.should == "500/3"
|
|
|
|
@b.to_f.to_s.should == "166.66666666666666"
|
|
|
|
Float(@b).to_s.should == "166.66666666666666"
|
|
|
|
end
|
|
|
|
|
2020-12-27 11:35:32 -05:00
|
|
|
it "has the expected precision on the LHS" do
|
|
|
|
suppress_warning { @a.precs[0] }.should == 18
|
|
|
|
end
|
2019-11-30 15:26:52 -05:00
|
|
|
|
2020-12-27 11:35:32 -05:00
|
|
|
it "has the expected maximum precision on the LHS" do
|
|
|
|
suppress_warning { @a.precs[1] }.should == 27
|
2019-11-30 15:26:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "produces the expected result when done via Float" do
|
|
|
|
(Float(@a) - Float(@b)).to_s.should == "-6.666596163995564e-10"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "produces the expected result when done via to_f" do
|
|
|
|
(@a.to_f - @b.to_f).to_s.should == "-6.666596163995564e-10"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check underlying methods work as we understand
|
|
|
|
|
2020-12-27 11:35:32 -05:00
|
|
|
it "BigDecimal precision is the number of digits rounded up to a multiple of nine" do
|
|
|
|
1.upto(100) do |n|
|
|
|
|
b = BigDecimal('4' * n)
|
|
|
|
precs, _ = suppress_warning { b.precs }
|
|
|
|
(precs >= 9).should be_true
|
|
|
|
(precs >= n).should be_true
|
|
|
|
(precs % 9).should == 0
|
2019-11-30 15:26:52 -05:00
|
|
|
end
|
2020-12-27 11:35:32 -05:00
|
|
|
suppress_warning { BigDecimal('NaN').precs[0] }.should == 9
|
|
|
|
end
|
2019-11-30 15:26:52 -05:00
|
|
|
|
2020-12-27 11:35:32 -05:00
|
|
|
it "BigDecimal maximum precision is nine more than precision except for abnormals" do
|
|
|
|
1.upto(100) do |n|
|
|
|
|
b = BigDecimal('4' * n)
|
|
|
|
precs, max = suppress_warning { b.precs }
|
|
|
|
max.should == precs + 9
|
2019-11-30 15:26:52 -05:00
|
|
|
end
|
2020-12-27 11:35:32 -05:00
|
|
|
suppress_warning { BigDecimal('NaN').precs[1] }.should == 9
|
2019-11-30 15:26:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "BigDecimal(Rational, 18) produces the result we expect" do
|
|
|
|
BigDecimal(@b, 18).to_s.should == "0.166666666666666667e3"
|
|
|
|
end
|
|
|
|
|
2020-12-27 11:35:32 -05:00
|
|
|
it "BigDecimal(Rational, BigDecimal.precs[0]) produces the result we expect" do
|
|
|
|
BigDecimal(@b, suppress_warning { @a.precs[0] }).to_s.should == "0.166666666666666667e3"
|
2019-11-30 15:26:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check the top-level expression works as we expect
|
|
|
|
|
|
|
|
it "produces a BigDecimal" do
|
|
|
|
@c.class.should == BigDecimal
|
|
|
|
end
|
|
|
|
|
|
|
|
it "produces the expected result" do
|
|
|
|
@c.should == BigDecimal("-0.666667e-9")
|
|
|
|
@c.to_s.should == "-0.666667e-9"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "produces the correct class for other arithmetic operators" do
|
|
|
|
(@a + @b).class.should == BigDecimal
|
|
|
|
(@a * @b).class.should == BigDecimal
|
|
|
|
(@a / @b).class.should == BigDecimal
|
|
|
|
(@a % @b).class.should == BigDecimal
|
|
|
|
end
|
|
|
|
end
|
2018-04-28 15:50:06 -04:00
|
|
|
end
|