1 require File.dirname(__FILE__) + '/../../spec_helper'
4 describe "BigDecimal.new" do
6 it "creates a new object of class BigDecimal" do
7 BigDecimal.new("3.14159").class.should == BigDecimal
9 BigDecimal.new("1#{i}").should == 10 + i
10 BigDecimal.new("-1#{i}").should == -10 - i
11 BigDecimal.new("1E#{i}").should == 10**i
12 BigDecimal.new("1000000E-#{i}").should == 10**(6-i)
15 BigDecimal.new("100.#{i}").to_s.should == "0.100#{i}E3"
16 BigDecimal.new("-100.#{i}").to_s.should == "-0.100#{i}E3"
20 it "Number of significant digits >= given precision" do
21 pi_string = "3.1415923"
22 BigDecimal.new("3.1415923", 10).precs[1].should >= 10
25 it "determines precision from initial value" do
26 pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043"
27 BigDecimal.new(pi_string).precs[1].should >= pi_string.size
30 it "ignores leading whitespace" do
31 BigDecimal.new(" \t\n \r1234").should == BigDecimal.new("1234")
32 BigDecimal.new(" \t\n \rNaN \n").nan?.should == true
33 BigDecimal.new(" \t\n \rInfinity \n").infinite?.should == 1
34 BigDecimal.new(" \t\n \r-Infinity \n").infinite?.should == -1
35 BigDecimal.new(" \t\n \r-\t\t\tInfinity \n").should == 0.0
38 it "ignores trailing garbage" do
39 BigDecimal.new("123E45ruby").should == BigDecimal.new("123E45")
40 BigDecimal.new("123x45").should == BigDecimal.new("123")
41 BigDecimal.new("123.4%E5").should == BigDecimal.new("123.4")
42 BigDecimal.new("1E2E3E4E5E").should == BigDecimal.new("100")
45 it "treats invalid strings as 0.0" do
46 BigDecimal.new("ruby").should == BigDecimal.new("0.0")
49 it "allows omitting the integer part" do
50 BigDecimal.new(".123").should == BigDecimal.new("0.123")
51 BigDecimal.new("-.123").should == BigDecimal.new("-0.123")
54 it "allows for underscores in all parts" do
55 reference = BigDecimal.new("12345.67E89")
57 BigDecimal.new("12_345.67E89").should == reference
58 BigDecimal.new("1_2_3_4_5_._6____7_E89").should == reference
59 BigDecimal.new("12345_.67E_8__9_").should == reference
62 it "accepts NaN and [+-]Infinity" do
63 BigDecimal.new("NaN").nan?.should == true
65 pos_inf = BigDecimal.new("Infinity")
66 pos_inf.finite?.should == false
68 pos_inf.should == BigDecimal.new("+Infinity")
70 neg_inf = BigDecimal.new("-Infinity")
71 neg_inf.finite?.should == false
75 it "allows for [eEdD] as exponent separator" do
76 reference = BigDecimal.new("12345.67E89")
78 BigDecimal.new("12345.67e89").should == reference
79 BigDecimal.new("12345.67E89").should == reference
80 BigDecimal.new("12345.67d89").should == reference
81 BigDecimal.new("12345.67D89").should == reference
84 it "allows for varying signs" do
85 reference = BigDecimal.new("123.456E1")
87 BigDecimal.new("+123.456E1").should == reference
88 BigDecimal.new("-123.456E1").should == -reference
89 BigDecimal.new("123.456E+1").should == reference
90 BigDecimal.new("12345.6E-1").should == reference
91 BigDecimal.new("+123.456E+1").should == reference
92 BigDecimal.new("+12345.6E-1").should == reference
93 BigDecimal.new("-123.456E+1").should == -reference
94 BigDecimal.new("-12345.6E-1").should == -reference