long-decimal 0.00.09 → 0.00.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,8 @@
2
2
  #
3
3
  # testlongdecimal.rb -- runit test for longdecimal.rb
4
4
  #
5
- # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/test/testlongdecimal.rb,v 1.5 2006/03/04 21:49:00 bk1 Exp $
6
- # CVS-Label: $Name: PRE_ALPHA_0_09 $
5
+ # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/test/testlongdecimal.rb,v 1.6 2006/03/10 20:10:45 bk1 Exp $
6
+ # CVS-Label: $Name: PRE_ALPHA_0_10 $
7
7
  # Author: $Author: bk1 $ (Karl Brodowsky)
8
8
  #
9
9
 
@@ -18,7 +18,7 @@ load "lib/longdecimal.rb"
18
18
  #
19
19
  class TestLongDecimal_class < RUNIT::TestCase
20
20
 
21
- @RCS_ID='-$Id: testlongdecimal.rb,v 1.5 2006/03/04 21:49:00 bk1 Exp $-'
21
+ @RCS_ID='-$Id: testlongdecimal.rb,v 1.6 2006/03/10 20:10:45 bk1 Exp $-'
22
22
 
23
23
  def check_split_merge_words(x, l, wl)
24
24
  w = LongMath.split_to_words(x, l)
@@ -62,23 +62,39 @@ class TestLongDecimal_class < RUNIT::TestCase
62
62
 
63
63
  #
64
64
  # helper method for test_exp
65
+ # tests if exp(x) with precision prec is calculated correctly
65
66
  #
66
67
  def check_exp_floated(x, prec)
68
+
69
+ # make sure x is LongDecimal
67
70
  x0 = x
68
71
  x = x.to_ld
72
+ # calculate y = exp(x)
69
73
  y = LongMath.exp(x, prec)
74
+
75
+ # compare y against z = exp(x) calculated using regular floating point arithmetic
70
76
  z = Math.exp(x.to_f)
71
77
  yf = y.to_f
72
78
  assert((yf - z) / [yf.abs, z.abs, Float::MIN].max < 1e-9, "y=#{yf.to_s} and z=#{z.to_s} should be almost equal x=#{x}")
79
+
80
+ # check by taking log(exp(x))
81
+ # we have to take into account that we might not have enough
82
+ # significant digits, so we have to go down with the precision
83
+ if (y > 0) then
84
+ lprec = prec
85
+ if (y < 1) then
86
+ l10 = (Math.log(y.to_f) / Math.log(10)).round
87
+ lprec += l10
88
+ end
89
+ z = LongMath.log(y, lprec)
90
+ assert((x - z).abs <= z.unit, "x=#{x.to_s} and z=#{z.to_s} should be almost equal (y=#{y.to_s} lprec=#{lprec} prec=#{prec})")
91
+ end
92
+
93
+ # check by doing calculation with different internal rounding modes. They should not differ.
73
94
  yd = LongMath.exp_internal(x, prec, nil, nil, nil, nil, LongDecimal::ROUND_DOWN)
74
95
  yu = LongMath.exp_internal(x, prec, nil, nil, nil, nil, LongDecimal::ROUND_UP)
75
- # if (x >= 0)
76
96
  assert_equal(yd, yu, "the result yd/yu should not depend on the internal rounding mode x0=#{x0} x=#{x} p=#{prec} d=#{(yd-yu).to_f.to_s}")
77
97
  assert_equal(y, yu, "the result y/yu should not depend on the internal rounding mode x0=#{x0} x=#{x} p=#{prec} d=#{(y -yu).to_f.to_s}")
78
- # else
79
- # assert((yu - yd).abs <= y.unit, "the difference should not be too much x=#{x} d=#{(yd-yu).to_f.to_s}")
80
- # assert((yu - y ).abs <= y.unit, "the difference should not be too much x=#{x} d=#{(y -yu).to_f.to_s}")
81
- # end
82
98
  end
83
99
 
84
100
  #
@@ -108,6 +124,80 @@ class TestLongDecimal_class < RUNIT::TestCase
108
124
  check_exp_floated(-700, 100)
109
125
  end
110
126
 
