|
4 | 4 | describe "BigDecimal.new" do |
5 | 5 |
|
6 | 6 | it "creates a new object of class BigDecimal" do |
7 | | - BigDecimal.new("3.14159").should be_kind_of(BigDecimal) |
| 7 | + BigDecimal("3.14159").should be_kind_of(BigDecimal) |
8 | 8 | (0..9).each {|i| |
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).to_f |
| 9 | + BigDecimal("1#{i}").should == 10 + i |
| 10 | + BigDecimal("-1#{i}").should == -10 - i |
| 11 | + BigDecimal("1E#{i}").should == 10**i |
| 12 | + BigDecimal("1000000E-#{i}").should == 10**(6-i).to_f |
13 | 13 | # ^ to_f to avoid Rational type |
14 | 14 | } |
15 | 15 | (1..9).each {|i| |
16 | | - BigDecimal.new("100.#{i}").to_s.should =~ /\A0\.100#{i}E3\z/i |
17 | | - BigDecimal.new("-100.#{i}").to_s.should =~ /\A-0\.100#{i}E3\z/i |
| 16 | + BigDecimal("100.#{i}").to_s.should =~ /\A0\.100#{i}E3\z/i |
| 17 | + BigDecimal("-100.#{i}").to_s.should =~ /\A-0\.100#{i}E3\z/i |
18 | 18 | } |
19 | 19 | end |
20 | 20 |
|
21 | 21 | it "accepts significant digits >= given precision" do |
22 | | - BigDecimal.new("3.1415923", 10).precs[1].should >= 10 |
| 22 | + BigDecimal("3.1415923", 10).precs[1].should >= 10 |
23 | 23 | end |
24 | 24 |
|
25 | 25 | it "determines precision from initial value" do |
26 | 26 | pi_string = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043" |
27 | | - BigDecimal.new(pi_string).precs[1].should >= pi_string.size-1 |
| 27 | + BigDecimal(pi_string).precs[1].should >= pi_string.size-1 |
28 | 28 | end |
29 | 29 |
|
30 | 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 |
| 31 | + BigDecimal(" \t\n \r1234").should == BigDecimal("1234") |
| 32 | + BigDecimal(" \t\n \rNaN \n").nan?.should == true |
| 33 | + BigDecimal(" \t\n \rInfinity \n").infinite?.should == 1 |
| 34 | + BigDecimal(" \t\n \r-Infinity \n").infinite?.should == -1 |
35 | 35 | end |
36 | 36 |
|
37 | 37 | it "ignores trailing garbage" do |
38 | | - BigDecimal.new("123E45ruby").should == BigDecimal.new("123E45") |
39 | | - BigDecimal.new("123x45").should == BigDecimal.new("123") |
40 | | - BigDecimal.new("123.4%E5").should == BigDecimal.new("123.4") |
41 | | - BigDecimal.new("1E2E3E4E5E").should == BigDecimal.new("100") |
| 38 | + BigDecimal("123E45ruby").should == BigDecimal("123E45") |
| 39 | + BigDecimal("123x45").should == BigDecimal("123") |
| 40 | + BigDecimal("123.4%E5").should == BigDecimal("123.4") |
| 41 | + BigDecimal("1E2E3E4E5E").should == BigDecimal("100") |
42 | 42 | end |
43 | 43 |
|
44 | 44 | ruby_version_is ""..."2.4" do |
45 | 45 | it "treats invalid strings as 0.0" do |
46 | | - BigDecimal.new("ruby").should == BigDecimal.new("0.0") |
47 | | - BigDecimal.new(" \t\n \r-\t\t\tInfinity \n").should == BigDecimal.new("0.0") |
| 46 | + BigDecimal("ruby").should == BigDecimal("0.0") |
| 47 | + BigDecimal(" \t\n \r-\t\t\tInfinity \n").should == BigDecimal("0.0") |
48 | 48 | end |
49 | 49 | end |
50 | 50 |
|
51 | 51 | ruby_version_is "2.4" do |
52 | 52 | it "raises ArgumentError for invalid strings" do |
53 | | - lambda { BigDecimal.new("ruby") }.should raise_error(ArgumentError) |
54 | | - lambda { BigDecimal.new(" \t\n \r-\t\t\tInfinity \n") }.should raise_error(ArgumentError) |
| 53 | + lambda { BigDecimal("ruby") }.should raise_error(ArgumentError) |
| 54 | + lambda { BigDecimal(" \t\n \r-\t\t\tInfinity \n") }.should raise_error(ArgumentError) |
55 | 55 | end |
56 | 56 | end |
57 | 57 |
|
58 | 58 | it "allows omitting the integer part" do |
59 | | - BigDecimal.new(".123").should == BigDecimal.new("0.123") |
| 59 | + BigDecimal(".123").should == BigDecimal("0.123") |
60 | 60 | end |
61 | 61 |
|
62 | 62 | it "allows for underscores in all parts" do |
63 | | - reference = BigDecimal.new("12345.67E89") |
| 63 | + reference = BigDecimal("12345.67E89") |
64 | 64 |
|
65 | | - BigDecimal.new("12_345.67E89").should == reference |
66 | | - BigDecimal.new("1_2_3_4_5_._6____7_E89").should == reference |
67 | | - BigDecimal.new("12345_.67E_8__9_").should == reference |
| 65 | + BigDecimal("12_345.67E89").should == reference |
| 66 | + BigDecimal("1_2_3_4_5_._6____7_E89").should == reference |
| 67 | + BigDecimal("12345_.67E_8__9_").should == reference |
68 | 68 | end |
69 | 69 |
|
70 | 70 | it "accepts NaN and [+-]Infinity" do |
71 | | - BigDecimal.new("NaN").nan?.should == true |
| 71 | + BigDecimal("NaN").nan?.should == true |
72 | 72 |
|
73 | | - pos_inf = BigDecimal.new("Infinity") |
| 73 | + pos_inf = BigDecimal("Infinity") |
74 | 74 | pos_inf.finite?.should == false |
75 | 75 | pos_inf.should > 0 |
76 | | - pos_inf.should == BigDecimal.new("+Infinity") |
| 76 | + pos_inf.should == BigDecimal("+Infinity") |
77 | 77 |
|
78 | | - neg_inf = BigDecimal.new("-Infinity") |
| 78 | + neg_inf = BigDecimal("-Infinity") |
79 | 79 | neg_inf.finite?.should == false |
80 | 80 | neg_inf.should < 0 |
81 | 81 | end |
82 | 82 |
|
83 | 83 | it "allows for [eEdD] as exponent separator" do |
84 | | - reference = BigDecimal.new("12345.67E89") |
| 84 | + reference = BigDecimal("12345.67E89") |
85 | 85 |
|
86 | | - BigDecimal.new("12345.67e89").should == reference |
87 | | - BigDecimal.new("12345.67E89").should == reference |
88 | | - BigDecimal.new("12345.67d89").should == reference |
89 | | - BigDecimal.new("12345.67D89").should == reference |
| 86 | + BigDecimal("12345.67e89").should == reference |
| 87 | + BigDecimal("12345.67E89").should == reference |
| 88 | + BigDecimal("12345.67d89").should == reference |
| 89 | + BigDecimal("12345.67D89").should == reference |
90 | 90 | end |
91 | 91 |
|
92 | 92 | it "allows for varying signs" do |
93 | | - reference = BigDecimal.new("123.456E1") |
94 | | - |
95 | | - BigDecimal.new("+123.456E1").should == reference |
96 | | - BigDecimal.new("-123.456E1").should == -reference |
97 | | - BigDecimal.new("123.456E+1").should == reference |
98 | | - BigDecimal.new("12345.6E-1").should == reference |
99 | | - BigDecimal.new("+123.456E+1").should == reference |
100 | | - BigDecimal.new("+12345.6E-1").should == reference |
101 | | - BigDecimal.new("-123.456E+1").should == -reference |
102 | | - BigDecimal.new("-12345.6E-1").should == -reference |
| 93 | + reference = BigDecimal("123.456E1") |
| 94 | + |
| 95 | + BigDecimal("+123.456E1").should == reference |
| 96 | + BigDecimal("-123.456E1").should == -reference |
| 97 | + BigDecimal("123.456E+1").should == reference |
| 98 | + BigDecimal("12345.6E-1").should == reference |
| 99 | + BigDecimal("+123.456E+1").should == reference |
| 100 | + BigDecimal("+12345.6E-1").should == reference |
| 101 | + BigDecimal("-123.456E+1").should == -reference |
| 102 | + BigDecimal("-12345.6E-1").should == -reference |
103 | 103 | end |
104 | 104 |
|
105 | 105 | it 'raises ArgumentError when Float is used without precision' do |
|
0 commit comments