ruby-decimal 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -1
- data/README.txt +6 -3
- data/lib/decimal/decimal.rb +1680 -486
- data/lib/decimal/version.rb +1 -1
- data/test/test_dectest.rb +15 -6
- data/test/test_exact.rb +120 -5
- data/test/test_odd_even.rb +78 -0
- metadata +4 -2
data/lib/decimal/version.rb
CHANGED
data/test/test_dectest.rb
CHANGED
@@ -40,15 +40,14 @@ FUNCTIONS = {
|
|
40
40
|
'nexttoward'=>'next_toward',
|
41
41
|
'tosci'=>'to_sci_string',
|
42
42
|
'toeng'=>'to_eng_string',
|
43
|
-
'class'=>'number_class'
|
43
|
+
'class'=>'number_class',
|
44
|
+
'power'=>'power',
|
45
|
+
'log10'=>'log10',
|
46
|
+
'exp'=>'exp',
|
47
|
+
'ln'=>'ln'
|
44
48
|
}
|
45
49
|
# Functions not yet implemented
|
46
50
|
PENDING = %w{
|
47
|
-
exp
|
48
|
-
power
|
49
|
-
ln
|
50
|
-
log10
|
51
|
-
|
52
51
|
rotate
|
53
52
|
shift
|
54
53
|
trim
|
@@ -84,6 +83,15 @@ FLAG_NAMES = {
|
|
84
83
|
'conversion_syntax'=>:ConversionSyntax
|
85
84
|
}
|
86
85
|
|
86
|
+
# Excluded tests that we don't currently pass
|
87
|
+
EXCEPTIONS = %w{
|
88
|
+
powx1183 powx1184
|
89
|
+
powx4001 powx4002 powx4003 powx4005
|
90
|
+
powx4008 powx4010 powx4012 powx4014
|
91
|
+
logx901 logx902 logx903 logx903 logx905
|
92
|
+
expx901 expx902 expx903 expx905
|
93
|
+
lnx901 lnx902 lnx903 lnx905
|
94
|
+
}
|
87
95
|
|
88
96
|
|
89
97
|
def unquote(txt)
|
@@ -136,6 +144,7 @@ class TestBasic < Test::Unit::TestCase
|
|
136
144
|
|
137
145
|
$test_id = id
|
138
146
|
funct = FUNCTIONS[original_funct=funct]
|
147
|
+
next if EXCEPTIONS.include?(id)
|
139
148
|
if funct
|
140
149
|
# do test
|
141
150
|
msg = "Test #{id}: #{funct}(#{valstemp.join(',')}) = #{ans}"
|
data/test/test_exact.rb
CHANGED
@@ -9,24 +9,139 @@ class TestExact < Test::Unit::TestCase
|
|
9
9
|
end
|
10
10
|
|
11
11
|
|
12
|
-
def
|
12
|
+
def test_exact_no_traps
|
13
13
|
|
14
14
|
Decimal.context.exact = true
|
15
|
+
Decimal.context.traps[Decimal::Inexact] = false
|
15
16
|
|
16
17
|
assert_equal Decimal("9"*100+"E-50"), Decimal('1E50')-Decimal('1E-50')
|
18
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
17
19
|
assert_equal Decimal(2),Decimal(6)/Decimal(3)
|
20
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
18
21
|
assert_equal Decimal('1.5'),Decimal(6)/Decimal(4)
|
22
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
19
23
|
assert_equal Decimal('15241578780673678546105778281054720515622620750190521'), Decimal('123456789123456789123456789')*Decimal('123456789123456789123456789')
|
24
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
25
|
+
assert_equal Decimal(2), Decimal('4').sqrt
|
26
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
27
|
+
assert_equal Decimal(4), Decimal('16').sqrt
|
28
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
29
|
+
|
30
|
+
assert_equal Decimal('42398.78077199232'), Decimal('1.23456')*Decimal('34343.232222')
|
31
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
32
|
+
assert_equal Decimal('12.369885'), Decimal('210.288045')/Decimal('17')
|
33
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
34
|
+
assert_equal Decimal('25'),Decimal('125')/Decimal('5')
|
35
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
36
|
+
assert_equal Decimal('12345678900000000000.1234567890'),Decimal('1234567890E10')+Decimal('1234567890E-10')
|
37
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
38
|
+
assert_equal Decimal('39304'),Decimal('34').power(Decimal(3))
|
39
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
40
|
+
assert_equal Decimal('39.304'),Decimal('3.4').power(Decimal(3))
|
41
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
42
|
+
assert_equal Decimal('4'),Decimal('16').power(Decimal('0.5'))
|
43
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
44
|
+
assert_equal Decimal('4'),Decimal('10000.0').log10
|
45
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
46
|
+
assert_equal Decimal('-5'),Decimal('0.00001').log10
|
47
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
48
|
+
assert_equal Decimal.infinity, Decimal.infinity.exp
|
49
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
50
|
+
assert_equal Decimal.zero, Decimal.infinity(-1).exp
|
51
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
52
|
+
assert_equal Decimal(1), Decimal(0).exp
|
53
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
54
|
+
assert_equal Decimal.infinity(-1), Decimal(0).ln
|
55
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
56
|
+
assert_equal Decimal.infinity, Decimal.infinity.ln
|
57
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
58
|
+
assert_equal Decimal(0), Decimal(1).ln
|
59
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
60
|
+
|
61
|
+
|
62
|
+
assert((Decimal(1)/Decimal(3)).nan?)
|
63
|
+
assert Decimal.context.flags[Decimal::Inexact]
|
64
|
+
Decimal.context.flags[Decimal::Inexact] = false
|
65
|
+
assert((Decimal(18).power(Decimal('0.5'))).nan?)
|
66
|
+
assert Decimal.context.flags[Decimal::Inexact]
|
67
|
+
Decimal.context.flags[Decimal::Inexact] = false
|
68
|
+
assert((Decimal(18).power(Decimal('1.5'))).nan?)
|
69
|
+
assert Decimal.context.flags[Decimal::Inexact]
|
70
|
+
Decimal.context.flags[Decimal::Inexact] = false
|
71
|
+
assert Decimal(18).log10.nan?
|
72
|
+
assert Decimal.context.flags[Decimal::Inexact]
|
73
|
+
Decimal.context.flags[Decimal::Inexact] = false
|
74
|
+
assert Decimal(1).exp.nan?
|
75
|
+
assert Decimal.context.flags[Decimal::Inexact]
|
76
|
+
Decimal.context.flags[Decimal::Inexact] = false
|
77
|
+
assert Decimal('-1.2').exp.nan?
|
78
|
+
assert Decimal.context.flags[Decimal::Inexact]
|
79
|
+
Decimal.context.flags[Decimal::Inexact] = false
|
80
|
+
assert Decimal('1.1').ln.nan?
|
81
|
+
assert Decimal.context.flags[Decimal::Inexact]
|
82
|
+
Decimal.context.flags[Decimal::Inexact] = false
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_exact_traps
|
87
|
+
|
88
|
+
Decimal.context.exact = true
|
89
|
+
|
20
90
|
assert_nothing_raised(Decimal::Inexact){ Decimal(6)/Decimal(4) }
|
21
|
-
assert_raise(Decimal::Inexact){ Decimal(1)/Decimal(3) }
|
22
|
-
# assert_raise(Decimal::Inexact){ Decimal(2).sqrt }
|
23
91
|
|
92
|
+
assert_equal Decimal("9"*100+"E-50"), Decimal('1E50')-Decimal('1E-50')
|
93
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
94
|
+
assert_equal Decimal(2),Decimal(6)/Decimal(3)
|
95
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
96
|
+
assert_equal Decimal('1.5'),Decimal(6)/Decimal(4)
|
97
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
98
|
+
assert_equal Decimal('15241578780673678546105778281054720515622620750190521'), Decimal('123456789123456789123456789')*Decimal('123456789123456789123456789')
|
99
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
24
100
|
assert_equal Decimal(2), Decimal('4').sqrt
|
101
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
25
102
|
assert_equal Decimal(4), Decimal('16').sqrt
|
26
|
-
|
103
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
104
|
+
assert_equal Decimal.infinity, Decimal.infinity.exp
|
105
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
106
|
+
assert_equal Decimal.zero, Decimal.infinity(-1).exp
|
107
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
108
|
+
assert_equal Decimal(1), Decimal(0).exp
|
109
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
27
110
|
|
111
|
+
assert_equal Decimal('42398.78077199232'), Decimal('1.23456')*Decimal('34343.232222')
|
112
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
113
|
+
assert_equal Decimal('12.369885'), Decimal('210.288045')/Decimal('17')
|
114
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
115
|
+
assert_equal Decimal('25'),Decimal('125')/Decimal('5')
|
116
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
117
|
+
assert_equal Decimal('12345678900000000000.1234567890'),Decimal('1234567890E10')+Decimal('1234567890E-10')
|
118
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
119
|
+
assert_equal Decimal('39304'),Decimal('34').power(Decimal(3))
|
120
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
121
|
+
assert_equal Decimal('39.304'),Decimal('3.4').power(Decimal(3))
|
122
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
123
|
+
assert_equal Decimal('4'),Decimal('16').power(Decimal('0.5'))
|
124
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
125
|
+
assert_equal Decimal('4'),Decimal('10000.0').log10
|
126
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
127
|
+
assert_equal Decimal('-5'),Decimal('0.00001').log10
|
128
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
129
|
+
assert_equal Decimal.infinity(-1), Decimal(0).ln
|
130
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
131
|
+
assert_equal Decimal.infinity, Decimal.infinity.ln
|
132
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
133
|
+
assert_equal Decimal(0), Decimal(1).ln
|
134
|
+
assert !Decimal.context.flags[Decimal::Inexact]
|
28
135
|
|
29
|
-
|
136
|
+
assert_raise(Decimal::Inexact){ Decimal(2).sqrt }
|
137
|
+
assert_raise(Decimal::Inexact){ Decimal(1)/Decimal(3) }
|
138
|
+
assert_raise(Decimal::Inexact){ Decimal(18).power(Decimal('0.5')) }
|
139
|
+
assert_raise(Decimal::Inexact){ Decimal(18).power(Decimal('1.5')) }
|
140
|
+
assert_raise(Decimal::Inexact){ Decimal(18).log10 }
|
141
|
+
assert_raise(Decimal::Inexact){ Decimal(1).exp }
|
142
|
+
assert_raise(Decimal::Inexact){ Decimal('1.2').exp }
|
143
|
+
assert_raise(Decimal::Inexact){ Decimal('1.2').ln }
|
30
144
|
|
145
|
+
end
|
31
146
|
|
32
147
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper.rb'
|
2
|
+
|
3
|
+
class TestOddEven < Test::Unit::TestCase
|
4
|
+
|
5
|
+
|
6
|
+
def setup
|
7
|
+
initialize_context
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_even
|
11
|
+
assert !Decimal.nan.even?
|
12
|
+
assert !Decimal.infinity.even?
|
13
|
+
assert !Decimal.infinity(-1).even?
|
14
|
+
assert Decimal.zero.even?
|
15
|
+
assert Decimal.zero(-1).even?
|
16
|
+
assert !Decimal('0.1').even?
|
17
|
+
assert !Decimal('-0.1').even?
|
18
|
+
assert !Decimal(-7).even?
|
19
|
+
assert Decimal(-6).even?
|
20
|
+
assert !Decimal(-5).even?
|
21
|
+
assert Decimal(-4).even?
|
22
|
+
assert !Decimal(-3).even?
|
23
|
+
assert Decimal(-2).even?
|
24
|
+
assert !Decimal(-1).even?
|
25
|
+
assert Decimal(0).even?
|
26
|
+
assert !Decimal(1).even?
|
27
|
+
assert Decimal(2).even?
|
28
|
+
assert !Decimal(3).even?
|
29
|
+
assert Decimal(4).even?
|
30
|
+
assert !Decimal(5).even?
|
31
|
+
assert Decimal(6).even?
|
32
|
+
assert !Decimal(7).even?
|
33
|
+
assert !Decimal('101').even?
|
34
|
+
assert Decimal('102').even?
|
35
|
+
assert !Decimal('103').even?
|
36
|
+
assert Decimal('10100').even?
|
37
|
+
assert Decimal('10200').even?
|
38
|
+
assert !Decimal('101.00').even?
|
39
|
+
assert Decimal('102.00').even?
|
40
|
+
assert !Decimal('101.01').even?
|
41
|
+
assert !Decimal('102.01').even?
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_odd
|
45
|
+
assert !Decimal.nan.odd?
|
46
|
+
assert !Decimal.infinity.odd?
|
47
|
+
assert !Decimal.infinity(-1).odd?
|
48
|
+
assert !Decimal.zero.odd?
|
49
|
+
assert !Decimal.zero(-1).odd?
|
50
|
+
assert !Decimal('0.1').odd?
|
51
|
+
assert !Decimal('-0.1').odd?
|
52
|
+
assert Decimal(-7).odd?
|
53
|
+
assert !Decimal(-6).odd?
|
54
|
+
assert Decimal(-5).odd?
|
55
|
+
assert !Decimal(-4).odd?
|
56
|
+
assert Decimal(-3).odd?
|
57
|
+
assert !Decimal(-2).odd?
|
58
|
+
assert Decimal(-1).odd?
|
59
|
+
assert !Decimal(0).odd?
|
60
|
+
assert Decimal(1).odd?
|
61
|
+
assert !Decimal(2).odd?
|
62
|
+
assert Decimal(3).odd?
|
63
|
+
assert !Decimal(4).odd?
|
64
|
+
assert Decimal(5).odd?
|
65
|
+
assert !Decimal(6).odd?
|
66
|
+
assert Decimal(7).odd?
|
67
|
+
assert Decimal(101).odd?
|
68
|
+
assert !Decimal(102).odd?
|
69
|
+
assert Decimal(103).odd?
|
70
|
+
assert !Decimal(10100).odd?
|
71
|
+
assert !Decimal(10200).odd?
|
72
|
+
assert Decimal('101.00').odd?
|
73
|
+
assert !Decimal('102.00').odd?
|
74
|
+
assert !Decimal('101.01').odd?
|
75
|
+
assert !Decimal('102.01').odd?
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-decimal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Javier Goizueta
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-21 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- test/test_exact.rb
|
67
67
|
- test/test_flags.rb
|
68
68
|
- test/test_multithreading.rb
|
69
|
+
- test/test_odd_even.rb
|
69
70
|
- test/test_round.rb
|
70
71
|
- test/test_to_int.rb
|
71
72
|
- test/test_to_rf.rb
|
@@ -115,6 +116,7 @@ test_files:
|
|
115
116
|
- test/test_exact.rb
|
116
117
|
- test/test_flags.rb
|
117
118
|
- test/test_multithreading.rb
|
119
|
+
- test/test_odd_even.rb
|
118
120
|
- test/test_round.rb
|
119
121
|
- test/test_to_int.rb
|
120
122
|
- test/test_to_rf.rb
|