127
+ #
128
+ # helper method for test_log
129
+ # tests if exp(x) with precision prec is calculated correctly
130
+ #
131
+ def check_log_floated(x, prec)
132
+
133
+ # make sure x is LongDecimal
134
+ x0 = x
135
+ x = x.to_ld
136
+ # calculate y = log(x)
137
+ y = LongMath.log(x, prec)
138
+
139
+ # compare y against z = exp(x) calculated using regular floating
140
+ # point arithmetic
141
+ if (x <= LongMath::MAX_FLOATABLE) then
142
+ xf = x.to_f
143
+ if (xf > 0) then
144
+ z = Math.log(x.to_f)
145
+ yf = y.to_f
146
+ assert((yf - z) / [yf.abs, z.abs, Float::MIN].max < 1e-9, "y=#{yf.to_s} and z=#{z.to_s} should be almost equal x=#{x}")
147
+ end
148
+ end
149
+
150
+ # check by taking exp(log(x))
151
+ # we have to take into account that we might not have enough
152
+ # significant digits, so we have to go down with the precision
153
+ if (y <= LongMath::MAX_EXP_ABLE) then
154
+ eprec = prec
155
+ if (y > 1) then
156
+ lx = 0
157
+ if (x > LongMath::MAX_FLOATABLE) then
158
+ puts("unusual x=#{x} y=#{y}\n")
159
+ lx = LongMath::MAX_EXP_ABLE
160
+ else
161
+ lx = Math.log(x.to_f)
162
+ end
163
+ l10 = (lx / Math.log(10)).ceil
164
+ eprec -= l10
165
+ end
166
+
167
+ z = LongMath.exp(y, eprec)
168
+ assert((x - z).abs <= z.unit, "x=#{x.to_s} and z=#{z.to_s} should be almost equal (y=#{y.to_s} eprec=#{eprec} prec=#{prec})")
169
+ end
170
+
171
+ # check by doing calculation with different internal rounding modes. They should not differ.
172
+ yd = LongMath.log_internal(x, prec, nil, nil, LongDecimal::ROUND_DOWN)
173
+ yu = LongMath.log_internal(x, prec, nil, nil, LongDecimal::ROUND_UP)
174
+ assert_equal(yd, yu, "the result yd/yu should not depend on the internal rounding mode x0=#{x0} x=#{x} p=#{prec} d=#{(yd-yu).to_f.to_s}")
175
+ assert_equal(y, yu, "the result y/yu should not depend on the internal rounding mode x0=#{x0} x=#{x} p=#{prec} d=#{(y -yu).to_f.to_s}")
176
+ end
177
+
178
+ #
179
+ # test the calculation of the exponential function
180
+ #
181
+ def test_log
182
+ check_log_floated(10**2000, 10)
183
+ check_log_floated(100, 10)
184
+ check_log_floated(1, 10)
185
+ check_log_floated(0.01, 10)
186
+ check_log_floated(1e-10, 10)
187
+ check_log_floated(1e-90, 10)
188
+ check_log_floated(1e-300, 10)
189
+ check_log_floated(LongDecimal(1, 2000), 10)
190
+
191
+ check_log_floated(10**2000, 100)
192
+ check_log_floated(100, 100)
193
+ check_log_floated(1, 100)
194
+ check_log_floated(0.01, 100)
195
+ check_log_floated(1e-10, 100)
196
+ check_log_floated(1e-90, 100)
197
+ check_log_floated(1e-300, 100)
198
+ check_log_floated(LongDecimal(1, 2000), 100)
199
+ end
200
+
111
201
  #
112
202
  # helper method for test_sqrtb
113
203
  #
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: long-decimal
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.00.09
7
- date: 2006-03-04 00:00:00 +01:00
6
+ version: 0.00.10
7
+ date: 2006-03-10 00:00:00 +01:00
8
8
  summary: LongDecimal for numbers with fixed point
9
9
  require_paths:
10
10
  - lib
@@ -180,9 +180,16 @@ files:
180
180
  - doc/classes/LongMath.src/M000119.html
181
181
  - doc/classes/LongMath.src/M000120.html
182
182
  - doc/classes/LongMath.src/M000121.html
183
+ - doc/classes/LongMath.src/M000122.html
184
+ - doc/classes/LongMath.src/M000123.html
185
+ - doc/classes/LongMath.src/M000124.html
186
+ - doc/classes/LongMath.src/M000125.html
187
+ - doc/classes/LongMath.src/M000126.html
188
+ - doc/classes/LongMath.src/M000127.html
183
189
  - doc/classes/LongDecimalRoundingMode/RoundingModeClass.src
184
190
  - doc/classes/LongDecimalRoundingMode/RoundingModeClass.html
185
191
  - doc/classes/LongDecimalRoundingMode/RoundingModeClass.src/M000122.html
192
+ - doc/classes/LongDecimalRoundingMode/RoundingModeClass.src/M000128.html
186
193
  - lib/longdecimal.rb
187
194
  - test/testlongdecimal.rb
188
195
  test_files: