intervals 0.3.63 → 0.4.75

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # Tests for the Struct::Float class.
4
+ #
5
+ # Copyright (c) 2006 Stefano Taschini <taschini@ieee.org>.
6
+ # Licensed under the terms of LGPL: http://www.gnu.org/copyleft/lesser.html
7
+
8
+ require 'test/unit'
9
+ require File.dirname(__FILE__) + '/../lib/struct_float.rb' if __FILE__ == $0
10
+
11
+ # Tests Struct::Float.
12
+ class Struct::Float::Test < Test::Unit::TestCase
13
+
14
+ def assert_consistent(f,s)
15
+ assert_equal(f,s.to_f)
16
+ assert_equal(s,f.to_struct)
17
+ assert_equal(s, eval(s.inspect))
18
+ assert(f.ulp.infinite? || (f/f.ulp).to_i == f/f.ulp, [f,f.ulp].inspect)
19
+ end
20
+
21
+ def assert_nan(s)
22
+ # NaN does not have a unique representation, and therefore
23
+ # it does not make sense to have it as a constant as for
24
+ # Infinity. In particular a NaN has 2047 as biased exponent and
25
+ # non-zero fraction
26
+ assert(2047, s.biased_exp)
27
+ assert(!s.fraction.zero?, s.fraction)
28
+ # You cannot test for NaN with equality. You MUST use nan?
29
+ assert(s.to_f.nan?, s.to_f)
30
+ assert_equal(s, eval(s.inspect))
31
+ end
32
+
33
+ def test_consistency
34
+ assert_consistent Infinity, Struct::Float[1,2047,0]
35
+ assert_consistent -Infinity, Struct::Float[-1,2047,0]
36
+ assert_consistent 2.0 ** -1074, Struct::Float[1,0,1]
37
+ assert_consistent 0.0, Struct::Float[1,0,0]
38
+ assert_consistent -0.0, Struct::Float[-1,0,0]
39
+ assert_consistent 1.0, Struct::Float[1,1023, 0]
40
+ assert_consistent 1/3.0, Struct::Float[1,1021,0x5555555555555]
41
+ assert_consistent 1/5.0, Struct::Float[1,1020,0x999999999999a]
42
+ end
43
+
44
+ def test_nan
45
+ assert_nan( (0.0/0.0).to_struct)
46
+ assert_nan( Struct::Float[-1,2047,34])
47
+ assert_nan( Struct::Float[+1,2047,34])
48
+ end
49
+
50
+ def test_denormalized
51
+ assert_consistent 34 * 2.0 ** -(1023+51), Struct::Float[1,0,34]
52
+ end
53
+
54
+ def test_normalized
55
+ assert_consistent -(2**52+34) * 2.0**(1043-1023-52), Struct::Float[-1,1043,34]
56
+ end
57
+
58
+ def test_samples
59
+ n = 1000
60
+ srand 12345
61
+ n.times{
62
+ s = Struct::Float[2*rand(2) -1, rand(2048), rand(2**52)]
63
+ f = s.to_f
64
+ if f.nan?
65
+ assert_nan(s)
66
+ else
67
+ assert_consistent(f,s)
68
+ end
69
+ }
70
+ end
71
+
72
+ end
data/var/clean-nan.rb ADDED
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # Alternative implementation of Interval::Simple.multiply that does
4
+ # not rely on catching exceptions to detect NaNs
5
+ #
6
+ # Copyright (c) 2006 Stefano Taschini <taschini@ieee.org>.
7
+ # Licensed under the terms of LGPL: http://www.gnu.org/copyleft/lesser.html
8
+
9
+ require File.dirname(__FILE__) + '/../lib/interval'
10
+
11
+ module Enumerable
12
+
13
+ # Enumerable#min fails if the collection includes a NaN. Instead,
14
+ # this methods returns a NaN.
15
+ def ieee_min
16
+ find( proc { min } ){|x| x.respond_to?(:nan?) && x.nan?}
17
+ end
18
+
19
+ # Enumerable#max fails if the collection includes a NaN. Instead,
20
+ # this methods returns a NaN.
21
+ def ieee_max
22
+ find( proc { max } ){|x| x.respond_to?(:nan?) && x.nan?}
23
+ end
24
+
25
+ end
26
+
27
+ class Interval::Simple
28
+
29
+ # Used to implement Interval's multiplicative operator (\*).
30
+ def multiply (other)
31
+ self.class.new(
32
+ FPU.down {
33
+ [inf * other.inf, inf * other.sup,
34
+ sup * other.inf, sup * other.sup]}.ieee_min,
35
+ FPU.up {
36
+ [inf * other.inf, inf * other.sup,
37
+ sup * other.inf, sup * other.sup]}.ieee_max)
38
+ end
39
+
40
+ end
41
+
42
+ # Tests exception-free multiplication.
43
+ class CleanNanTest < Test::Unit::TestCase
44
+
45
+ def assert_nan(s)
46
+ assert(s.nan?, "#{s} is not nan")
47
+ end
48
+
49
+ def test_ieee_min_and_max
50
+ assert_equal(1,[1,2,3].ieee_min)
51
+ assert_equal(3,[1,2,3].ieee_max)
52
+ assert_nan([1,0.0/0.0,3].ieee_min)
53
+ assert_nan([1,0.0/0.0,3].ieee_max)
54
+ end
55
+
56
+ end
57
+
58
+ Test::Unit.run = (__FILE__ != $0)
59
+
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: intervals
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.63
7
- date: 2006-03-31 00:00:00 +02:00
6
+ version: 0.4.75
7
+ date: 2006-04-09 00:00:00 +02:00
8
8
  summary: Interval arithmetic in Ruby.
9
9
  require_paths:
10
10
  - lib
@@ -28,14 +28,18 @@ cert_chain:
28
28
  authors:
29
29
  - Stefano Taschini
30
30
  files:
31
+ - test/runall.rb
31
32
  - test/data_sin.txt
32
33
  - test/data_cosh.txt
33
34
  - test/data_exp.txt
35
+ - test/test_interval.rb
34
36
  - test/data_log.txt
35
37
  - test/data_sinh.txt
36
38
  - test/data_tan.txt
37
39
  - test/data_atan.txt
40
+ - test/test_struct_float.rb
38
41
  - test/data_cos.txt
42
+ - test/test_fpu.rb
39
43
  - ext/jamis-mod.rb
40
44
  - ext/extconf.rb
41
45
  - ext/fpu.c
@@ -162,10 +166,11 @@ files:
162
166
  - lib/interval.rb
163
167
  - lib/struct_float.rb
164
168
  - lib/fpu.rb
169
+ - var/clean-nan.rb
165
170
  - README.txt
166
171
  - VERSION.txt
167
172
  test_files:
168
- - lib/interval.rb
173
+ - test/runall.rb
169
174
  rdoc_options:
170
175
  - --inline-source
171
176
  - --template