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,71 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
|
3
|
+
#
|
4
|
+
# linear.rb
|
5
|
+
#
|
6
|
+
# Solves linear equation system(A*x = b) by LU decomposition method.
|
7
|
+
# where A is a coefficient matrix,x is an answer vector,b is a constant vector.
|
8
|
+
#
|
9
|
+
# USAGE:
|
10
|
+
# ruby linear.rb [input file solved]
|
11
|
+
#
|
12
|
+
|
13
|
+
require "bigdecimal"
|
14
|
+
require "bigdecimal/ludcmp"
|
15
|
+
|
16
|
+
#
|
17
|
+
# NOTE:
|
18
|
+
# Change following BigDecimal::limit() if needed.
|
19
|
+
BigDecimal::limit(100)
|
20
|
+
#
|
21
|
+
|
22
|
+
include LUSolve
|
23
|
+
def rd_order(na)
|
24
|
+
printf("Number of equations ?") if(na <= 0)
|
25
|
+
n = ARGF.gets().to_i
|
26
|
+
end
|
27
|
+
|
28
|
+
na = ARGV.size
|
29
|
+
zero = BigDecimal::new("0.0")
|
30
|
+
one = BigDecimal::new("1.0")
|
31
|
+
|
32
|
+
while (n=rd_order(na))>0
|
33
|
+
a = []
|
34
|
+
as= []
|
35
|
+
b = []
|
36
|
+
if na <= 0
|
37
|
+
# Read data from console.
|
38
|
+
printf("\nEnter coefficient matrix element A[i,j]\n");
|
39
|
+
for i in 0...n do
|
40
|
+
for j in 0...n do
|
41
|
+
printf("A[%d,%d]? ",i,j); s = ARGF.gets
|
42
|
+
a << BigDecimal::new(s);
|
43
|
+
as << BigDecimal::new(s);
|
44
|
+
end
|
45
|
+
printf("Contatant vector element b[%d] ? ",i); b << BigDecimal::new(ARGF.gets);
|
46
|
+
end
|
47
|
+
else
|
48
|
+
# Read data from specified file.
|
49
|
+
printf("Coefficient matrix and constant vector.\n");
|
50
|
+
for i in 0...n do
|
51
|
+
s = ARGF.gets
|
52
|
+
printf("%d) %s",i,s)
|
53
|
+
s = s.split
|
54
|
+
for j in 0...n do
|
55
|
+
a << BigDecimal::new(s[j]);
|
56
|
+
as << BigDecimal::new(s[j]);
|
57
|
+
end
|
58
|
+
b << BigDecimal::new(s[n]);
|
59
|
+
end
|
60
|
+
end
|
61
|
+
x = lusolve(a,b,ludecomp(a,n,zero,one),zero)
|
62
|
+
printf("Answer(x[i] & (A*x-b)[i]) follows\n")
|
63
|
+
for i in 0...n do
|
64
|
+
printf("x[%d]=%s ",i,x[i].to_s)
|
65
|
+
s = zero
|
66
|
+
for j in 0...n do
|
67
|
+
s = s + as[i*n+j]*x[j]
|
68
|
+
end
|
69
|
+
printf(" & %s\n",(s-b[i]).to_s)
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
|
3
|
+
#
|
4
|
+
# nlsolve.rb
|
5
|
+
# An example for solving nonlinear algebraic equation system.
|
6
|
+
#
|
7
|
+
|
8
|
+
require "bigdecimal"
|
9
|
+
require "bigdecimal/newton"
|
10
|
+
include Newton
|
11
|
+
|
12
|
+
class Function
|
13
|
+
def initialize()
|
14
|
+
@zero = BigDecimal::new("0.0")
|
15
|
+
@one = BigDecimal::new("1.0")
|
16
|
+
@two = BigDecimal::new("2.0")
|
17
|
+
@ten = BigDecimal::new("10.0")
|
18
|
+
@eps = BigDecimal::new("1.0e-16")
|
19
|
+
end
|
20
|
+
def zero;@zero;end
|
21
|
+
def one ;@one ;end
|
22
|
+
def two ;@two ;end
|
23
|
+
def ten ;@ten ;end
|
24
|
+
def eps ;@eps ;end
|
25
|
+
def values(x) # <= defines functions solved
|
26
|
+
f = []
|
27
|
+
f1 = x[0]*x[0] + x[1]*x[1] - @two # f1 = x**2 + y**2 - 2 => 0
|
28
|
+
f2 = x[0] - x[1] # f2 = x - y => 0
|
29
|
+
f <<= f1
|
30
|
+
f <<= f2
|
31
|
+
f
|
32
|
+
end
|
33
|
+
end
|
34
|
+
f = BigDecimal::limit(100)
|
35
|
+
f = Function.new
|
36
|
+
x = [f.zero,f.zero] # Initial values
|
37
|
+
n = nlsolve(f,x)
|
38
|
+
p x
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
|
3
|
+
#
|
4
|
+
# pi.rb
|
5
|
+
#
|
6
|
+
# Calculates 3.1415.... (the number of times that a circle's diameter
|
7
|
+
# will fit around the circle) using J. Machin's formula.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "bigdecimal"
|
11
|
+
require "bigdecimal/math.rb"
|
12
|
+
|
13
|
+
include BigMath
|
14
|
+
|
15
|
+
if ARGV.size == 1
|
16
|
+
print "PI("+ARGV[0]+"):\n"
|
17
|
+
p PI(ARGV[0].to_i)
|
18
|
+
else
|
19
|
+
print "TRY: ruby pi.rb 1000 \n"
|
20
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#
|
2
|
+
# BigDecimal utility library.
|
3
|
+
#
|
4
|
+
# To use these functions, require 'bigdecimal/util'
|
5
|
+
#
|
6
|
+
# The following methods are provided to convert other types to BigDecimals:
|
7
|
+
#
|
8
|
+
# String#to_d -> BigDecimal
|
9
|
+
# Float#to_d -> BigDecimal
|
10
|
+
# Rational#to_d -> BigDecimal
|
11
|
+
#
|
12
|
+
# The following method is provided to convert BigDecimals to other types:
|
13
|
+
#
|
14
|
+
# BigDecimal#to_r -> Rational
|
15
|
+
#
|
16
|
+
# ----------------------------------------------------------------------
|
17
|
+
#
|
18
|
+
class Float < Numeric
|
19
|
+
def to_d
|
20
|
+
BigDecimal(self.to_s)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class String
|
25
|
+
def to_d
|
26
|
+
BigDecimal(self)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class BigDecimal < Numeric
|
31
|
+
# Converts a BigDecimal to a String of the form "nnnnnn.mmm".
|
32
|
+
# This method is deprecated; use BigDecimal#to_s("F") instead.
|
33
|
+
def to_digits
|
34
|
+
if self.nan? || self.infinite? || self.zero?
|
35
|
+
self.to_s
|
36
|
+
else
|
37
|
+
i = self.to_i.to_s
|
38
|
+
s,f,y,z = self.frac.split
|
39
|
+
i + "." + ("0"*(-z)) + f
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Converts a BigDecimal to a Rational.
|
44
|
+
def to_r
|
45
|
+
sign,digits,base,power = self.split
|
46
|
+
numerator = sign*digits.to_i
|
47
|
+
denomi_power = power - digits.size # base is always 10
|
48
|
+
if denomi_power < 0
|
49
|
+
Rational(numerator,base ** (-denomi_power))
|
50
|
+
else
|
51
|
+
Rational(numerator * (base ** denomi_power),1)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Rational < Numeric
|
57
|
+
# Converts a Rational to a BigDecimal
|
58
|
+
def to_d(nFig=0)
|
59
|
+
num = self.numerator.to_s
|
60
|
+
if nFig<=0
|
61
|
+
nFig = BigDecimal.double_fig*2+1
|
62
|
+
end
|
63
|
+
BigDecimal.new(num).div(self.denominator,nFig)
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require './lib/rubysl/bigdecimal/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "rubysl-bigdecimal"
|
6
|
+
spec.version = RubySL::BigDecimal::VERSION
|
7
|
+
spec.authors = ["Brian Shirai"]
|
8
|
+
spec.email = ["brixen@gmail.com"]
|
9
|
+
spec.description = %q{Ruby standard library bigdecimal.}
|
10
|
+
spec.summary = %q{Ruby standard library bigdecimal.}
|
11
|
+
spec.homepage = "https://github.com/rubysl/rubysl-bigdecimal"
|
12
|
+
spec.license = "BSD"
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.extensions = ["ext/rubysl/bigdecimal/extconf.rb"]
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "mspec", "~> 1.5"
|
23
|
+
spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
|
24
|
+
end
|
data/spec/abs_spec.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
describe "BigDecimal#abs" 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
|
+
@mixed = BigDecimal("1.23456789")
|
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 the absolute value" do
|
21
|
+
pos_int = BigDecimal("2E5555")
|
22
|
+
neg_int = BigDecimal("-2E5555")
|
23
|
+
pos_frac = BigDecimal("2E-9999")
|
24
|
+
neg_frac = BigDecimal("-2E-9999")
|
25
|
+
|
26
|
+
pos_int.abs.should == pos_int
|
27
|
+
neg_int.abs.should == pos_int
|
28
|
+
pos_frac.abs.should == pos_frac
|
29
|
+
neg_frac.abs.should == pos_frac
|
30
|
+
@one.abs.should == 1
|
31
|
+
@two.abs.should == 2
|
32
|
+
@three.abs.should == 3
|
33
|
+
@mixed.abs.should == @mixed
|
34
|
+
@one_minus.abs.should == @one
|
35
|
+
end
|
36
|
+
|
37
|
+
it "properly handles special values" do
|
38
|
+
@infinity.abs.should == @infinity
|
39
|
+
@infinity_minus.abs.should == @infinity
|
40
|
+
@nan.abs.nan?.should == true # have to do it this way, since == doesn't work on NaN
|
41
|
+
@zero.abs.should == 0
|
42
|
+
@zero.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
43
|
+
@zero_pos.abs.should == 0
|
44
|
+
@zero_pos.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
45
|
+
@zero_neg.abs.should == 0
|
46
|
+
@zero_neg.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/spec/add_spec.rb
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
2
|
+
|
3
|
+
require 'bigdecimal'
|
4
|
+
|
5
|
+
describe "BigDecimal#add" do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@one = BigDecimal("1")
|
9
|
+
@zero = BigDecimal("0")
|
10
|
+
@two = BigDecimal("2")
|
11
|
+
@three = BigDecimal("3")
|
12
|
+
@ten = BigDecimal("10")
|
13
|
+
@eleven = BigDecimal("11")
|
14
|
+
@nan = BigDecimal("NaN")
|
15
|
+
@infinity = BigDecimal("Infinity")
|
16
|
+
@infinity_minus = BigDecimal("-Infinity")
|
17
|
+
@one_minus = BigDecimal("-1")
|
18
|
+
@frac_1 = BigDecimal("1E-99999")
|
19
|
+
@frac_2 = BigDecimal("0.9E-99999")
|
20
|
+
@frac_3 = BigDecimal("12345E10")
|
21
|
+
@frac_4 = BigDecimal("98765E10")
|
22
|
+
@dot_ones = BigDecimal("0.1111111111")
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns a + b with given precision" do
|
26
|
+
# documentation states, that precision ist optional, but it ain't,
|
27
|
+
@two.add(@one, 1).should == @three
|
28
|
+
@one .add(@two, 1).should == @three
|
29
|
+
@one.add(@one_minus, 1).should == @zero
|
30
|
+
@ten.add(@one, 2).should == @eleven
|
31
|
+
@zero.add(@one, 1).should == @one
|
32
|
+
@frac_2.add(@frac_1, 10000).should == BigDecimal("1.9E-99999")
|
33
|
+
@frac_1.add(@frac_1, 10000).should == BigDecimal("2E-99999")
|
34
|
+
@frac_3.add(@frac_4, 0).should == BigDecimal("0.11111E16")
|
35
|
+
@frac_3.add(@frac_4, 1).should == BigDecimal("0.1E16")
|
36
|
+
@frac_3.add(@frac_4, 2).should == BigDecimal("0.11E16")
|
37
|
+
@frac_3.add(@frac_4, 3).should == BigDecimal("0.111E16")
|
38
|
+
@frac_3.add(@frac_4, 4).should == BigDecimal("0.1111E16")
|
39
|
+
@frac_3.add(@frac_4, 5).should == BigDecimal("0.11111E16")
|
40
|
+
@frac_3.add(@frac_4, 6).should == BigDecimal("0.11111E16")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "returns a + [Fixnum value] with given precision" do
|
44
|
+
(1..10).each {|precision|
|
45
|
+
@dot_ones.add(0, precision).should == BigDecimal("0." + "1" * precision)
|
46
|
+
}
|
47
|
+
BigDecimal("0.88").add(0, 1).should == BigDecimal("0.9")
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns a + [Bignum value] with given precision" do
|
51
|
+
bignum = 10000000000000000000
|
52
|
+
(1..20).each {|precision|
|
53
|
+
@dot_ones.add(bignum, precision).should == BigDecimal("0.1E20")
|
54
|
+
}
|
55
|
+
(21..30).each {|precision|
|
56
|
+
@dot_ones.add(bignum, precision).should == BigDecimal(
|
57
|
+
"0.10000000000000000000" + "1" * (precision - 20) + "E20")
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
# TODO:
|
62
|
+
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/17374
|
63
|
+
#
|
64
|
+
# This doesn't work on MRI and looks like a bug to me:
|
65
|
+
# one can use BigDecimal + Float, but not Bigdecimal.add(Float)
|
66
|
+
#
|
67
|
+
# it "returns a + [Float value] with given precision" do
|
68
|
+
# (1..10).each {|precision|
|
69
|
+
# @dot_ones.add(0.0, precision).should == BigDecimal("0." + "1" * precision)
|
70
|
+
# }
|
71
|
+
#
|
72
|
+
# BigDecimal("0.88").add(0.0, 1).should == BigDecimal("0.9")
|
73
|
+
# end
|
74
|
+
|
75
|
+
it "favors the precision specified in the second argument over the global limit" do
|
76
|
+
BigDecimalSpecs::with_limit(1) do
|
77
|
+
BigDecimal('0.888').add(@zero, 3).should == BigDecimal('0.888')
|
78
|
+
end
|
79
|
+
|
80
|
+
BigDecimalSpecs::with_limit(2) do
|
81
|
+
BigDecimal('0.888').add(@zero, 1).should == BigDecimal('0.9')
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it "uses the current rounding mode if rounding is needed" do
|
86
|
+
BigDecimalSpecs::with_rounding(BigDecimal::ROUND_UP) do
|
87
|
+
BigDecimal('0.111').add(@zero, 1).should == BigDecimal('0.2')
|
88
|
+
BigDecimal('-0.111').add(@zero, 1).should == BigDecimal('-0.2')
|
89
|
+
end
|
90
|
+
BigDecimalSpecs::with_rounding(BigDecimal::ROUND_DOWN) do
|
91
|
+
BigDecimal('0.999').add(@zero, 1).should == BigDecimal('0.9')
|
92
|
+
BigDecimal('-0.999').add(@zero, 1).should == BigDecimal('-0.9')
|
93
|
+
end
|
94
|
+
BigDecimalSpecs::with_rounding(BigDecimal::ROUND_HALF_UP) do
|
95
|
+
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.9')
|
96
|
+
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.9')
|
97
|
+
end
|
98
|
+
BigDecimalSpecs::with_rounding(BigDecimal::ROUND_HALF_DOWN) do
|
99
|
+
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.8')
|
100
|
+
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.8')
|
101
|
+
end
|
102
|
+
BigDecimalSpecs::with_rounding(BigDecimal::ROUND_HALF_EVEN) do
|
103
|
+
BigDecimal('0.75').add(@zero, 1).should == BigDecimal('0.8')
|
104
|
+
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.8')
|
105
|
+
BigDecimal('-0.75').add(@zero, 1).should == BigDecimal('-0.8')
|
106
|
+
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.8')
|
107
|
+
end
|
108
|
+
BigDecimalSpecs::with_rounding(BigDecimal::ROUND_CEILING) do
|
109
|
+
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.9')
|
110
|
+
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.8')
|
111
|
+
end
|
112
|
+
BigDecimalSpecs::with_rounding(BigDecimal::ROUND_FLOOR) do
|
113
|
+
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.8')
|
114
|
+
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.9')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
it "uses the default ROUND_HALF_UP rounding if it wasn't explicitly changed" do
|
119
|
+
BigDecimal('0.85').add(@zero, 1).should == BigDecimal('0.9')
|
120
|
+
BigDecimal('-0.85').add(@zero, 1).should == BigDecimal('-0.9')
|
121
|
+
end
|
122
|
+
|
123
|
+
it "returns NaN if NaN is involved" do
|
124
|
+
@one.add(@nan, 10000).nan?.should == true
|
125
|
+
@nan.add(@one, 1).nan?.should == true
|
126
|
+
end
|
127
|
+
|
128
|
+
it "returns Infinity or -Infinity if these are involved" do
|
129
|
+
@zero.add(@infinity, 1).should == @infinity
|
130
|
+
@frac_2.add(@infinity, 1).should == @infinity
|
131
|
+
@one_minus.add(@infinity, 1).should == @infinity
|
132
|
+
@two.add(@infinity, 1).should == @infinity
|
133
|
+
|
134
|
+
@zero.add(@infinity_minus, 1).should == @infinity_minus
|
135
|
+
@frac_2.add(@infinity_minus, 1).should == @infinity_minus
|
136
|
+
@one_minus.add(@infinity_minus, 1).should == @infinity_minus
|
137
|
+
@two.add(@infinity_minus, 1).should == @infinity_minus
|
138
|
+
|
139
|
+
@infinity.add(@zero, 1).should == @infinity
|
140
|
+
@infinity.add(@frac_2, 1).should == @infinity
|
141
|
+
@infinity.add(@one_minus, 1).should == @infinity
|
142
|
+
@infinity.add(@two, 1).should == @infinity
|
143
|
+
|
144
|
+
@infinity_minus.add(@zero, 1).should == @infinity_minus
|
145
|
+
@infinity_minus.add(@frac_2, 1).should == @infinity_minus
|
146
|
+
@infinity_minus.add(@one_minus, 1).should == @infinity_minus
|
147
|
+
@infinity_minus.add(@two, 1).should == @infinity_minus
|
148
|
+
|
149
|
+
@infinity.add(@infinity, 10000).should == @infinity
|
150
|
+
@infinity_minus.add(@infinity_minus, 10000).should == @infinity_minus
|
151
|
+
end
|
152
|
+
|
153
|
+
it "returns NaN if Infinity + (- Infinity)" do
|
154
|
+
@infinity.add(@infinity_minus, 10000).nan?.should == true
|
155
|
+
@infinity_minus.add(@infinity, 10000).nan?.should == true
|
156
|
+
end
|
157
|
+
|
158
|
+
it "raises TypeError when adds nil" do
|
159
|
+
lambda {
|
160
|
+
@one.add(nil, 10)
|
161
|
+
}.should raise_error(TypeError)
|
162
|
+
lambda {
|
163
|
+
@one.add(nil, 0)
|
164
|
+
}.should raise_error(TypeError)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "raises TypeError when precision parameter is nil" do
|
168
|
+
lambda {
|
169
|
+
@one.add(@one, nil)
|
170
|
+
}.should raise_error(TypeError)
|
171
|
+
end
|
172
|
+
|
173
|
+
it "raises ArgumentError when precision parameter is negative" do
|
174
|
+
lambda {
|
175
|
+
@one.add(@one, -10)
|
176
|
+
}.should raise_error(ArgumentError)
|
177
|
+
end
|
178
|
+
end
|