ruby-decimal 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/License.txt +20 -0
- data/Manifest.txt +24 -0
- data/README.txt +306 -0
- data/Rakefile +35 -0
- data/lib/decimal.rb +4 -0
- data/lib/decimal/decimal.rb +2750 -0
- data/lib/decimal/support.rb +337 -0
- data/lib/decimal/version.rb +9 -0
- data/setup.rb +1585 -0
- data/tasks/ann.rake +80 -0
- data/tasks/bones.rake +20 -0
- data/tasks/gem.rake +192 -0
- data/tasks/git.rake +40 -0
- data/tasks/manifest.rake +48 -0
- data/tasks/notes.rake +27 -0
- data/tasks/post_load.rake +39 -0
- data/tasks/rdoc.rake +50 -0
- data/tasks/rubyforge.rake +55 -0
- data/tasks/setup.rb +279 -0
- data/tasks/spec.rake +54 -0
- data/tasks/svn.rake +47 -0
- data/tasks/test.rake +40 -0
- data/test/all_tests.rb +11 -0
- data/test/helper.rb +7 -0
- data/test/test_basic.rb +398 -0
- data/test/test_coercion.rb +22 -0
- data/test/test_comparisons.rb +53 -0
- data/test/test_dectest.rb +207 -0
- data/test/test_define_conversions.rb +100 -0
- data/test/test_exact.rb +32 -0
- data/test/test_flags.rb +34 -0
- data/test/test_multithreading.rb +32 -0
- data/test/test_round.rb +49 -0
- data/test/test_to_int.rb +104 -0
- data/test/test_to_rf.rb +36 -0
- metadata +120 -0
data/tasks/test.rake
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
if test(?e, PROJ.test.file) or not PROJ.test.files.to_a.empty?
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
namespace :test do
|
6
|
+
|
7
|
+
Rake::TestTask.new(:run) do |t|
|
8
|
+
t.libs = PROJ.libs
|
9
|
+
t.test_files = if test(?f, PROJ.test.file) then [PROJ.test.file]
|
10
|
+
else PROJ.test.files end
|
11
|
+
t.ruby_opts += PROJ.ruby_opts
|
12
|
+
t.ruby_opts += PROJ.test.opts
|
13
|
+
end
|
14
|
+
|
15
|
+
if HAVE_RCOV
|
16
|
+
desc 'Run rcov on the unit tests'
|
17
|
+
task :rcov => :clobber_rcov do
|
18
|
+
opts = PROJ.rcov.opts.dup << '-o' << PROJ.rcov.dir
|
19
|
+
opts = opts.join(' ')
|
20
|
+
files = if test(?f, PROJ.test.file) then [PROJ.test.file]
|
21
|
+
else PROJ.test.files end
|
22
|
+
files = files.join(' ')
|
23
|
+
sh "#{RCOV} #{files} #{opts}"
|
24
|
+
end
|
25
|
+
|
26
|
+
task :clobber_rcov do
|
27
|
+
rm_r 'coverage' rescue nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end # namespace :test
|
32
|
+
|
33
|
+
desc 'Alias to test:run'
|
34
|
+
task :test => 'test:run'
|
35
|
+
|
36
|
+
task :clobber => 'test:clobber_rcov' if HAVE_RCOV
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
# EOF
|
data/test/all_tests.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_flags.rb'
|
2
|
+
require File.dirname(__FILE__) + '/test_basic.rb'
|
3
|
+
require File.dirname(__FILE__) + '/test_dectest.rb'
|
4
|
+
require File.dirname(__FILE__) + '/test_exact.rb'
|
5
|
+
require File.dirname(__FILE__) + '/test_round.rb'
|
6
|
+
require File.dirname(__FILE__) + '/test_multithreading.rb'
|
7
|
+
require File.dirname(__FILE__) + '/test_comparisons.rb'
|
8
|
+
require File.dirname(__FILE__) + '/test_coercion.rb'
|
9
|
+
require File.dirname(__FILE__) + '/test_to_int.rb'
|
10
|
+
require File.dirname(__FILE__) + '/test_to_rf.rb'
|
11
|
+
require File.dirname(__FILE__) + '/test_define_conversions.rb'
|
data/test/helper.rb
ADDED
data/test/test_basic.rb
ADDED
@@ -0,0 +1,398 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper.rb'
|
2
|
+
|
3
|
+
def exp(x,c=nil)
|
4
|
+
i, lasts, s, fact, num = 0, 0, 1, 1, 1
|
5
|
+
Decimal.local_context(c) do
|
6
|
+
# result context
|
7
|
+
Decimal.local_context do |context|
|
8
|
+
# working context
|
9
|
+
context.precision += 2
|
10
|
+
context.rounding = Decimal::ROUND_HALF_EVEN
|
11
|
+
while s != lasts
|
12
|
+
lasts = s
|
13
|
+
i += 1
|
14
|
+
fact *= i
|
15
|
+
num *= x
|
16
|
+
s += num / fact
|
17
|
+
end
|
18
|
+
end
|
19
|
+
+s
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def exp1(x, c=nil)
|
24
|
+
return Decimal(BigDecimal("NaN")) if x.infinite? || x.nan?
|
25
|
+
y = nil
|
26
|
+
ext = 2
|
27
|
+
Decimal.local_context(c) do |context|
|
28
|
+
n = (context.precision += ext)
|
29
|
+
|
30
|
+
one = Decimal("1")
|
31
|
+
x1 = one
|
32
|
+
y = one
|
33
|
+
d = y
|
34
|
+
z = one
|
35
|
+
i = 0
|
36
|
+
while d.nonzero? && ((m = n - (y.fractional_exponent - d.fractional_exponent).abs) > 0)
|
37
|
+
m = ext if m < ext
|
38
|
+
x1 *= x
|
39
|
+
i += 1
|
40
|
+
z *= i
|
41
|
+
|
42
|
+
#d = x1.divide(z,:precision=>m)
|
43
|
+
context.precision = m
|
44
|
+
d = x1/z
|
45
|
+
context.precision = n
|
46
|
+
|
47
|
+
y += d
|
48
|
+
end
|
49
|
+
end
|
50
|
+
return +y
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
class TestBasic < Test::Unit::TestCase
|
56
|
+
|
57
|
+
|
58
|
+
def setup
|
59
|
+
initialize_context
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def test_basic
|
64
|
+
|
65
|
+
Decimal.context.precision = 4
|
66
|
+
assert_equal 4, Decimal.context.precision
|
67
|
+
assert_equal Decimal("0.3333"), Decimal(1)/Decimal(3)
|
68
|
+
Decimal.context.precision = 10
|
69
|
+
assert_equal 10, Decimal.context.precision
|
70
|
+
assert_equal Decimal("0.3333333333"), Decimal(1)/Decimal(3)
|
71
|
+
Decimal.local_context {
|
72
|
+
assert_equal 10, Decimal.context.precision
|
73
|
+
Decimal.context.precision = 3
|
74
|
+
assert_equal 3, Decimal.context.precision
|
75
|
+
assert_equal Decimal("0.333"), Decimal(1)/Decimal(3)
|
76
|
+
}
|
77
|
+
assert_equal 10, Decimal.context.precision
|
78
|
+
assert_equal "0.3333333333", (Decimal(1)/Decimal(3)).to_s
|
79
|
+
assert_equal Decimal("0.3333333333"), Decimal(1)/Decimal(3)
|
80
|
+
|
81
|
+
Decimal.local_context(:precision=>4) {
|
82
|
+
assert_equal 4, Decimal.context.precision
|
83
|
+
}
|
84
|
+
assert_equal 10, Decimal.context.precision
|
85
|
+
|
86
|
+
Decimal.context(:precision=>4) {
|
87
|
+
assert_equal 4, Decimal.context.precision
|
88
|
+
}
|
89
|
+
assert_equal 10, Decimal.context.precision
|
90
|
+
|
91
|
+
Decimal.local_context(Decimal::BasicContext) {
|
92
|
+
assert_equal :half_up, Decimal.context.rounding
|
93
|
+
assert_equal 9, Decimal.context.precision
|
94
|
+
}
|
95
|
+
assert_equal :half_even, Decimal.context.rounding
|
96
|
+
assert_equal 10, Decimal.context.precision
|
97
|
+
|
98
|
+
Decimal.context(Decimal::BasicContext) {
|
99
|
+
assert_equal :half_up, Decimal.context.rounding
|
100
|
+
assert_equal 9, Decimal.context.precision
|
101
|
+
}
|
102
|
+
assert_equal :half_even, Decimal.context.rounding
|
103
|
+
assert_equal 10, Decimal.context.precision
|
104
|
+
|
105
|
+
Decimal.local_context(Decimal::BasicContext, :precision=>4) {
|
106
|
+
assert_equal :half_up, Decimal.context.rounding
|
107
|
+
assert_equal 4, Decimal.context.precision
|
108
|
+
}
|
109
|
+
assert_equal :half_even, Decimal.context.rounding
|
110
|
+
assert_equal 10, Decimal.context.precision
|
111
|
+
|
112
|
+
Decimal.context(Decimal::BasicContext, :precision=>4) {
|
113
|
+
assert_equal :half_up, Decimal.context.rounding
|
114
|
+
assert_equal 4, Decimal.context.precision
|
115
|
+
}
|
116
|
+
assert_equal :half_even, Decimal.context.rounding
|
117
|
+
assert_equal 10, Decimal.context.precision
|
118
|
+
|
119
|
+
|
120
|
+
assert_equal Decimal("0."+"3"*100), Decimal(1)./(Decimal(3),Decimal.Context(:precision=>100))
|
121
|
+
assert_equal 10, Decimal.context.precision
|
122
|
+
assert_equal Decimal("0.33"), Decimal(1).divide(Decimal(3),Decimal.Context(:precision=>2))
|
123
|
+
assert_equal Decimal("0.3333333333"), Decimal(1)/Decimal(3)
|
124
|
+
|
125
|
+
assert_equal Decimal("11.0"), Decimal(11).abs
|
126
|
+
assert_equal Decimal("11.0"), Decimal(-11).abs
|
127
|
+
assert_equal Decimal("-11.0"), -Decimal(11)
|
128
|
+
|
129
|
+
assert_equal [-11,0], Decimal("-11").to_int_scale
|
130
|
+
|
131
|
+
assert_equal [-110,-1], Decimal("-11.0").to_int_scale
|
132
|
+
assert_equal [-11,-1], Decimal("-1.1").to_int_scale
|
133
|
+
assert_equal [-110,0], Decimal("-110").to_int_scale
|
134
|
+
assert_equal [110,-1], Decimal("11.0").to_int_scale
|
135
|
+
assert_equal [11,-1], Decimal("1.1").to_int_scale
|
136
|
+
assert_equal [110,0], Decimal("110").to_int_scale
|
137
|
+
|
138
|
+
assert_equal [-11,0], Decimal("-11.0").reduce.to_int_scale
|
139
|
+
assert_equal [-11,-1], Decimal("-1.1").reduce.to_int_scale
|
140
|
+
assert_equal [-11,1], Decimal("-110").reduce.to_int_scale
|
141
|
+
assert_equal [11,0], Decimal("11.0").reduce.to_int_scale
|
142
|
+
assert_equal [11,-1], Decimal("1.1").reduce.to_int_scale
|
143
|
+
assert_equal [11,1], Decimal("110").reduce.to_int_scale
|
144
|
+
|
145
|
+
|
146
|
+
assert_equal Decimal('2.1'), Decimal('2.1').remainder(Decimal('3'))
|
147
|
+
assert_equal Decimal('-2.1'), Decimal('-2.1').remainder(Decimal('3'))
|
148
|
+
assert_equal Decimal('2.1'), Decimal('2.1').remainder(Decimal('-3'))
|
149
|
+
assert_equal Decimal('-2.1'), Decimal('-2.1').remainder(Decimal('-3'))
|
150
|
+
assert_equal Decimal('1'), Decimal('10').remainder(Decimal('3'))
|
151
|
+
assert_equal Decimal('-1'), Decimal('-10').remainder(Decimal('3'))
|
152
|
+
assert_equal Decimal('1'), Decimal('10').remainder(Decimal('-3'))
|
153
|
+
assert_equal Decimal('-1'), Decimal('-10').remainder(Decimal('-3'))
|
154
|
+
assert_equal Decimal('0.2'), Decimal('10.2').remainder(Decimal('1'))
|
155
|
+
assert_equal Decimal('0.1'), Decimal('10').remainder(Decimal('0.3'))
|
156
|
+
assert_equal Decimal('1.0'), Decimal('3.6').remainder(Decimal('1.3'))
|
157
|
+
assert_equal Decimal('2'), Decimal('2').remainder(Decimal('3'))
|
158
|
+
assert_equal Decimal('1'), Decimal('10').remainder(Decimal('3'))
|
159
|
+
assert_equal Decimal('.1'), Decimal('1').remainder(Decimal('0.3'))
|
160
|
+
|
161
|
+
assert_equal Decimal('0'), Decimal('2.1').divide_int(Decimal('3'))
|
162
|
+
assert_equal Decimal('0'), Decimal('-2.1').divide_int(Decimal('3'))
|
163
|
+
assert_equal Decimal('0'), Decimal('2.1').divide_int(Decimal('-3'))
|
164
|
+
assert_equal Decimal('0'), Decimal('-2.1').divide_int(Decimal('-3'))
|
165
|
+
assert_equal Decimal('3'), Decimal('10').divide_int(Decimal('3'))
|
166
|
+
assert_equal Decimal('-3'), Decimal('-10').divide_int(Decimal('3'))
|
167
|
+
assert_equal Decimal('-3'), Decimal('10').divide_int(Decimal('-3'))
|
168
|
+
assert_equal Decimal('3'), Decimal('-10').divide_int(Decimal('-3'))
|
169
|
+
assert_equal Decimal('10'), Decimal('10.2').divide_int(Decimal('1'))
|
170
|
+
assert_equal Decimal('33'), Decimal('10').divide_int(Decimal('0.3'))
|
171
|
+
assert_equal Decimal('2'), Decimal('3.6').divide_int(Decimal('1.3'))
|
172
|
+
assert_equal Decimal('0'), Decimal('2').divide_int(Decimal('3'))
|
173
|
+
assert_equal Decimal('3'), Decimal('10').divide_int(Decimal('3'))
|
174
|
+
assert_equal Decimal('3'), Decimal('1').divide_int(Decimal('0.3'))
|
175
|
+
|
176
|
+
assert_equal Decimal('2.1'), Decimal('2.1').modulo(Decimal('3'))
|
177
|
+
assert_equal Decimal('0.9'), Decimal('-2.1').modulo(Decimal('3'))
|
178
|
+
assert_equal Decimal('-0.9'), Decimal('2.1').modulo(Decimal('-3'))
|
179
|
+
assert_equal Decimal('-2.1'), Decimal('-2.1').modulo(Decimal('-3'))
|
180
|
+
assert_equal Decimal('1'), Decimal('10').modulo(Decimal('3'))
|
181
|
+
assert_equal Decimal('2'), Decimal('-10').modulo(Decimal('3'))
|
182
|
+
assert_equal Decimal('-2'), Decimal('10').modulo(Decimal('-3'))
|
183
|
+
assert_equal Decimal('-1'), Decimal('-10').modulo(Decimal('-3'))
|
184
|
+
assert_equal Decimal('0.2'), Decimal('10.2').modulo(Decimal('1'))
|
185
|
+
assert_equal Decimal('0.1'), Decimal('10').modulo(Decimal('0.3'))
|
186
|
+
assert_equal Decimal('1.0'), Decimal('3.6').modulo(Decimal('1.3'))
|
187
|
+
assert_equal Decimal('2'), Decimal('2').modulo(Decimal('3'))
|
188
|
+
assert_equal Decimal('1'), Decimal('10').modulo(Decimal('3'))
|
189
|
+
assert_equal Decimal('.1'), Decimal('1').modulo(Decimal('0.3'))
|
190
|
+
|
191
|
+
assert_equal Decimal('0'), Decimal('2.1').div(Decimal('3'))
|
192
|
+
assert_equal Decimal('-1'), Decimal('-2.1').div(Decimal('3'))
|
193
|
+
assert_equal Decimal('-1'), Decimal('2.1').div(Decimal('-3'))
|
194
|
+
assert_equal Decimal('0'), Decimal('-2.1').div(Decimal('-3'))
|
195
|
+
assert_equal Decimal('3'), Decimal('10').div(Decimal('3'))
|
196
|
+
assert_equal Decimal('-4'), Decimal('-10').div(Decimal('3'))
|
197
|
+
assert_equal Decimal('-4'), Decimal('10').div(Decimal('-3'))
|
198
|
+
assert_equal Decimal('3'), Decimal('-10').div(Decimal('-3'))
|
199
|
+
assert_equal Decimal('10'), Decimal('10.2').div(Decimal('1'))
|
200
|
+
assert_equal Decimal('33'), Decimal('10').div(Decimal('0.3'))
|
201
|
+
assert_equal Decimal('2'), Decimal('3.6').div(Decimal('1.3'))
|
202
|
+
assert_equal Decimal('0'), Decimal('2').div(Decimal('3'))
|
203
|
+
assert_equal Decimal('3'), Decimal('10').div(Decimal('3'))
|
204
|
+
assert_equal Decimal('3'), Decimal('1').div(Decimal('0.3'))
|
205
|
+
|
206
|
+
assert_equal Decimal('-0.9'), Decimal('2.1').remainder_near(Decimal('3'))
|
207
|
+
assert_equal Decimal('-2'), Decimal('10').remainder_near(Decimal('6'))
|
208
|
+
assert_equal Decimal('1'), Decimal('10').remainder_near(Decimal('3'))
|
209
|
+
assert_equal Decimal('-1'), Decimal('-10').remainder_near(Decimal('3'))
|
210
|
+
assert_equal Decimal('0.2'), Decimal('10.2').remainder_near(Decimal('1'))
|
211
|
+
assert_equal Decimal('0.1'), Decimal('10').remainder_near(Decimal('0.3'))
|
212
|
+
assert_equal Decimal('-0.3'), Decimal('3.6').remainder_near(Decimal('1.3'))
|
213
|
+
|
214
|
+
|
215
|
+
assert_equal 2, Decimal('123.4567').adjusted_exponent
|
216
|
+
assert_equal 2, Decimal('123.45670').adjusted_exponent
|
217
|
+
assert_equal 2, Decimal.context.scaleb(Decimal('1.2345670'),2).adjusted_exponent
|
218
|
+
assert_equal 2, Decimal.context.scaleb(Decimal('1.2345670'),Decimal('2')).adjusted_exponent
|
219
|
+
assert_equal Decimal(2), Decimal.context.logb(Decimal('123.4567'))
|
220
|
+
assert_equal Decimal(2), Decimal.context.logb(Decimal('123.45670'))
|
221
|
+
assert_equal 3, Decimal('123.4567').fractional_exponent
|
222
|
+
assert_equal 3, Decimal('123.45670').fractional_exponent
|
223
|
+
|
224
|
+
assert_equal(-7, Decimal.context.normalized_integral_exponent(Decimal('123.4567')))
|
225
|
+
assert_equal(1234567000, Decimal.context.normalized_integral_significand(Decimal('123.4567')))
|
226
|
+
|
227
|
+
assert_equal 7, Decimal('123.4567').number_of_digits
|
228
|
+
assert_equal 8, Decimal('123.45670').number_of_digits
|
229
|
+
assert_equal 7, Decimal('123.45670').reduce.number_of_digits
|
230
|
+
|
231
|
+
assert_equal 1234567, Decimal('123.4567').integral_significand
|
232
|
+
assert_equal 12345670, Decimal('123.45670').integral_significand
|
233
|
+
assert_equal 1234567, Decimal('123.45670').reduce.integral_significand
|
234
|
+
|
235
|
+
assert_equal 2, Decimal('-123.4567').adjusted_exponent
|
236
|
+
assert_equal 2, Decimal('-123.45670').adjusted_exponent
|
237
|
+
assert_equal 2, Decimal.context.scaleb(Decimal('-1.2345670'),2).adjusted_exponent
|
238
|
+
assert_equal 2, Decimal.context.scaleb(Decimal('-1.2345670'),Decimal('2')).adjusted_exponent
|
239
|
+
assert_equal Decimal(2), Decimal.context.logb(Decimal('-123.4567'))
|
240
|
+
assert_equal Decimal(2), Decimal.context.logb(Decimal('-123.45670'))
|
241
|
+
assert_equal 3, Decimal('-123.4567').fractional_exponent
|
242
|
+
assert_equal 3, Decimal('-123.45670').fractional_exponent
|
243
|
+
|
244
|
+
assert_equal(-7, Decimal.context.normalized_integral_exponent(Decimal('-123.4567')))
|
245
|
+
assert_equal(1234567000, Decimal.context.normalized_integral_significand(Decimal('-123.4567')))
|
246
|
+
|
247
|
+
assert_equal 7, Decimal('-123.4567').number_of_digits
|
248
|
+
# assert_equal 9, Decimal('123.45670').number_of_digits # not with BigDecimal
|
249
|
+
assert_equal 7, Decimal('-123.45670').reduce.number_of_digits
|
250
|
+
|
251
|
+
assert_equal(1234567, Decimal('-123.4567').integral_significand)
|
252
|
+
#assert_equal(12345670, Decimal('-123.45670').integral_significand) # not with BigDecimal
|
253
|
+
assert_equal(1234567, Decimal('-123.45670').reduce.integral_significand)
|
254
|
+
|
255
|
+
x = Decimal('123.4567')
|
256
|
+
assert_equal x, Decimal(x.integral_significand)*10**x.integral_exponent
|
257
|
+
assert_equal x, Decimal(Decimal.context.normalized_integral_significand(x))*10**Decimal.context.normalized_integral_exponent(x)
|
258
|
+
|
259
|
+
assert_equal x, Decimal.context.scaleb(x.integral_significand, x.integral_exponent)
|
260
|
+
assert_equal x, Decimal.context.scaleb(Decimal.context.normalized_integral_significand(x),Decimal.context.normalized_integral_exponent(x))
|
261
|
+
|
262
|
+
x = Decimal('-123.4567')
|
263
|
+
assert_equal x.abs, Decimal(x.integral_significand)*10**x.integral_exponent
|
264
|
+
assert_equal x.abs, Decimal(Decimal.context.normalized_integral_significand(x))*10**Decimal.context.normalized_integral_exponent(x)
|
265
|
+
|
266
|
+
assert_equal x.abs, Decimal.context.scaleb(x.integral_significand, x.integral_exponent)
|
267
|
+
assert_equal x.abs, Decimal.context.scaleb(Decimal.context.normalized_integral_significand(x),Decimal.context.normalized_integral_exponent(x))
|
268
|
+
|
269
|
+
Decimal.context.precision = 3
|
270
|
+
Decimal.context.rounding = :half_up
|
271
|
+
assert_equal Decimal("100"), (+Decimal('100.4'))
|
272
|
+
assert_equal Decimal("101"), (+Decimal('101.4'))
|
273
|
+
assert_equal Decimal("101"), (+Decimal('100.5'))
|
274
|
+
assert_equal Decimal("102"), (+Decimal('101.5'))
|
275
|
+
assert_equal Decimal("-101"), (+Decimal('-100.5'))
|
276
|
+
assert_equal Decimal("-102"), (+Decimal('-101.5'))
|
277
|
+
assert_equal Decimal("-100"), (+Decimal('-100.4'))
|
278
|
+
assert_equal Decimal("-101"), (+Decimal('-101.4'))
|
279
|
+
Decimal.context.rounding = :half_even
|
280
|
+
assert_equal Decimal("100"), (+Decimal('100.5'))
|
281
|
+
assert_equal Decimal("101"), (+Decimal('100.51'))
|
282
|
+
assert_equal Decimal("101"), (+Decimal('100.6'))
|
283
|
+
assert_equal Decimal("102"), (+Decimal('101.5'))
|
284
|
+
assert_equal Decimal("101"), (+Decimal('101.4'))
|
285
|
+
assert_equal Decimal("-100"), (+Decimal('-100.5'))
|
286
|
+
assert_equal Decimal("-102"), (+Decimal('-101.5'))
|
287
|
+
assert_equal Decimal("-101"), (+Decimal('-101.4'))
|
288
|
+
Decimal.context.rounding = :half_down
|
289
|
+
assert_equal Decimal("100"), (+Decimal('100.5'))
|
290
|
+
assert_equal Decimal("101"), (+Decimal('101.5'))
|
291
|
+
assert_equal Decimal("-100"), (+Decimal('-100.5'))
|
292
|
+
assert_equal Decimal("-101"), (+Decimal('-101.5'))
|
293
|
+
Decimal.context.rounding = :down
|
294
|
+
assert_equal Decimal("100"), (+Decimal('100.9'))
|
295
|
+
assert_equal Decimal("101"), (+Decimal('101.9'))
|
296
|
+
assert_equal Decimal("-100"), (+Decimal('-100.9'))
|
297
|
+
assert_equal Decimal("-101"), (+Decimal('-101.9'))
|
298
|
+
Decimal.context.rounding = :up
|
299
|
+
assert_equal Decimal("101"), (+Decimal('100.1'))
|
300
|
+
assert_equal Decimal("102"), (+Decimal('101.1'))
|
301
|
+
assert_equal Decimal("-101"), (+Decimal('-100.1'))
|
302
|
+
assert_equal Decimal("-102"), (+Decimal('-101.1'))
|
303
|
+
Decimal.context.rounding = :floor
|
304
|
+
assert_equal Decimal("100"), (+Decimal('100.9'))
|
305
|
+
assert_equal Decimal("101"), (+Decimal('101.9'))
|
306
|
+
assert_equal Decimal("-101"), (+Decimal('-100.9'))
|
307
|
+
assert_equal Decimal("-102"), (+Decimal('-101.9'))
|
308
|
+
Decimal.context.rounding = :ceiling
|
309
|
+
assert_equal Decimal("101"), (+Decimal('100.1'))
|
310
|
+
assert_equal Decimal("102"), (+Decimal('101.1'))
|
311
|
+
assert_equal Decimal("-100"), (+Decimal('-100.1'))
|
312
|
+
assert_equal Decimal("-101"), (+Decimal('-101.1'))
|
313
|
+
|
314
|
+
end
|
315
|
+
|
316
|
+
def test_exp
|
317
|
+
Decimal.context.precision = 100
|
318
|
+
Decimal.context.rounding = :half_even
|
319
|
+
e_100 = "2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427"
|
320
|
+
assert_equal e_100, exp(Decimal(1)).to_s
|
321
|
+
assert_equal Decimal(e_100), exp(Decimal(1))
|
322
|
+
assert_equal e_100, exp1(Decimal(1)).to_s
|
323
|
+
assert_equal Decimal(e_100), exp1(Decimal(1))
|
324
|
+
end
|
325
|
+
|
326
|
+
def test_special
|
327
|
+
nan = Decimal(0)/Decimal(0)
|
328
|
+
inf_pos = Decimal(1)/Decimal(0)
|
329
|
+
inf_neg = Decimal(-1)/Decimal(0)
|
330
|
+
zero_pos = Decimal(0)
|
331
|
+
zero_neg = Decimal('-0')
|
332
|
+
pos = Decimal(1)
|
333
|
+
neg = Decimal(-1)
|
334
|
+
|
335
|
+
assert nan.nan?
|
336
|
+
assert nan.special?
|
337
|
+
assert !nan.zero?
|
338
|
+
assert !nan.finite?
|
339
|
+
assert !nan.infinite?
|
340
|
+
assert inf_pos.infinite?
|
341
|
+
assert !inf_pos.finite?
|
342
|
+
assert !inf_pos.nan?
|
343
|
+
assert inf_pos.special?
|
344
|
+
assert inf_neg.infinite?
|
345
|
+
assert !inf_neg.finite?
|
346
|
+
assert !inf_neg.nan?
|
347
|
+
assert inf_neg.special?
|
348
|
+
assert zero_pos.finite?
|
349
|
+
assert zero_neg.finite?
|
350
|
+
assert zero_pos.zero?
|
351
|
+
assert zero_neg.zero?
|
352
|
+
assert !pos.zero?
|
353
|
+
assert !neg.zero?
|
354
|
+
assert pos.nonzero?
|
355
|
+
assert neg.nonzero?
|
356
|
+
|
357
|
+
#assert nan.sign.nil?
|
358
|
+
assert_equal(+1, inf_pos.sign)
|
359
|
+
assert_equal(-1, inf_neg.sign)
|
360
|
+
assert_equal(+1, zero_pos.sign)
|
361
|
+
assert_equal(-1, zero_neg.sign)
|
362
|
+
assert_equal(+1, pos.sign)
|
363
|
+
assert_equal(-1, neg.sign)
|
364
|
+
end
|
365
|
+
|
366
|
+
def test_context_parameters
|
367
|
+
#Decimal.context = Decimal.Context
|
368
|
+
Decimal.context.precision = 3
|
369
|
+
Decimal.context.rounding = :half_even
|
370
|
+
x = Decimal(1)
|
371
|
+
y = Decimal(3)
|
372
|
+
assert_equal Decimal('0.333'), x.divide(y)
|
373
|
+
assert_equal Decimal('0.3333'), x.divide(y, Decimal.Context(:precision=>4))
|
374
|
+
assert_equal Decimal('0.333'), x.divide(y)
|
375
|
+
assert_equal Decimal('0.33333'), x.divide(y, :precision=>5)
|
376
|
+
end
|
377
|
+
|
378
|
+
def test_integer
|
379
|
+
|
380
|
+
%w{ 0 0E10 0E-10 12.0 12 120E-1 12.1E10 }.each do |x|
|
381
|
+
assert Decimal(x).integral?, "Decimal('#{x}').integral?"
|
382
|
+
end
|
383
|
+
|
384
|
+
[12, Rational(6,2)].each do |x|
|
385
|
+
assert Decimal(x).integral?, "Decimal(#{x}).integral?"
|
386
|
+
end
|
387
|
+
|
388
|
+
%w{ NaN Inf 12.1 121E-1 }.each do |x|
|
389
|
+
assert !Decimal(x).integral?, "!Decimal('#{x}').integral?"
|
390
|
+
end
|
391
|
+
|
392
|
+
[ Rational(121,10) ].each do |x|
|
393
|
+
assert !Decimal(x).integral?, "!Decimal(#{x}).integral?"
|
394
|
+
end
|
395
|
+
|
396
|
+
end
|
397
|
+
|
398
|
+
end
|