nio 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,32 +1,32 @@
1
- require 'test/unit'
1
+ require 'test/unit'
2
2
  require File.dirname(__FILE__) + '/../lib/nio'
3
- require 'yaml'
4
-
5
- module PrepareData
6
-
7
- @@data = []
8
-
9
- def self.add(x)
10
- @@data << [x].pack('E').unpack('H*')[0].upcase
11
- end
12
-
13
- def self.init
14
- unless File.exists?('test/data.yaml')
15
- 100.times do
16
- x = rand
17
- x *= rand(1000) if rand<0.5
18
- x /= rand(1000) if rand<0.5
19
- x *= rand(9999) if rand<0.5
20
- x /= rand(9999) if rand<0.5
21
- x = -x if rand<0.5
22
- #puts x
23
- add x
24
- end
25
- add 1.0/3
26
- add 0.1
27
- File.open('test/data.yaml','w') { |out| out << @@data.to_yaml }
28
- end
29
- end
30
- end
31
-
32
- PrepareData.init
3
+ require 'yaml'
4
+
5
+ module PrepareData
6
+
7
+ @@data = []
8
+
9
+ def self.add(x)
10
+ @@data << [x].pack('E').unpack('H*')[0].upcase
11
+ end
12
+
13
+ def self.init
14
+ unless File.exist?('test/data.yaml')
15
+ 100.times do
16
+ x = rand
17
+ x *= rand(1000) if rand<0.5
18
+ x /= rand(1000) if rand<0.5
19
+ x *= rand(9999) if rand<0.5
20
+ x /= rand(9999) if rand<0.5
21
+ x = -x if rand<0.5
22
+ #puts x
23
+ add x
24
+ end
25
+ add 1.0/3
26
+ add 0.1
27
+ File.open('test/data.yaml','w') { |out| out << @@data.to_yaml }
28
+ end
29
+ end
30
+ end
31
+
32
+ PrepareData.init
@@ -1,88 +1,88 @@
1
-
2
-
3
- # Copyright (C) 2003-2005, Javier Goizueta <javier@goizueta.info>
4
- #
5
- # This program is free software; you can redistribute it and/or
6
- # modify it under the terms of the GNU General Public License
7
- # as published by the Free Software Foundation; either version 2
8
- # of the License, or (at your option) any later version.
9
-
10
- #require File.dirname(__FILE__) + '/test_helper.rb'
11
- require 'test/unit'
12
-
13
- require 'nio/repdec'
14
- include Nio
15
- require 'yaml'
16
-
17
- class TestRepdec < Test::Unit::TestCase
18
-
19
- def setup
20
-
21
- end
22
-
23
-
24
- def test_basic_repdec
25
- r = RepDec.new
26
- assert_equal "2.<3>", r.setQ(7,3).getS(0)
27
- assert_equal [7, 3], r.setQ(7,3).getQ
28
-
29
- assert_equal "1.<3>", r.setS("1.<3>").getS(0)
30
- assert_equal [4, 3], r.setS("1.<3>").getQ
31
-
32
- assert_equal "254.34212<678>", r.setS("254.34212<678>").getS(0)
33
- assert_equal [4234796411, 16650000], r.setS("254.34212<678>").getQ
34
-
35
- assert_equal "254.34212<678>", r.setS("254.34212678678...").getS(0)
36
- assert_equal [4234796411, 16650000], r.setS("254.34212678678...").getQ
37
-
38
- assert_equal "254.34212<678>", r.setS("254.34212678678678678...").getS(0)
39
- assert_equal [4234796411, 16650000], r.setS("254.34212678678678678...").getQ
40
-
41
- assert_equal "0.<3>", r.setS("0.3333333...").getS(0)
42
- assert_equal [1, 3], r.setS("0.3333333...").getQ
43
-
44
- assert_equal "-7.2<14>", r.setS("-7.2141414...").getS(0)
45
- assert_equal [-3571, 495], r.setS("-7.2141414...").getQ
46
-
47
- assert_equal "-7.21414...", r.setS("-7.2141414...").getS(1)
48
- assert_equal [-3571, 495], r.setS("-7.2141414...").getQ
49
-
50
- assert_equal "1.<234545>", r.setS("1.234545234545...").getS(0)
51
- assert_equal [1234544, 999999], r.setS("1.234545234545...").getQ
52
-
53
- assert_equal "1.234545234545...", r.setS("1.234545234545...").getS(1)
54
- assert_equal [1234544, 999999], r.setS("1.234545234545...").getQ
55
-
56
- assert_equal "1.23454523<45>", r.setS("1.23454523454545...").getS(0)
57
- assert_equal [678999879, 550000000], r.setS("1.23454523454545...").getQ
58
-
59
- assert_equal "1.23454523454545...", r.setS("1.23454523454545...").getS(1)
60
- assert_equal [678999879, 550000000], r.setS("1.23454523454545...").getQ
61
-
62
- assert_equal "0.<9>", r.setS(".<9>").getS(0)
63
- assert_equal [1, 1], r.setS(".<9>").getQ
64
-
65
- assert_equal "0.1<9>", r.setS("0.1999999...",RepDec::DEF_OPT.dup.set_digits(DigitsDef.base(16))).getS(0)
66
- assert_equal [1, 10], r.setS("0.1999999...",RepDec::DEF_OPT.dup.set_digits(DigitsDef.base(16))).getQ
67
-
68
- assert_equal "Infinity", r.setQ(10,0).getS(0)
69
- assert_equal [1, 0], r.setQ(10,0).getQ
70
-
71
- assert_equal "-Infinity", r.setQ(-10,0).getS(0)
72
- assert_equal [-1, 0], r.setQ(-10,0).getQ
73
-
74
- assert_equal "NaN", r.setQ(0,0).getS(0)
75
- assert_equal [0, 0], r.setQ(0,0).getQ
76
-
77
- assert_equal "NaN", r.setS("NaN").getS(0)
78
- assert_equal [0, 0], r.setS("NaN").getQ
79
-
80
- assert_equal "Infinity", r.setS("Infinity").getS(0)
81
- assert_equal [1, 0], r.setS("Infinity").getQ
82
-
83
- assert_equal "-Infinity", r.setS("-Infinity").getS(0)
84
- assert_equal [-1, 0], r.setS("-Infinity").getQ
85
- end
86
-
87
- end
1
+
2
+
3
+ # Copyright (C) 2003-2005, Javier Goizueta <javier@goizueta.info>
4
+ #
5
+ # This program is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU General Public License
7
+ # as published by the Free Software Foundation; either version 2
8
+ # of the License, or (at your option) any later version.
9
+
10
+ #require File.dirname(__FILE__) + '/test_helper.rb'
11
+ require 'test/unit'
12
+
13
+ require 'nio/repdec'
14
+ include Nio
15
+ require 'yaml'
16
+
17
+ class TestRepdec < Test::Unit::TestCase
18
+
19
+ def setup
20
+
21
+ end
22
+
23
+
24
+ def test_basic_repdec
25
+ r = RepDec.new
26
+ assert_equal "2.<3>", r.setQ(7,3).getS(0)
27
+ assert_equal [7, 3], r.setQ(7,3).getQ
28
+
29
+ assert_equal "1.<3>", r.setS("1.<3>").getS(0)
30
+ assert_equal [4, 3], r.setS("1.<3>").getQ
31
+
32
+ assert_equal "254.34212<678>", r.setS("254.34212<678>").getS(0)
33
+ assert_equal [4234796411, 16650000], r.setS("254.34212<678>").getQ
34
+
35
+ assert_equal "254.34212<678>", r.setS("254.34212678678...").getS(0)
36
+ assert_equal [4234796411, 16650000], r.setS("254.34212678678...").getQ
37
+
38
+ assert_equal "254.34212<678>", r.setS("254.34212678678678678...").getS(0)
39
+ assert_equal [4234796411, 16650000], r.setS("254.34212678678678678...").getQ
40
+
41
+ assert_equal "0.<3>", r.setS("0.3333333...").getS(0)
42
+ assert_equal [1, 3], r.setS("0.3333333...").getQ
43
+
44
+ assert_equal "-7.2<14>", r.setS("-7.2141414...").getS(0)
45
+ assert_equal [-3571, 495], r.setS("-7.2141414...").getQ
46
+
47
+ assert_equal "-7.21414...", r.setS("-7.2141414...").getS(1)
48
+ assert_equal [-3571, 495], r.setS("-7.2141414...").getQ
49
+
50
+ assert_equal "1.<234545>", r.setS("1.234545234545...").getS(0)
51
+ assert_equal [1234544, 999999], r.setS("1.234545234545...").getQ
52
+
53
+ assert_equal "1.234545234545...", r.setS("1.234545234545...").getS(1)
54
+ assert_equal [1234544, 999999], r.setS("1.234545234545...").getQ
55
+
56
+ assert_equal "1.23454523<45>", r.setS("1.23454523454545...").getS(0)
57
+ assert_equal [678999879, 550000000], r.setS("1.23454523454545...").getQ
58
+
59
+ assert_equal "1.23454523454545...", r.setS("1.23454523454545...").getS(1)
60
+ assert_equal [678999879, 550000000], r.setS("1.23454523454545...").getQ
61
+
62
+ assert_equal "0.<9>", r.setS(".<9>").getS(0)
63
+ assert_equal [1, 1], r.setS(".<9>").getQ
64
+
65
+ assert_equal "0.1<9>", r.setS("0.1999999...",RepDec::DEF_OPT.dup.set_digits(DigitsDef.base(16))).getS(0)
66
+ assert_equal [1, 10], r.setS("0.1999999...",RepDec::DEF_OPT.dup.set_digits(DigitsDef.base(16))).getQ
67
+
68
+ assert_equal "Infinity", r.setQ(10,0).getS(0)
69
+ assert_equal [1, 0], r.setQ(10,0).getQ
70
+
71
+ assert_equal "-Infinity", r.setQ(-10,0).getS(0)
72
+ assert_equal [-1, 0], r.setQ(-10,0).getQ
73
+
74
+ assert_equal "NaN", r.setQ(0,0).getS(0)
75
+ assert_equal [0, 0], r.setQ(0,0).getQ
76
+
77
+ assert_equal "NaN", r.setS("NaN").getS(0)
78
+ assert_equal [0, 0], r.setS("NaN").getQ
79
+
80
+ assert_equal "Infinity", r.setS("Infinity").getS(0)
81
+ assert_equal [1, 0], r.setS("Infinity").getQ
82
+
83
+ assert_equal "-Infinity", r.setS("-Infinity").getS(0)
84
+ assert_equal [-1, 0], r.setS("-Infinity").getQ
85
+ end
86
+
87
+ end
88
88
 
@@ -1,125 +1,125 @@
1
-
2
- # Copyright (C) 2003-2005, Javier Goizueta <javier@goizueta.info>
3
- #
4
- # This program is free software; you can redistribute it and/or
5
- # modify it under the terms of the GNU General Public License
6
- # as published by the Free Software Foundation; either version 2
7
- # of the License, or (at your option) any later version.
8
- require 'test/unit'
9
-
10
- require 'nio/rtnlzr'
11
- require 'nio/sugar'
12
- include Nio
13
- require 'yaml'
14
- require 'bigdecimal/math'
15
-
16
- class TestRtnlzr < Test::Unit::TestCase
17
-
18
- class BgMth
19
- extend BigMath
20
- end
21
-
22
- def setup
23
-
24
- $data = YAML.load(File.read(File.join(File.dirname(__FILE__) ,'data.yaml'))).collect{|x| [x].pack('H*').unpack('E')[0]}
25
-
26
- end
27
-
28
-
29
- def test_basic_rtnlzr
30
- # basic Rtnlzr tests
31
- r = Rtnlzr.new
32
- assert_equal [13,10], r.rationalize(1.3)
33
- assert_equal [13,10], Rtnlzr.max_denominator(1.3,10)
34
- assert_equal [13,10], Rtnlzr.max_denominator(BigDecimal('1.3'),10)
35
- assert_equal [1,3], Rtnlzr.max_denominator(1.0/3,10)
36
- assert_equal [1,3], Rtnlzr.max_denominator(BigDecimal('1')/3,10)
37
-
38
- # basic tests of Float#nio_r
39
- assert_equal Rational(1,3), (1.0/3.0).nio_r
40
- assert_equal Rational(2,3), (2.0/3.0).nio_r
41
- assert_equal Rational(1237,1234), (1237.0/1234.0).nio_r
42
- assert_equal Rational(89,217), (89.0/217.0).nio_r
43
-
44
- # rationalization of Floats using a tolerance
45
- t = Tolerance.new(1e-15,:sig)
46
- assert_equal Rational(540429, 12500),43.23432.nio_r(t)
47
- assert_equal Rational(6636649, 206596193),0.032123772.nio_r(t)
48
- assert_equal Rational(280943, 2500000), 0.1123772.nio_r(t)
49
- assert_equal Rational(39152929, 12500), 3132.23432.nio_r(t)
50
- assert_equal Rational(24166771439, 104063), 232232.123223432.nio_r(t)
51
- assert_equal Rational(792766404965, 637), 1244531247.98273123.nio_r(t)
52
- #$data.each do |x|
53
- # assert t.equals?(x, x.nio_r(t).to_f), "out of tolerance: #{x.inspect} #{x.nio_r(t).inspect}"
54
- #end
55
-
56
- # rationalization with maximum denominator
57
- assert_equal Rational(9441014047197, 7586), (1244531247.98273123.nio_r(10000))
58
- assert_equal Rational(11747130449709, 9439), BigDecimal('1244531247.982731230').nio_r(10000)
59
-
60
-
61
- # approximate a value in [0.671,0.672];
62
- # Float
63
- assert_equal [43,64], Rtnlzr.new(Tolerance.new(0.0005)).rationalize(0.6715)
64
- assert_equal [43,64], Rtnlzr.new(Tol(0.0005)).rationalize(0.6715)
65
- assert_equal [43,64], Rtnlzr.new(Rational(5,10000)).rationalize(0.6715)
66
- # BigDecimal
67
- assert_equal [43,64], Rtnlzr.new(BigTolerance.new(BigDecimal('0.0005'))).rationalize(BigDecimal('0.6715'))
68
- assert_equal [43,64], Rtnlzr.new(Tol(BigDecimal('0.0005'))).rationalize(BigDecimal('0.6715'))
69
- assert_equal [43,64], Rtnlzr.new(Rational(5,10000)).rationalize(BigDecimal('0.6715'))
70
- #
71
- assert_equal Rational(43,64), 0.6715.nio_r(0.0005)
72
- assert_equal Rational(43,64), 0.6715.nio_r(Rational(5,10000))
73
- assert_equal Rational(47,70), 0.6715.nio_r(70)
74
- assert_equal Rational(45,67), 0.6715.nio_r(69)
75
- assert_equal Rational(2,3), 0.6715.nio_r(10)
76
-
77
- # some PI tests
78
- assert_equal Rational(899125804609,286200632530), BgMth.PI(64).nio_r(BigTolerance.new(BigDec('261E-24')))
79
- assert_equal Rational(899125804609,286200632530), BgMth.PI(64).nio_r(Tol(BigDec('261E-24')))
80
- assert_equal Rational(899125804609,286200632530), BgMth.PI(64).nio_r(BigDec('261E-24'))
81
- assert_equal Rational(899125804609,286200632530), BgMth.PI(64).nio_r(BigDec(261E-24))
82
- assert_equal Rational(899125804609,286200632530), BgMth.PI(64).nio_r(261E-24)
83
-
84
- # BigDecimal tests
85
- #t = BigTolerance.new(BigDecimal('1e-15'),:sig)
86
- t = BigTolerance.decimals(20,:sig)
87
- $data.each do |x|
88
- x = BigDec(x,:exact)
89
- q = x.nio_r(t)
90
- assert t.equals?(x, BigDec(q)), "out of tolerance: #{x.inspect} #{BigDec(q)}"
91
- end
92
- end
93
-
94
- def test_compare_algorithms
95
- r = Rtnlzr.new(Tolerance.new(1e-5,:sig))
96
- ($data + $data.collect{|x| -x}).each do |x|
97
- q1 = r.rationalize_Knuth(x)
98
- q2 = r.rationalize_Horn(x)
99
- q3 = r.rationalize_HornHutchins(x)
100
- #q4 = r.rationalize_KnuthB(x)
101
- q1 = [-q1[0],-q1[1]] if q1[1] < 0
102
- q2 = [-q2[0],-q2[1]] if q2[1] < 0
103
- q3 = [-q3[0],-q3[1]] if q3[1] < 0
104
- assert_equal q1, q2
105
- assert_equal q1, q3
106
- #assert_equal q1, q4
107
- end
108
- r = Rtnlzr.new(Tolerance.epsilon)
109
- ($data + $data.collect{|x| -x}).each do |x|
110
- q1 = r.rationalize_Knuth(x)
111
- q2 = r.rationalize_Horn(x)
112
- q3 = r.rationalize_HornHutchins(x)
113
- q1 = [-q1[0],-q1[1]] if q1[1] < 0
114
- q2 = [-q2[0],-q2[1]] if q2[1] < 0
115
- q3 = [-q3[0],-q3[1]] if q3[1] < 0
116
- #q4 = r.rationalize_KnuthB(x)
117
- assert_equal q1, q2
118
- assert_equal q1, q3
119
- #assert_equal q1, q4
120
- end
121
-
122
- end
123
-
124
-
125
- end
1
+
2
+ # Copyright (C) 2003-2005, Javier Goizueta <javier@goizueta.info>
3
+ #
4
+ # This program is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU General Public License
6
+ # as published by the Free Software Foundation; either version 2
7
+ # of the License, or (at your option) any later version.
8
+ require 'test/unit'
9
+
10
+ require 'nio/rtnlzr'
11
+ require 'nio/sugar'
12
+ include Nio
13
+ require 'yaml'
14
+ require 'bigdecimal/math'
15
+
16
+ class TestRtnlzr < Test::Unit::TestCase
17
+
18
+ class BgMth
19
+ extend BigMath
20
+ end
21
+
22
+ def setup
23
+
24
+ $data = YAML.load(File.read(File.join(File.dirname(__FILE__) ,'data.yaml'))).collect{|x| [x].pack('H*').unpack('E')[0]}
25
+
26
+ end
27
+
28
+
29
+ def test_basic_rtnlzr
30
+ # basic Rtnlzr tests
31
+ r = Rtnlzr.new
32
+ assert_equal [13,10], r.rationalize(1.3)
33
+ assert_equal [13,10], Rtnlzr.max_denominator(1.3,10)
34
+ assert_equal [13,10], Rtnlzr.max_denominator(BigDecimal('1.3'),10)
35
+ assert_equal [1,3], Rtnlzr.max_denominator(1.0/3,10)
36
+ assert_equal [1,3], Rtnlzr.max_denominator(BigDecimal('1')/3,10)
37
+
38
+ # basic tests of Float#nio_r
39
+ assert_equal Rational(1,3), (1.0/3.0).nio_r
40
+ assert_equal Rational(2,3), (2.0/3.0).nio_r
41
+ assert_equal Rational(1237,1234), (1237.0/1234.0).nio_r
42
+ assert_equal Rational(89,217), (89.0/217.0).nio_r
43
+
44
+ # rationalization of Floats using a tolerance
45
+ t = Tolerance.new(1e-15,:sig)
46
+ assert_equal Rational(540429, 12500),43.23432.nio_r(t)
47
+ assert_equal Rational(6636649, 206596193),0.032123772.nio_r(t)
48
+ assert_equal Rational(280943, 2500000), 0.1123772.nio_r(t)
49
+ assert_equal Rational(39152929, 12500), 3132.23432.nio_r(t)
50
+ assert_equal Rational(24166771439, 104063), 232232.123223432.nio_r(t)
51
+ assert_equal Rational(792766404965, 637), 1244531247.98273123.nio_r(t)
52
+ #$data.each do |x|
53
+ # assert t.equals?(x, x.nio_r(t).to_f), "out of tolerance: #{x.inspect} #{x.nio_r(t).inspect}"
54
+ #end
55
+
56
+ # rationalization with maximum denominator
57
+ assert_equal Rational(9441014047197, 7586), (1244531247.98273123.nio_r(10000))
58
+ assert_equal Rational(11747130449709, 9439), BigDecimal('1244531247.982731230').nio_r(10000)
59
+
60
+
61
+ # approximate a value in [0.671,0.672];
62
+ # Float
63
+ assert_equal [43,64], Rtnlzr.new(Tolerance.new(0.0005)).rationalize(0.6715)
64
+ assert_equal [43,64], Rtnlzr.new(Tol(0.0005)).rationalize(0.6715)
65
+ assert_equal [43,64], Rtnlzr.new(Rational(5,10000)).rationalize(0.6715)
66
+ # BigDecimal
67
+ assert_equal [43,64], Rtnlzr.new(BigTolerance.new(BigDecimal('0.0005'))).rationalize(BigDecimal('0.6715'))
68
+ assert_equal [43,64], Rtnlzr.new(Tol(BigDecimal('0.0005'))).rationalize(BigDecimal('0.6715'))
69
+ assert_equal [43,64], Rtnlzr.new(Rational(5,10000)).rationalize(BigDecimal('0.6715'))
70
+ #
71
+ assert_equal Rational(43,64), 0.6715.nio_r(0.0005)
72
+ assert_equal Rational(43,64), 0.6715.nio_r(Rational(5,10000))
73
+ assert_equal Rational(47,70), 0.6715.nio_r(70)
74
+ assert_equal Rational(45,67), 0.6715.nio_r(69)
75
+ assert_equal Rational(2,3), 0.6715.nio_r(10)
76
+
77
+ # some PI tests
78
+ assert_equal Rational(899125804609,286200632530), BgMth.PI(64).nio_r(BigTolerance.new(BigDec('261E-24')))
79
+ assert_equal Rational(899125804609,286200632530), BgMth.PI(64).nio_r(Tol(BigDec('261E-24')))
80
+ assert_equal Rational(899125804609,286200632530), BgMth.PI(64).nio_r(BigDec('261E-24'))
81
+ assert_equal Rational(899125804609,286200632530), BgMth.PI(64).nio_r(BigDec(261E-24))
82
+ assert_equal Rational(899125804609,286200632530), BgMth.PI(64).nio_r(261E-24)
83
+
84
+ # BigDecimal tests
85
+ #t = BigTolerance.new(BigDecimal('1e-15'),:sig)
86
+ t = BigTolerance.decimals(20,:sig)
87
+ $data.each do |x|
88
+ x = BigDec(x,:exact)
89
+ q = x.nio_r(t)
90
+ assert t.equals?(x, BigDec(q)), "out of tolerance: #{x.inspect} #{BigDec(q)}"
91
+ end
92
+ end
93
+
94
+ def test_compare_algorithms
95
+ r = Rtnlzr.new(Tolerance.new(1e-5,:sig))
96
+ ($data + $data.collect{|x| -x}).each do |x|
97
+ q1 = r.rationalize_Knuth(x)
98
+ q2 = r.rationalize_Horn(x)
99
+ q3 = r.rationalize_HornHutchins(x)
100
+ #q4 = r.rationalize_KnuthB(x)
101
+ q1 = [-q1[0],-q1[1]] if q1[1] < 0
102
+ q2 = [-q2[0],-q2[1]] if q2[1] < 0
103
+ q3 = [-q3[0],-q3[1]] if q3[1] < 0
104
+ assert_equal q1, q2
105
+ assert_equal q1, q3
106
+ #assert_equal q1, q4
107
+ end
108
+ r = Rtnlzr.new(Tolerance.epsilon)
109
+ ($data + $data.collect{|x| -x}).each do |x|
110
+ q1 = r.rationalize_Knuth(x)
111
+ q2 = r.rationalize_Horn(x)
112
+ q3 = r.rationalize_HornHutchins(x)
113
+ q1 = [-q1[0],-q1[1]] if q1[1] < 0
114
+ q2 = [-q2[0],-q2[1]] if q2[1] < 0
115
+ q3 = [-q3[0],-q3[1]] if q3[1] < 0
116
+ #q4 = r.rationalize_KnuthB(x)
117
+ assert_equal q1, q2
118
+ assert_equal q1, q3
119
+ #assert_equal q1, q4
120
+ end
121
+
122
+ end
123
+
124
+
125
+ end