ruby-decimal 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +18 -3
- data/Manifest.txt +4 -0
- data/README.txt +107 -16
- data/lib/decimal/decimal.rb +294 -104
- data/lib/decimal/shortcut.rb +6 -0
- data/lib/decimal/version.rb +1 -1
- data/test/all_tests.rb +3 -0
- data/test/test_basic.rb +0 -2
- data/test/test_define_conversions.rb +12 -0
- data/test/test_epsilon.rb +34 -0
- data/test/test_ulp.rb +76 -0
- metadata +7 -2
data/lib/decimal/version.rb
CHANGED
data/test/all_tests.rb
CHANGED
@@ -9,3 +9,6 @@ require File.dirname(__FILE__) + '/test_coercion.rb'
|
|
9
9
|
require File.dirname(__FILE__) + '/test_to_int.rb'
|
10
10
|
require File.dirname(__FILE__) + '/test_to_rf.rb'
|
11
11
|
require File.dirname(__FILE__) + '/test_define_conversions.rb'
|
12
|
+
require File.dirname(__FILE__) + '/test_odd_even.rb'
|
13
|
+
require File.dirname(__FILE__) + '/test_odd_epsilon.rb'
|
14
|
+
require File.dirname(__FILE__) + '/test_ulp.rb'
|
data/test/test_basic.rb
CHANGED
@@ -95,6 +95,18 @@ class TestDefineConversions < Test::Unit::TestCase
|
|
95
95
|
assert_equal Decimal('11'), Decimal(11.0)
|
96
96
|
assert Decimal(11.0).is_a?(Decimal)
|
97
97
|
|
98
|
+
Decimal.context.define_conversion_from(Float) do |x, context|
|
99
|
+
Decimal.context(context, :exact=>true) do
|
100
|
+
s,e = Math.frexp(x)
|
101
|
+
s = Math.ldexp(s, Float::MANT_DIG).to_i
|
102
|
+
e -= Float::MANT_DIG
|
103
|
+
Decimal(s*(Float::RADIX**e))
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
assert_equal '0.1000000000000000055511151231257827021181583404541015625', Decimal(0.1).to_s
|
108
|
+
assert_equal '1.100000000000000088817841970012523233890533447265625', Decimal(1.1).to_s
|
109
|
+
|
98
110
|
end
|
99
111
|
|
100
112
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper.rb'
|
2
|
+
|
3
|
+
class TestEpsilon < Test::Unit::TestCase
|
4
|
+
|
5
|
+
|
6
|
+
def setup
|
7
|
+
initialize_context
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_epsilon
|
11
|
+
eps = Decimal.context.epsilon
|
12
|
+
assert_equal((Decimal(1).next_plus - Decimal(1)), eps)
|
13
|
+
assert_equal Decimal(1,1,1-Decimal.context.precision), eps
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_strict_epsilon
|
17
|
+
|
18
|
+
[:up, :ceiling, :down, :floor, :half_up, :half_down, :half_even, :up05].each do |rounding|
|
19
|
+
Decimal.context.rounding = rounding
|
20
|
+
eps = Decimal.context.strict_epsilon
|
21
|
+
eps_1 = Decimal(1)+eps
|
22
|
+
r = eps.next_minus
|
23
|
+
r_1 = Decimal(1)+r
|
24
|
+
assert((eps_1 > Decimal(1)) && (r_1 == Decimal(1)), "Test stritct epsilon for rounding #{rounding}")
|
25
|
+
assert_equal(((Decimal(1)+eps)-Decimal(1)), Decimal.context.epsilon)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_half_epsilon
|
30
|
+
assert_equal Decimal.context.epsilon/2, Decimal.context.half_epsilon
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
end
|
data/test/test_ulp.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper.rb'
|
2
|
+
|
3
|
+
class TestUlp < Test::Unit::TestCase
|
4
|
+
|
5
|
+
|
6
|
+
def setup
|
7
|
+
initialize_context
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
NUMBERS = %w{
|
12
|
+
0.0000
|
13
|
+
0.0001
|
14
|
+
0.0002
|
15
|
+
0.0003
|
16
|
+
0.0999
|
17
|
+
0.1000
|
18
|
+
0.1001
|
19
|
+
0.1999
|
20
|
+
0.2000
|
21
|
+
0.2001
|
22
|
+
0.8999
|
23
|
+
0.9000
|
24
|
+
0.9001
|
25
|
+
0.9999
|
26
|
+
1.0000
|
27
|
+
1.0001
|
28
|
+
1E27
|
29
|
+
1.0001E27
|
30
|
+
}
|
31
|
+
|
32
|
+
def test_ulp_4dig
|
33
|
+
Decimal.context.precision = 4
|
34
|
+
Decimal.context.emin = -99
|
35
|
+
Decimal.context.emax = 99
|
36
|
+
|
37
|
+
(NUMBERS+[Decimal.zero,Decimal.zero(-1),
|
38
|
+
Decimal.context.minimum_nonzero, Decimal.context.maximum_subnormal, Decimal.context.minimum_normal,
|
39
|
+
Decimal.context.maximum_finite]).each do |n|
|
40
|
+
x = Decimal(n)
|
41
|
+
if x >= 0
|
42
|
+
assert_equal x-x.next_minus, x.ulp
|
43
|
+
else
|
44
|
+
assert_equal x.next_plus-x, x.ulp
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
assert_equal Decimal.context.maximum_finite-Decimal.context.maximum_finite.next_minus, Decimal.infinity.ulp
|
49
|
+
assert_equal Decimal('0.00001'), Decimal('0.1').ulp
|
50
|
+
assert_equal Decimal('1E-7'), Decimal('0.001').ulp
|
51
|
+
assert_equal Decimal('0.00001'), Decimal('0.100').ulp
|
52
|
+
assert_equal Decimal('0.00001'), Decimal('0.1000').ulp
|
53
|
+
assert_equal Decimal('0.0001'), Decimal('0.1001').ulp
|
54
|
+
assert_equal Decimal('0.0001'), Decimal('0.9').ulp
|
55
|
+
assert_equal Decimal('0.0001'), Decimal('0.99999').ulp
|
56
|
+
assert_equal Decimal('0.0001'), Decimal('0.9000').ulp
|
57
|
+
assert_equal Decimal('0.0001'), Decimal('0.990').ulp
|
58
|
+
assert_equal Decimal('0.0001'), Decimal('0.23').ulp
|
59
|
+
assert_equal Decimal('1E-101'), Decimal('1.001E-98').ulp
|
60
|
+
assert_equal Decimal('1E-102'), Decimal('1.001E-99').ulp
|
61
|
+
assert_equal Decimal('1E-102'), Decimal('1E-99').ulp
|
62
|
+
assert_equal Decimal('1E-102'), Decimal('9.99E-100').ulp
|
63
|
+
assert_equal Decimal('1E-102'), Decimal('1E-102').ulp
|
64
|
+
assert_equal Decimal('1E-102'), Decimal('0').ulp
|
65
|
+
assert_equal Decimal('1E-102'), Decimal('0').ulp
|
66
|
+
|
67
|
+
Decimal.context.exact = true
|
68
|
+
assert Decimal(1).ulp.nan?, "No ulps can be computed in exact contexts"
|
69
|
+
Decimal.context.traps[Decimal::InvalidOperation] = true
|
70
|
+
assert_raise(Decimal::InvalidOperation) { Decimal(1).ulp }
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
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.2.
|
4
|
+
version: 0.2.1
|
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-23 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- Rakefile
|
41
41
|
- lib/decimal.rb
|
42
42
|
- lib/decimal/decimal.rb
|
43
|
+
- lib/decimal/shortcut.rb
|
43
44
|
- lib/decimal/support.rb
|
44
45
|
- lib/decimal/version.rb
|
45
46
|
- setup.rb
|
@@ -63,6 +64,7 @@ files:
|
|
63
64
|
- test/test_comparisons.rb
|
64
65
|
- test/test_dectest.rb
|
65
66
|
- test/test_define_conversions.rb
|
67
|
+
- test/test_epsilon.rb
|
66
68
|
- test/test_exact.rb
|
67
69
|
- test/test_flags.rb
|
68
70
|
- test/test_multithreading.rb
|
@@ -70,6 +72,7 @@ files:
|
|
70
72
|
- test/test_round.rb
|
71
73
|
- test/test_to_int.rb
|
72
74
|
- test/test_to_rf.rb
|
75
|
+
- test/test_ulp.rb
|
73
76
|
has_rdoc: true
|
74
77
|
homepage: http://ruby-decimal.rubyforge.org
|
75
78
|
licenses: []
|
@@ -113,6 +116,7 @@ test_files:
|
|
113
116
|
- test/test_comparisons.rb
|
114
117
|
- test/test_dectest.rb
|
115
118
|
- test/test_define_conversions.rb
|
119
|
+
- test/test_epsilon.rb
|
116
120
|
- test/test_exact.rb
|
117
121
|
- test/test_flags.rb
|
118
122
|
- test/test_multithreading.rb
|
@@ -120,3 +124,4 @@ test_files:
|
|
120
124
|
- test/test_round.rb
|
121
125
|
- test/test_to_int.rb
|
122
126
|
- test/test_to_rf.rb
|
127
|
+
- test/test_ulp.rb
|