rubysl-bigdecimal 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE +25 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/ext/rubysl/bigdecimal/bigdecimal.c +4760 -0
- data/ext/rubysl/bigdecimal/bigdecimal.h +220 -0
- data/ext/rubysl/bigdecimal/extconf.rb +6 -0
- data/lib/bigdecimal.rb +1 -0
- data/lib/bigdecimal/README +60 -0
- data/lib/bigdecimal/bigdecimal_en.html +796 -0
- data/lib/bigdecimal/bigdecimal_ja.html +799 -0
- data/lib/bigdecimal/jacobian.rb +85 -0
- data/lib/bigdecimal/ludcmp.rb +84 -0
- data/lib/bigdecimal/math.rb +235 -0
- data/lib/bigdecimal/newton.rb +77 -0
- data/lib/bigdecimal/sample/linear.rb +71 -0
- data/lib/bigdecimal/sample/nlsolve.rb +38 -0
- data/lib/bigdecimal/sample/pi.rb +20 -0
- data/lib/bigdecimal/util.rb +65 -0
- data/lib/rubysl/bigdecimal.rb +2 -0
- data/lib/rubysl/bigdecimal/version.rb +5 -0
- data/rubysl-bigdecimal.gemspec +24 -0
- data/spec/abs_spec.rb +49 -0
- data/spec/add_spec.rb +178 -0
- data/spec/case_compare_spec.rb +6 -0
- data/spec/ceil_spec.rb +122 -0
- data/spec/coerce_spec.rb +25 -0
- data/spec/comparison_spec.rb +80 -0
- data/spec/div_spec.rb +143 -0
- data/spec/divide_spec.rb +6 -0
- data/spec/divmod_spec.rb +233 -0
- data/spec/double_fig_spec.rb +8 -0
- data/spec/eql_spec.rb +5 -0
- data/spec/equal_value_spec.rb +6 -0
- data/spec/exponent_spec.rb +37 -0
- data/spec/finite_spec.rb +34 -0
- data/spec/fix_spec.rb +56 -0
- data/spec/fixtures/classes.rb +17 -0
- data/spec/floor_spec.rb +109 -0
- data/spec/frac_spec.rb +47 -0
- data/spec/gt_spec.rb +86 -0
- data/spec/gte_spec.rb +90 -0
- data/spec/induced_from_spec.rb +36 -0
- data/spec/infinite_spec.rb +31 -0
- data/spec/inspect_spec.rb +40 -0
- data/spec/limit_spec.rb +29 -0
- data/spec/lt_spec.rb +84 -0
- data/spec/lte_spec.rb +90 -0
- data/spec/minus_spec.rb +57 -0
- data/spec/mode_spec.rb +64 -0
- data/spec/modulo_spec.rb +11 -0
- data/spec/mult_spec.rb +23 -0
- data/spec/multiply_spec.rb +25 -0
- data/spec/nan_spec.rb +22 -0
- data/spec/new_spec.rb +120 -0
- data/spec/nonzero_spec.rb +28 -0
- data/spec/plus_spec.rb +49 -0
- data/spec/power_spec.rb +5 -0
- data/spec/precs_spec.rb +48 -0
- data/spec/quo_spec.rb +12 -0
- data/spec/remainder_spec.rb +83 -0
- data/spec/round_spec.rb +193 -0
- data/spec/shared/eql.rb +65 -0
- data/spec/shared/modulo.rb +146 -0
- data/spec/shared/mult.rb +97 -0
- data/spec/shared/power.rb +83 -0
- data/spec/shared/quo.rb +59 -0
- data/spec/shared/to_int.rb +27 -0
- data/spec/sign_spec.rb +46 -0
- data/spec/split_spec.rb +87 -0
- data/spec/sqrt_spec.rb +111 -0
- data/spec/sub_spec.rb +52 -0
- data/spec/to_f_spec.rb +54 -0
- data/spec/to_i_spec.rb +6 -0
- data/spec/to_int_spec.rb +7 -0
- data/spec/to_s_spec.rb +71 -0
- data/spec/truncate_spec.rb +100 -0
- data/spec/uminus_spec.rb +57 -0
- data/spec/uplus_spec.rb +19 -0
- data/spec/ver_spec.rb +10 -0
- data/spec/zero_spec.rb +27 -0
- metadata +243 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe :bigdecimal_to_int , :shared => true do
|
4
|
+
ruby_version_is "" ... "1.9" do
|
5
|
+
ruby_bug "fixed_in_ruby_1_8_7@25799", "1.8.7.202" do
|
6
|
+
it "returns nil if BigDecimal is infinity or NaN" do
|
7
|
+
BigDecimal("Infinity").send(@method).should == nil
|
8
|
+
BigDecimal("NaN").send(@method).should == nil
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
ruby_version_is "1.9" do
|
14
|
+
it "raises FloatDomainError if BigDecimal is infinity or NaN" do
|
15
|
+
lambda { BigDecimal("Infinity").send(@method) }.should raise_error(FloatDomainError)
|
16
|
+
lambda { BigDecimal("NaN").send(@method) }.should raise_error(FloatDomainError)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns Integer or Bignum otherwise" do
|
21
|
+
BigDecimal("3E-20001").send(@method).should == 0
|
22
|
+
BigDecimal("2E4000").send(@method).should == 2 * 10 ** 4000
|
23
|
+
BigDecimal("2").send(@method).should == 2
|
24
|
+
BigDecimal("2E10").send(@method).should == 20000000000
|
25
|
+
BigDecimal("3.14159").send(@method).should == 3
|
26
|
+
end
|
27
|
+
end
|
data/spec/sign_spec.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#sign" do
|
4
|
+
|
5
|
+
it "BigDecimal defines several constants for signs" do
|
6
|
+
# are these really correct?
|
7
|
+
BigDecimal::SIGN_POSITIVE_INFINITE.should == 3
|
8
|
+
BigDecimal::SIGN_NEGATIVE_INFINITE.should == -3
|
9
|
+
BigDecimal::SIGN_POSITIVE_ZERO.should == 1
|
10
|
+
BigDecimal::SIGN_NEGATIVE_ZERO.should == -1
|
11
|
+
BigDecimal::SIGN_POSITIVE_FINITE.should == 2
|
12
|
+
BigDecimal::SIGN_NEGATIVE_FINITE.should == -2
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns positive value if BigDecimal greater than 0" do
|
16
|
+
BigDecimal("1").sign.should == BigDecimal::SIGN_POSITIVE_FINITE
|
17
|
+
BigDecimal("1E-20000000").sign.should == BigDecimal::SIGN_POSITIVE_FINITE
|
18
|
+
BigDecimal("1E200000000").sign.should == BigDecimal::SIGN_POSITIVE_FINITE
|
19
|
+
BigDecimal("Infinity").sign.should == BigDecimal::SIGN_POSITIVE_INFINITE
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns negative value if BigDecimal less than 0" do
|
23
|
+
BigDecimal("-1").sign.should == BigDecimal::SIGN_NEGATIVE_FINITE
|
24
|
+
BigDecimal("-1E-9990000").sign.should == BigDecimal::SIGN_NEGATIVE_FINITE
|
25
|
+
BigDecimal("-1E20000000").sign.should == BigDecimal::SIGN_NEGATIVE_FINITE
|
26
|
+
BigDecimal("-Infinity").sign.should == BigDecimal::SIGN_NEGATIVE_INFINITE
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns positive zero if BigDecimal equals positve zero" do
|
30
|
+
BigDecimal("0").sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
31
|
+
BigDecimal("0E-200000000").sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
32
|
+
BigDecimal("0E200000000").sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns negative zero if BigDecimal equals negative zero" do
|
36
|
+
BigDecimal("-0").sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
37
|
+
BigDecimal("-0E-200000000").sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
38
|
+
BigDecimal("-0E200000000").sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns BigDecimal::SIGN_NaN if BigDecimal is NaN" do
|
42
|
+
BigDecimal("NaN").sign.should == BigDecimal::SIGN_NaN
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
data/spec/split_spec.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#split" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@arr = BigDecimal("0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1").split
|
7
|
+
@arr_neg = BigDecimal("-0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043E1").split
|
8
|
+
@digits = "922337203685477580810101333333333333333333333333333"
|
9
|
+
@arr_big = BigDecimal("00#{@digits}000").split
|
10
|
+
@arr_big_neg = BigDecimal("-00#{@digits}000").split
|
11
|
+
@huge = BigDecimal('100000000000000000000000000000000000000000001E90000000').split
|
12
|
+
|
13
|
+
@infinity = BigDecimal("Infinity")
|
14
|
+
@infinity_neg = BigDecimal("-Infinity")
|
15
|
+
@nan = BigDecimal("NaN")
|
16
|
+
@zero = BigDecimal("0")
|
17
|
+
@zero_neg = BigDecimal("-0")
|
18
|
+
end
|
19
|
+
|
20
|
+
it "splits BigDecimal in an array with four values" do
|
21
|
+
@arr.size.should == 4
|
22
|
+
end
|
23
|
+
|
24
|
+
it "First value: 1 for numbers > 0" do
|
25
|
+
@arr[0].should == 1
|
26
|
+
@arr_big[0].should == 1
|
27
|
+
@zero.split[0].should == 1
|
28
|
+
@huge[0].should == 1
|
29
|
+
BigDecimal("+0").split[0].should == 1
|
30
|
+
BigDecimal("1E400").split[0].should == 1
|
31
|
+
@infinity.split[0].should == 1
|
32
|
+
end
|
33
|
+
|
34
|
+
it "First value: -1 for numbers < 0" do
|
35
|
+
@arr_neg[0].should == -1
|
36
|
+
@arr_big_neg[0].should == -1
|
37
|
+
@zero_neg.split[0].should == -1
|
38
|
+
BigDecimal("-1E400").split[0].should == -1
|
39
|
+
@infinity_neg.split[0].should == -1
|
40
|
+
end
|
41
|
+
|
42
|
+
it "First value: 0 if BigDecimal is NaN" do
|
43
|
+
BigDecimal("NaN").split[0].should == 0
|
44
|
+
end
|
45
|
+
|
46
|
+
it "Second value: a string with the significant digits" do
|
47
|
+
string = "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593014782083152134043"
|
48
|
+
@arr[1].should == string
|
49
|
+
@arr_big[1].should == @digits
|
50
|
+
@arr_big_neg[1].should == @digits
|
51
|
+
@huge[1].should == "100000000000000000000000000000000000000000001"
|
52
|
+
@infinity.split[1].should == @infinity.to_s
|
53
|
+
@nan.split[1].should == @nan.to_s
|
54
|
+
@infinity_neg.split[1].should == @infinity.to_s
|
55
|
+
@zero.split[1].should == "0"
|
56
|
+
BigDecimal("-0").split[1].should == "0"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "Third value: the base (currently always ten)" do
|
60
|
+
@arr[2].should == 10
|
61
|
+
@arr_neg[2].should == 10
|
62
|
+
@arr_big[2].should == 10
|
63
|
+
@arr_big_neg[2].should == 10
|
64
|
+
@huge[2].should == 10
|
65
|
+
@infinity.split[2].should == 10
|
66
|
+
@nan.split[2].should == 10
|
67
|
+
@infinity_neg.split[2].should == 10
|
68
|
+
@zero.split[2].should == 10
|
69
|
+
@zero_neg.split[2].should == 10
|
70
|
+
end
|
71
|
+
|
72
|
+
it "Fourth value: The exponent" do
|
73
|
+
@arr[3].should == 1
|
74
|
+
@arr_neg[3].should == 1
|
75
|
+
@arr_big[3].should == 54
|
76
|
+
@arr_big_neg[3].should == 54
|
77
|
+
@huge[3].should == 90000045
|
78
|
+
@infinity.split[3].should == 0
|
79
|
+
@nan.split[3].should == 0
|
80
|
+
@infinity_neg.split[3].should == 0
|
81
|
+
@zero.split[3].should == 0
|
82
|
+
@zero_neg.split[3].should == 0
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
|
data/spec/sqrt_spec.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
2
|
+
require 'bigdecimal'
|
3
|
+
|
4
|
+
describe "BigDecimal#sqrt" do
|
5
|
+
before(:each) do
|
6
|
+
@one = BigDecimal("1")
|
7
|
+
@zero = BigDecimal("0")
|
8
|
+
@zero_pos = BigDecimal("+0")
|
9
|
+
@zero_neg = BigDecimal("-0")
|
10
|
+
@two = BigDecimal("2.0")
|
11
|
+
@three = BigDecimal("3.0")
|
12
|
+
@nan = BigDecimal("NaN")
|
13
|
+
@infinity = BigDecimal("Infinity")
|
14
|
+
@infinity_minus = BigDecimal("-Infinity")
|
15
|
+
@one_minus = BigDecimal("-1")
|
16
|
+
@frac_1 = BigDecimal("1E-99999")
|
17
|
+
@frac_2 = BigDecimal("0.9E-99999")
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns square root of 2 with desired precision" do
|
21
|
+
string = "1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157"
|
22
|
+
(1..99).each { |idx|
|
23
|
+
@two.sqrt(idx).should be_close(BigDecimal(string), BigDecimal("1E-#{idx-1}"))
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns square root of 3 with desired precision" do
|
28
|
+
sqrt_3 = "1.732050807568877293527446341505872366942805253810380628055806979451933016908800037081146186757248575"
|
29
|
+
(1..99).each { |idx|
|
30
|
+
@three.sqrt(idx).should be_close(BigDecimal(sqrt_3), BigDecimal("1E-#{idx-1}"))
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns square root of 121 with desired precision" do
|
35
|
+
BigDecimal('121').sqrt(5).should be_close(11, 0.00001)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns square root of 0.9E-99999 with desired precision" do
|
39
|
+
@frac_2.sqrt(1).to_s.should == "0.3E-49999"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "raises ArgumentError when no argument is given" do
|
43
|
+
lambda {
|
44
|
+
@one.sqrt
|
45
|
+
}.should raise_error(ArgumentError)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "raises ArgumentError if a negative number is given" do
|
49
|
+
lambda {
|
50
|
+
@one.sqrt(-1)
|
51
|
+
}.should raise_error(ArgumentError)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "raises ArgumentError if 2 arguments are given" do
|
55
|
+
lambda {
|
56
|
+
@one.sqrt(1, 1)
|
57
|
+
}.should raise_error(ArgumentError)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "raises TypeError if nil is given" do
|
61
|
+
lambda {
|
62
|
+
@one.sqrt(nil)
|
63
|
+
}.should raise_error(TypeError)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "raises TypeError if a string is given" do
|
67
|
+
lambda {
|
68
|
+
@one.sqrt("stuff")
|
69
|
+
}.should raise_error(TypeError)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "raises TypeError if a plain Object is given" do
|
73
|
+
lambda {
|
74
|
+
@one.sqrt(Object.new)
|
75
|
+
}.should raise_error(TypeError)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "returns 1 if precision is 0 or 1" do
|
79
|
+
@one.sqrt(1).should == 1
|
80
|
+
@one.sqrt(0).should == 1
|
81
|
+
end
|
82
|
+
|
83
|
+
it "raises FloatDomainError on negative values" do
|
84
|
+
lambda {
|
85
|
+
BigDecimal('-1').sqrt(10)
|
86
|
+
}.should raise_error(FloatDomainError)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "returns positive infitinity for infinity" do
|
90
|
+
@infinity.sqrt(1).should == @infinity
|
91
|
+
end
|
92
|
+
|
93
|
+
it "raises FloatDomainError for negative infinity" do
|
94
|
+
lambda {
|
95
|
+
@infinity_minus.sqrt(1)
|
96
|
+
}.should raise_error(FloatDomainError)
|
97
|
+
end
|
98
|
+
|
99
|
+
it "raises FloatDomainError for NaN" do
|
100
|
+
lambda {
|
101
|
+
@nan.sqrt(1)
|
102
|
+
}.should raise_error(FloatDomainError)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "returns 0 for 0, +0.0 and -0.0" do
|
106
|
+
@zero.sqrt(1).should == 0
|
107
|
+
@zero_pos.sqrt(1).should == 0
|
108
|
+
@zero_neg.sqrt(1).should == 0
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
data/spec/sub_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#sub" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@one = BigDecimal("1")
|
7
|
+
@zero = BigDecimal("0")
|
8
|
+
@two = BigDecimal("2")
|
9
|
+
@nan = BigDecimal("NaN")
|
10
|
+
@infinity = BigDecimal("Infinity")
|
11
|
+
@infinity_minus = BigDecimal("-Infinity")
|
12
|
+
@one_minus = BigDecimal("-1")
|
13
|
+
@frac_1 = BigDecimal("1E-99999")
|
14
|
+
@frac_2 = BigDecimal("0.9E-99999")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns a - b with given precision" do
|
18
|
+
# documentation states, that precision is optional
|
19
|
+
# but implementation raises ArgumentError if not given.
|
20
|
+
|
21
|
+
@two.sub(@one, 1).should == @one
|
22
|
+
@one.sub(@two, 1).should == @one_minus
|
23
|
+
@one.sub(@one_minus, 1).should == @two
|
24
|
+
@frac_2.sub(@frac_1, 1000000).should == BigDecimal("-0.1E-99999")
|
25
|
+
@frac_2.sub(@frac_1, 1).should == BigDecimal("-0.1E-99999")
|
26
|
+
# the above two examples puzzle me.
|
27
|
+
in_arow_one = BigDecimal("1.23456789")
|
28
|
+
in_arow_two = BigDecimal("1.2345678")
|
29
|
+
in_arow_one.sub(in_arow_two, 10).should == BigDecimal("0.9E-7")
|
30
|
+
@two.sub(@two,1).should == @zero
|
31
|
+
@frac_1.sub(@frac_1, 1000000).should == @zero
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns NaN if NaN is involved" do
|
35
|
+
@one.sub(@nan, 1).nan?.should == true
|
36
|
+
@nan.sub(@one, 1).nan?.should == true
|
37
|
+
end
|
38
|
+
|
39
|
+
it "returns NaN if both values are infinite with the same signs" do
|
40
|
+
@infinity.sub(@infinity, 1).nan?.should == true
|
41
|
+
@infinity_minus.sub(@infinity_minus, 1).nan?.should == true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns Infinity or -Infinity if these are involved" do
|
45
|
+
@infinity.sub(@infinity_minus, 1).should == @infinity
|
46
|
+
@infinity_minus.sub(@infinity, 1).should == @infinity_minus
|
47
|
+
@zero.sub(@infinity, 1).should == @infinity_minus
|
48
|
+
@frac_2.sub( @infinity, 1).should == @infinity_minus
|
49
|
+
@two.sub(@infinity, 1).should == @infinity_minus
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/spec/to_f_spec.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#to_f" do
|
4
|
+
before(:each) do
|
5
|
+
@one = BigDecimal("1")
|
6
|
+
@zero = BigDecimal("0")
|
7
|
+
@zero_pos = BigDecimal("+0")
|
8
|
+
@zero_neg = BigDecimal("-0")
|
9
|
+
@two = BigDecimal("2")
|
10
|
+
@three = BigDecimal("3")
|
11
|
+
@nan = BigDecimal("NaN")
|
12
|
+
@infinity = BigDecimal("Infinity")
|
13
|
+
@infinity_minus = BigDecimal("-Infinity")
|
14
|
+
@one_minus = BigDecimal("-1")
|
15
|
+
@frac_1 = BigDecimal("1E-99999")
|
16
|
+
@frac_2 = BigDecimal("0.9E-99999")
|
17
|
+
@vals = [@one, @zero, @two, @three, @frac_1, @frac_2]
|
18
|
+
@spec_vals = [@zero_pos, @zero_neg, @nan, @infinity, @infinity_minus]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns number of type float" do
|
22
|
+
BigDecimal("3.14159").to_f.should be_kind_of(Float)
|
23
|
+
@vals.each { |val| val.to_f.should be_kind_of(Float) }
|
24
|
+
@spec_vals.each { |val| val.to_f.should be_kind_of(Float) }
|
25
|
+
end
|
26
|
+
|
27
|
+
it "Floating point rounding occurs" do
|
28
|
+
bigdec = BigDecimal("3.141592653589793238462643383279502884197169399375")
|
29
|
+
bigdec.to_f.should be_close(3.14159265358979, TOLERANCE)
|
30
|
+
@one.to_f.should == 1.0
|
31
|
+
@two.to_f.should == 2.0
|
32
|
+
@three.to_f.should be_close(3.0, TOLERANCE)
|
33
|
+
@one_minus.to_f.should == -1.0
|
34
|
+
|
35
|
+
# regression test for [ruby-talk:338957]
|
36
|
+
BigDecimal("10.03").to_f.should == 10.03
|
37
|
+
end
|
38
|
+
|
39
|
+
it "properly handles special values" do
|
40
|
+
@zero.to_f.should == 0
|
41
|
+
@zero.to_f.to_s.should == "0.0"
|
42
|
+
|
43
|
+
@nan.to_f.nan?.should == true
|
44
|
+
|
45
|
+
@infinity.to_f.infinite?.should == 1
|
46
|
+
@infinity_minus.to_f.infinite?.should == -1
|
47
|
+
end
|
48
|
+
|
49
|
+
it "remembers negative zero when converted to float" do
|
50
|
+
@zero_neg.to_f.should == 0
|
51
|
+
@zero_neg.to_f.to_s.should == "-0.0"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
data/spec/to_i_spec.rb
ADDED
data/spec/to_int_spec.rb
ADDED
data/spec/to_s_spec.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#to_s" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@bigdec_str = "3.14159265358979323846264338327950288419716939937"
|
7
|
+
@bigneg_str = "-3.1415926535897932384626433832795028841971693993"
|
8
|
+
@bigdec = BigDecimal(@bigdec_str)
|
9
|
+
@bigneg = BigDecimal(@bigneg_str)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "return type is of class String" do
|
13
|
+
@bigdec.to_s.kind_of?(String).should == true
|
14
|
+
@bigneg.to_s.kind_of?(String).should == true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "the default format looks like 0.xxxxEnn" do
|
18
|
+
@bigdec.to_s.should =~ /^0\.[0-9]*E[0-9]*$/
|
19
|
+
end
|
20
|
+
|
21
|
+
it "takes an optional argument" do
|
22
|
+
lambda {@bigdec.to_s("F")}.should_not raise_error()
|
23
|
+
end
|
24
|
+
|
25
|
+
it "starts with + if + is supplied and value is positive" do
|
26
|
+
@bigdec.to_s("+").should =~ /^\+.*/
|
27
|
+
@bigneg.to_s("+").should_not =~ /^\+.*/
|
28
|
+
end
|
29
|
+
|
30
|
+
it "inserts a space every n chars, if integer n is supplied" do
|
31
|
+
str =\
|
32
|
+
"0.314 159 265 358 979 323 846 264 338 327 950 288 419 716 939 937E1"
|
33
|
+
@bigdec.to_s(3).should == str
|
34
|
+
str1 = '-123.45678 90123 45678 9'
|
35
|
+
BigDecimal.new("-123.45678901234567890").to_s('5F').should == str1
|
36
|
+
# trailing zeroes removed
|
37
|
+
BigDecimal.new("1.00000000000").to_s('1F').should == "1.0"
|
38
|
+
# 0 is treated as no spaces
|
39
|
+
BigDecimal.new("1.2345").to_s('0F').should == "1.2345"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "can return a leading space for values > 0" do
|
43
|
+
@bigdec.to_s(" F").should =~ /\ .*/
|
44
|
+
@bigneg.to_s(" F").should_not =~ /\ .*/
|
45
|
+
end
|
46
|
+
|
47
|
+
it "removes trailing spaces in floating point notation" do
|
48
|
+
BigDecimal.new('-123.45678901234567890').to_s('F').should == "-123.4567890123456789"
|
49
|
+
BigDecimal.new('1.2500').to_s('F').should == "1.25"
|
50
|
+
BigDecimal.new('0000.00000').to_s('F').should == "0.0"
|
51
|
+
BigDecimal.new('-00.000010000').to_s('F').should == "-0.00001"
|
52
|
+
BigDecimal.new("5.00000E-2").to_s("F").should == "0.05"
|
53
|
+
|
54
|
+
BigDecimal.new("500000").to_s("F").should == "500000.0"
|
55
|
+
BigDecimal.new("5E2").to_s("F").should == "500.0"
|
56
|
+
BigDecimal.new("-5E100").to_s("F").should == "-5" + "0" * 100 + ".0"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "can use engineering notation" do
|
60
|
+
@bigdec.to_s("E").should =~ /^0\.[0-9]*E[0-9]*$/
|
61
|
+
end
|
62
|
+
|
63
|
+
it "can use conventional floating point notation" do
|
64
|
+
@bigdec.to_s("F").should == @bigdec_str
|
65
|
+
@bigneg.to_s("F").should == @bigneg_str
|
66
|
+
str2 = "+123.45678901 23456789"
|
67
|
+
BigDecimal.new('123.45678901234567890').to_s('+8F').should == str2
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|