nio 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +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
88
+
@@ -0,0 +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
@@ -0,0 +1,40 @@
1
+
2
+ #require File.dirname(__FILE__) + '/test_helper.rb'
3
+ require 'test/unit'
4
+
5
+ require 'nio/repdec'
6
+ include Nio
7
+ require 'yaml'
8
+
9
+
10
+ class SEclass
11
+ include StateEquivalent
12
+ def initialize(a,b)
13
+ @a = a
14
+ @b = b
15
+ end
16
+ end
17
+
18
+
19
+ class TestTools < Test::Unit::TestCase
20
+
21
+ def setup
22
+
23
+ end
24
+
25
+
26
+ def test_StateEquivalent
27
+ x = SEclass.new(11,22)
28
+ y = SEclass.new(11,22)
29
+ z = SEclass.new(11,23)
30
+ xx = x
31
+ assert_equal(true,x==xx)
32
+ assert_equal(true,x==y)
33
+ assert_equal(false,x==z)
34
+ assert_equal(x.hash,xx.hash)
35
+ assert_equal(x.hash,y.hash)
36
+ assert_equal(false,x.hash==z.hash)
37
+ end
38
+
39
+ end
40
+
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: nio
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.2.0
7
+ date: 2007-09-23 00:00:00 +02:00
8
+ summary: Numeric input/output
9
+ require_paths:
10
+ - lib
11
+ email: javier@goizueta.info
12
+ homepage: http://nio.rubyforge.org
13
+ rubyforge_project: nio
14
+ description: Numeric input/output
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Javier Goizueta
31
+ files:
32
+ - History.txt
33
+ - License.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ - SOURCE.txt
37
+ - Rakefile
38
+ - config/hoe.rb
39
+ - config/requirements.rb
40
+ - lib/nio.rb
41
+ - lib/nio/version.rb
42
+ - lib/nio/repdec.rb
43
+ - lib/nio/flttol.rb
44
+ - lib/nio/tools.rb
45
+ - lib/nio/rtnlzr.rb
46
+ - lib/nio/fmt.rb
47
+ - lib/nio/sugar.rb
48
+ - log/debug.log
49
+ - script/destroy
50
+ - script/destroy.cmd
51
+ - script/generate
52
+ - script/generate.cmd
53
+ - script/txt2html
54
+ - script/txt2html.cmd
55
+ - setup.rb
56
+ - tasks/nuweb.rake
57
+ - tasks/deployment.rake
58
+ - tasks/environment.rake
59
+ - tasks/website.rake
60
+ - test/data.yaml
61
+ - test/test_helper.rb
62
+ - test/test_tools.rb
63
+ - test/test_repdec.rb
64
+ - test/test_rtnlzr.rb
65
+ - test/test_fmt.rb
66
+ test_files:
67
+ - test/test_fmt.rb
68
+ - test/test_helper.rb
69
+ - test/test_repdec.rb
70
+ - test/test_rtnlzr.rb
71
+ - test/test_tools.rb
72
+ rdoc_options:
73
+ - --main
74
+ - README.txt
75
+ extra_rdoc_files:
76
+ - History.txt
77
+ - License.txt
78
+ - Manifest.txt
79
+ - README.txt
80
+ - SOURCE.txt
81
+ executables: []
82
+
83
+ extensions: []
84
+
85
+ requirements: []
86
+
87
+ dependencies: []
88
+