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,36 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
ruby_version_is "" ... "1.9" do
|
4
|
+
describe "BigDecimal.induced_from" do
|
5
|
+
it "returns the passed argument when passed a BigDecimal" do
|
6
|
+
BigDecimal.induced_from(BigDecimal("5")).should == BigDecimal("5")
|
7
|
+
BigDecimal.induced_from(BigDecimal("-5")).should == BigDecimal("-5")
|
8
|
+
BigDecimal.induced_from(BigDecimal("Infinity")).should == BigDecimal("Infinity")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "converts passed Fixnums to BigDecimal" do
|
12
|
+
BigDecimal.induced_from(5).should == BigDecimal("5")
|
13
|
+
BigDecimal.induced_from(-5).should == BigDecimal("-5")
|
14
|
+
BigDecimal.induced_from(0).should == BigDecimal("0")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "converts passed Bignums to BigDecimal" do
|
18
|
+
BigDecimal.induced_from(bignum_value).should == BigDecimal(bignum_value.to_s)
|
19
|
+
BigDecimal.induced_from(-bignum_value).should == BigDecimal((-bignum_value).to_s)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "does not try to convert non-Integers to Integer using #to_i" do
|
23
|
+
obj = mock("Not converted to Integer")
|
24
|
+
obj.should_not_receive(:to_i)
|
25
|
+
lambda { BigDecimal.induced_from(obj) }.should raise_error(TypeError)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "raises a TypeError when passed a non-Integer" do
|
29
|
+
lambda { BigDecimal.induced_from(2.0) }.should raise_error(TypeError)
|
30
|
+
lambda { BigDecimal.induced_from("2") }.should raise_error(TypeError)
|
31
|
+
lambda { BigDecimal.induced_from(:symbol) }.should raise_error(TypeError)
|
32
|
+
lambda { BigDecimal.induced_from(nil) }.should raise_error(TypeError)
|
33
|
+
lambda { BigDecimal.induced_from(Object.new) }.should raise_error(TypeError)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#infinite?" do
|
4
|
+
|
5
|
+
it "returns 1 if self is Infinity" do
|
6
|
+
BigDecimal("Infinity").infinite?.should == 1
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns -1 if self is -Infinity" do
|
10
|
+
BigDecimal("-Infinity").infinite?.should == -1
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns not true otherwise" do
|
14
|
+
e2_plus = BigDecimal("2E40001")
|
15
|
+
e3_minus = BigDecimal("3E-20001")
|
16
|
+
really_small_zero = BigDecimal("0E-200000000")
|
17
|
+
really_big_zero = BigDecimal("0E200000000000")
|
18
|
+
e3_minus.infinite?.should == nil
|
19
|
+
e2_plus.infinite?.should == nil
|
20
|
+
really_small_zero.infinite?.should == nil
|
21
|
+
really_big_zero.infinite?.should == nil
|
22
|
+
BigDecimal("0.000000000000000000000000").infinite?.should == nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns not true if self is NaN" do
|
26
|
+
# NaN is a special value which is neither finite nor infinite.
|
27
|
+
nan = BigDecimal("NaN")
|
28
|
+
nan.infinite?.should == nil
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#inspect" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@bigdec = BigDecimal.new("1234.5678")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns String" do
|
10
|
+
@bigdec.inspect.kind_of?(String).should == true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns String starting with #" do
|
14
|
+
@bigdec.inspect[0].should == ?#
|
15
|
+
end
|
16
|
+
|
17
|
+
it "encloses information in angle brackets" do
|
18
|
+
@bigdec.inspect.should =~ /^.<.*>$/
|
19
|
+
end
|
20
|
+
|
21
|
+
it "is comma separated list of three items" do
|
22
|
+
@bigdec.inspect.should =~ /...*,.*,.*/
|
23
|
+
end
|
24
|
+
|
25
|
+
it "value after first comma is value as string" do
|
26
|
+
@bigdec.inspect.split(",")[1].should == "\'0.12345678E4\'"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "last part is number of significant digits" do
|
30
|
+
signific_string = "#{@bigdec.precs[0]}(#{@bigdec.precs[1]})"
|
31
|
+
@bigdec.inspect.split(",")[2].should == signific_string + ">"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "looks like this" do
|
35
|
+
regex = /^\#\<BigDecimal\:.*,'0\.12345678E4',[0-9]+\([0-9]+\)>$/
|
36
|
+
@bigdec.inspect.should =~ regex
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
data/spec/limit_spec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
2
|
+
require 'bigdecimal'
|
3
|
+
|
4
|
+
describe "BigDecimal.limit" do
|
5
|
+
it "returns the value before set if the passed argument is nil or is not specified" do
|
6
|
+
old = BigDecimal.limit
|
7
|
+
BigDecimal.limit.should == 0
|
8
|
+
BigDecimal.limit(10).should == 0
|
9
|
+
BigDecimal.limit.should == 10
|
10
|
+
BigDecimal.limit(old)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "use the global limit if no precision is specified" do
|
14
|
+
BigDecimalSpecs::with_limit(0) do
|
15
|
+
(BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.888')
|
16
|
+
(BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('2.664')
|
17
|
+
end
|
18
|
+
|
19
|
+
BigDecimalSpecs::with_limit(1) do
|
20
|
+
(BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.9')
|
21
|
+
(BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('3')
|
22
|
+
end
|
23
|
+
|
24
|
+
BigDecimalSpecs::with_limit(2) do
|
25
|
+
(BigDecimal('0.888') + BigDecimal('0')).should == BigDecimal('0.89')
|
26
|
+
(BigDecimal('0.888') * BigDecimal('3')).should == BigDecimal('2.7')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/lt_spec.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#<" do
|
4
|
+
before(:each) do
|
5
|
+
@zero = BigDecimal("0")
|
6
|
+
@zero_pos = BigDecimal("+0")
|
7
|
+
@zero_neg = BigDecimal("-0")
|
8
|
+
@mixed = BigDecimal("1.23456789")
|
9
|
+
@pos_int = BigDecimal("2E5555")
|
10
|
+
@neg_int = BigDecimal("-2E5555")
|
11
|
+
@pos_frac = BigDecimal("2E-9999")
|
12
|
+
@neg_frac = BigDecimal("-2E-9999")
|
13
|
+
|
14
|
+
@int_mock = mock('123')
|
15
|
+
class << @int_mock
|
16
|
+
def coerce(other)
|
17
|
+
return [other, BigDecimal('123')]
|
18
|
+
end
|
19
|
+
def < (other)
|
20
|
+
BigDecimal('123') < other
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
@values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
|
25
|
+
-2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
|
26
|
+
@zero , 1, 2, 10, 10.5, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
|
27
|
+
|
28
|
+
@infinity = BigDecimal("Infinity")
|
29
|
+
@infinity_neg = BigDecimal("-Infinity")
|
30
|
+
@nan = BigDecimal("NaN")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns true if a < b" do
|
34
|
+
one = BigDecimal("1")
|
35
|
+
two = BigDecimal("2")
|
36
|
+
frac_1 = BigDecimal("1E-99999")
|
37
|
+
frac_2 = BigDecimal("0.9E-99999")
|
38
|
+
(@zero < one).should == true
|
39
|
+
(two < @zero).should == false
|
40
|
+
(frac_2 < frac_1).should == true
|
41
|
+
(@neg_int < @pos_int).should == true
|
42
|
+
(@pos_int < @neg_int).should == false
|
43
|
+
(@neg_int < @pos_frac).should == true
|
44
|
+
(@pos_frac < @neg_int).should == false
|
45
|
+
(@zero < @zero_pos).should == false
|
46
|
+
(@zero < @zero_neg).should == false
|
47
|
+
(@zero_neg < @zero_pos).should == false
|
48
|
+
(@zero_pos < @zero_neg).should == false
|
49
|
+
end
|
50
|
+
|
51
|
+
it "properly handles infinity values" do
|
52
|
+
@values.each { |val|
|
53
|
+
(val < @infinity).should == true
|
54
|
+
(@infinity < val).should == false
|
55
|
+
(val < @infinity_neg).should == false
|
56
|
+
(@infinity_neg < val).should == true
|
57
|
+
}
|
58
|
+
(@infinity < @infinity).should == false
|
59
|
+
(@infinity_neg < @infinity_neg).should == false
|
60
|
+
(@infinity < @infinity_neg).should == false
|
61
|
+
(@infinity_neg < @infinity).should == true
|
62
|
+
end
|
63
|
+
|
64
|
+
ruby_bug "redmine:2349", "1.8.7" do
|
65
|
+
it "properly handles NaN values" do
|
66
|
+
@values += [@infinity, @infinity_neg, @nan]
|
67
|
+
@values.each { |val|
|
68
|
+
(@nan < val).should == false
|
69
|
+
(val < @nan).should == false
|
70
|
+
}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
ruby_bug "redmine:2349", "1.8.7" do
|
75
|
+
it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do
|
76
|
+
lambda {@zero < nil }.should raise_error(ArgumentError)
|
77
|
+
lambda {@infinity < nil }.should raise_error(ArgumentError)
|
78
|
+
lambda {@infinity_neg < nil }.should raise_error(ArgumentError)
|
79
|
+
lambda {@mixed < nil }.should raise_error(ArgumentError)
|
80
|
+
lambda {@pos_int < nil }.should raise_error(ArgumentError)
|
81
|
+
lambda {@neg_frac < nil }.should raise_error(ArgumentError)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/spec/lte_spec.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#<=" do
|
4
|
+
before(:each) do
|
5
|
+
@zero = BigDecimal("0")
|
6
|
+
@zero_pos = BigDecimal("+0")
|
7
|
+
@zero_neg = BigDecimal("-0")
|
8
|
+
@mixed = BigDecimal("1.23456789")
|
9
|
+
@pos_int = BigDecimal("2E5555")
|
10
|
+
@neg_int = BigDecimal("-2E5555")
|
11
|
+
@pos_frac = BigDecimal("2E-9999")
|
12
|
+
@neg_frac = BigDecimal("-2E-9999")
|
13
|
+
|
14
|
+
@int_mock = mock('123')
|
15
|
+
class << @int_mock
|
16
|
+
def coerce(other)
|
17
|
+
return [other, BigDecimal('123')]
|
18
|
+
end
|
19
|
+
def <= (other)
|
20
|
+
BigDecimal('123') <= other
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
@values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
|
25
|
+
-2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
|
26
|
+
@zero , 1, 2, 10, 10.5, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]
|
27
|
+
|
28
|
+
@infinity = BigDecimal("Infinity")
|
29
|
+
@infinity_neg = BigDecimal("-Infinity")
|
30
|
+
@nan = BigDecimal("NaN")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns true if a <= b" do
|
34
|
+
one = BigDecimal("1")
|
35
|
+
two = BigDecimal("2")
|
36
|
+
|
37
|
+
frac_1 = BigDecimal("1E-99999")
|
38
|
+
frac_2 = BigDecimal("0.9E-99999")
|
39
|
+
|
40
|
+
(@zero <= one).should == true
|
41
|
+
(two <= @zero).should == false
|
42
|
+
|
43
|
+
(frac_2 <= frac_1).should == true
|
44
|
+
(two <= two).should == true
|
45
|
+
(frac_1 <= frac_1).should == true
|
46
|
+
|
47
|
+
(@neg_int <= @pos_int).should == true
|
48
|
+
(@pos_int <= @neg_int).should == false
|
49
|
+
(@neg_int <= @pos_frac).should == true
|
50
|
+
(@pos_frac <= @neg_int).should == false
|
51
|
+
(@zero <= @zero_pos).should == true
|
52
|
+
(@zero <= @zero_neg).should == true
|
53
|
+
(@zero_neg <= @zero_pos).should == true
|
54
|
+
(@zero_pos <= @zero_neg).should == true
|
55
|
+
end
|
56
|
+
|
57
|
+
it "properly handles infinity values" do
|
58
|
+
@values.each { |val|
|
59
|
+
(val <= @infinity).should == true
|
60
|
+
(@infinity <= val).should == false
|
61
|
+
(val <= @infinity_neg).should == false
|
62
|
+
(@infinity_neg <= val).should == true
|
63
|
+
}
|
64
|
+
(@infinity <= @infinity).should == true
|
65
|
+
(@infinity_neg <= @infinity_neg).should == true
|
66
|
+
(@infinity <= @infinity_neg).should == false
|
67
|
+
(@infinity_neg <= @infinity).should == true
|
68
|
+
end
|
69
|
+
|
70
|
+
ruby_bug "redmine:2349", "1.8.7" do
|
71
|
+
it "properly handles NaN values" do
|
72
|
+
@values += [@infinity, @infinity_neg, @nan]
|
73
|
+
@values.each { |val|
|
74
|
+
(@nan <= val).should == false
|
75
|
+
(val <= @nan).should == false
|
76
|
+
}
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
ruby_bug "redmine:2349", "1.8.7" do
|
81
|
+
it "raises an ArgumentError if the argument can't be coerced into a BigDecimal" do
|
82
|
+
lambda {@zero <= nil }.should raise_error(ArgumentError)
|
83
|
+
lambda {@infinity <= nil }.should raise_error(ArgumentError)
|
84
|
+
lambda {@infinity_neg <= nil }.should raise_error(ArgumentError)
|
85
|
+
lambda {@mixed <= nil }.should raise_error(ArgumentError)
|
86
|
+
lambda {@pos_int <= nil }.should raise_error(ArgumentError)
|
87
|
+
lambda {@neg_frac <= nil }.should raise_error(ArgumentError)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/spec/minus_spec.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#-" 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" do
|
18
|
+
(@two - @one).should == @one
|
19
|
+
(@one - @two).should == @one_minus
|
20
|
+
(@one - @one_minus).should == @two
|
21
|
+
(@frac_2 - @frac_1).should == BigDecimal("-0.1E-99999")
|
22
|
+
(@two - @two).should == @zero
|
23
|
+
(@frac_1 - @frac_1).should == @zero
|
24
|
+
(BigDecimal('1.23456789') - BigDecimal('1.2')).should == BigDecimal("0.03456789")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns NaN if NaN is involved" do
|
28
|
+
(@one - @nan).nan?.should == true
|
29
|
+
(@nan - @one).nan?.should == true
|
30
|
+
(@nan - @nan).nan?.should == true
|
31
|
+
(@nan - @infinity).nan?.should == true
|
32
|
+
(@nan - @infinity_minus).nan?.should == true
|
33
|
+
(@infinity - @nan).nan?.should == true
|
34
|
+
(@infinity_minus - @nan).nan?.should == true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns NaN both operands are infinite with the same sign" do
|
38
|
+
(@infinity - @infinity).nan?.should == true
|
39
|
+
(@infinity_minus - @infinity_minus).nan?.should == true
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns Infinity or -Infinity if these are involved" do
|
43
|
+
(@infinity - @infinity_minus).should == @infinity
|
44
|
+
(@infinity_minus - @infinity).should == @infinity_minus
|
45
|
+
|
46
|
+
(@infinity - @zero).should == @infinity
|
47
|
+
(@infinity - @frac_2).should == @infinity
|
48
|
+
(@infinity - @two).should == @infinity
|
49
|
+
(@infinity - @one_minus).should == @infinity
|
50
|
+
|
51
|
+
(@zero - @infinity).should == @infinity_minus
|
52
|
+
(@frac_2 - @infinity).should == @infinity_minus
|
53
|
+
(@two - @infinity).should == @infinity_minus
|
54
|
+
(@one_minus - @infinity).should == @infinity_minus
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/spec/mode_spec.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal.mode" do
|
4
|
+
#the default value of BigDecimal exception constants is false
|
5
|
+
after :all do
|
6
|
+
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
|
7
|
+
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
|
8
|
+
BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, false)
|
9
|
+
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
|
10
|
+
BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, false)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns the appropriate value and continue the computation if the flag is false" do
|
14
|
+
BigDecimal("NaN").add(BigDecimal("1"),0).nan?.should == true
|
15
|
+
BigDecimal("0").add(BigDecimal("Infinity"),0).should == BigDecimal("Infinity")
|
16
|
+
BigDecimal("1").quo(BigDecimal("0")).should == BigDecimal("Infinity")
|
17
|
+
end
|
18
|
+
|
19
|
+
ruby_version_is "" ... "1.9" do
|
20
|
+
it "returns zero when too big" do
|
21
|
+
BigDecimal("1E11111111111111111111").zero?.should == true
|
22
|
+
(BigDecimal("1E11111111111")*BigDecimal("1E11111111111")).zero?.should == true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
ruby_version_is "1.9" ... "1.9.3" do
|
27
|
+
it "returns Infinity when too big" do
|
28
|
+
BigDecimal("1E11111111111111111111").should == BigDecimal("Infinity")
|
29
|
+
(BigDecimal("1E11111111111")*BigDecimal("1E11111111111")).should ==
|
30
|
+
BigDecimal("Infinity")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "raise an exception if the flag is true" do
|
34
|
+
BigDecimal::mode(BigDecimal::EXCEPTION_NaN, true)
|
35
|
+
lambda { BigDecimal("NaN").add(BigDecimal("1"),0) }.should raise_error(FloatDomainError)
|
36
|
+
BigDecimal::mode(BigDecimal::EXCEPTION_INFINITY, true)
|
37
|
+
lambda { BigDecimal("0").add(BigDecimal("Infinity"),0) }.should raise_error(FloatDomainError)
|
38
|
+
BigDecimal::mode(BigDecimal::EXCEPTION_ZERODIVIDE, true)
|
39
|
+
lambda { BigDecimal("1").quo(BigDecimal("0")) }.should raise_error(FloatDomainError)
|
40
|
+
BigDecimal::mode(BigDecimal::EXCEPTION_OVERFLOW, true)
|
41
|
+
lambda { BigDecimal("1E11111111111111111111") }.should raise_error(FloatDomainError)
|
42
|
+
lambda { (BigDecimal("1E11111111111")*BigDecimal("1E11111111111")) }.should raise_error(FloatDomainError)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
ruby_version_is "1.9.3" do
|
47
|
+
it "returns Infinity when too big" do
|
48
|
+
BigDecimal("1E11111111111111111111").should == BigDecimal("Infinity")
|
49
|
+
(BigDecimal("1E1000000000000000000")**10).should == BigDecimal("Infinity")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "raise an exception if the flag is true" do
|
53
|
+
BigDecimal::mode(BigDecimal::EXCEPTION_NaN, true)
|
54
|
+
lambda { BigDecimal("NaN").add(BigDecimal("1"),0) }.should raise_error(FloatDomainError)
|
55
|
+
BigDecimal::mode(BigDecimal::EXCEPTION_INFINITY, true)
|
56
|
+
lambda { BigDecimal("0").add(BigDecimal("Infinity"),0) }.should raise_error(FloatDomainError)
|
57
|
+
BigDecimal::mode(BigDecimal::EXCEPTION_ZERODIVIDE, true)
|
58
|
+
lambda { BigDecimal("1").quo(BigDecimal("0")) }.should raise_error(FloatDomainError)
|
59
|
+
BigDecimal::mode(BigDecimal::EXCEPTION_OVERFLOW, true)
|
60
|
+
lambda { BigDecimal("1E11111111111111111111") }.should raise_error(FloatDomainError)
|
61
|
+
lambda { (BigDecimal("1E1000000000000000000")**10) }.should raise_error(FloatDomainError)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/spec/modulo_spec.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.expand_path('../shared/modulo', __FILE__)
|
2
|
+
|
3
|
+
describe "BigDecimal#%" do
|
4
|
+
it_behaves_like(:bigdecimal_modulo, :%)
|
5
|
+
it_behaves_like(:bigdecimal_modulo_zerodivisionerror, :%)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "BigDecimal#modulo" do
|
9
|
+
it_behaves_like(:bigdecimal_modulo, :modulo)
|
10
|
+
it_behaves_like(:bigdecimal_modulo_zerodivisionerror, :modulo)
|
11
|
+
end
|