nio 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ desc 'Release the website and new gem version'
2
+ task :deploy => [:check_version, :website, :release] do
3
+ puts "Remember to create SVN tag:"
4
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
5
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
6
+ puts "Suggested comment:"
7
+ puts "Tagging release #{CHANGES}"
8
+ end
9
+
10
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
11
+ task :local_deploy => [:website_generate, :install_gem]
12
+
13
+ task :check_version do
14
+ unless ENV['VERSION']
15
+ puts 'Must pass a VERSION=x.y.z release version'
16
+ exit
17
+ end
18
+ unless ENV['VERSION'] == VERS
19
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
20
+ exit
21
+ end
22
+ end
23
+
24
+ desc 'Install the package as a gem, without generating documentation(ri/rdoc)'
25
+ task :install_gem_no_doc => [:clean, :package] do
26
+ sh "#{'sudo ' unless Hoe::WINDOZE }gem install pkg/*.gem --no-rdoc --no-ri"
27
+ end
@@ -0,0 +1,7 @@
1
+ task :ruby_env do
2
+ RUBY_APP = if RUBY_PLATFORM =~ /java/
3
+ "jruby"
4
+ else
5
+ "ruby"
6
+ end unless defined? RUBY_APP
7
+ end
data/tasks/nuweb.rake ADDED
@@ -0,0 +1,69 @@
1
+ # nuweb build tasks
2
+
3
+ desc "Generate Ruby code"
4
+ task :tangle => Dir['source/*.w'].collect{|fn| fn.gsub /\.w/,'.ws'}+Dir['source/lib/**/*.rb'].collect{|fn| fn.gsub('source/lib/','lib/')}+[:test]
5
+
6
+ # directory 'lib'
7
+ # directory 'lib/nio'
8
+ # directory 'source/pdf'
9
+
10
+ rule '.ws' => ['.w'] do |t|
11
+ puts "build dir: #{Dir.pwd}"
12
+ puts "nuweb -t #{t.source}"
13
+ puts `nuweb -t #{t.source}`
14
+ File.open(t.name,'w'){|f| f.puts "sentinel"}
15
+ end
16
+
17
+ clean_exts = ['*.tex','*.dvi','*.log','*.aux','*.out','*.ws']
18
+
19
+ desc "clean up files"
20
+ task :clean_nuweb do |t| # to do: integrate in hoe clean
21
+ rm_r clean_exts.collect{|x| Dir.glob('*'+x)+Dir.glob('source/*'+x)+Dir.glob('source/pdf/*'+x)}.flatten
22
+ end
23
+
24
+ desc "Generate source code (nuweb) documentation"
25
+ task :weave => ['source/pdf'] + Dir['source/*.w'].collect{|fn| fn.gsub(/\.w/,'.pdf').gsub('source/','source/pdf/')}
26
+
27
+ def rem_ext(fn, ext)
28
+ ext = File.extname(fn) unless fn
29
+ File.join(File.dirname(fn),File.basename(fn,ext))
30
+ end
31
+
32
+ def sub_dir(dir, fn)
33
+ d,n = File.split(fn)
34
+ File.join(d,File.join(dir,n))
35
+ end
36
+
37
+ def rep_dir(dir, fn)
38
+ File.join(dir, File.basename(fn))
39
+ end
40
+
41
+ #note that if latex is run from the base directory and the file is in a subdirectory (source)
42
+ # .aux/.out/.log files are created in the subdirectory and won't be found by the second
43
+ # pass of latex;
44
+ def w_to_pdf(s)
45
+ fn = rem_ext(s,'.w')
46
+ puts "dir: #{File.dirname(fn)}"
47
+ doc_dir = File.dirname(fn)!='.' ? './pdf' : '../source/pdf'
48
+ cd(File.dirname(fn)) do
49
+ fn = File.basename(fn)
50
+ 2.times do
51
+ puts "nuweb -o -l #{fn}.w"
52
+ puts `nuweb -o -l #{fn}.w`
53
+ puts "latex -halt-on-error #{fn}.tex"
54
+ puts `latex -halt-on-error #{fn}.tex`
55
+ puts "dvipdfm -o #{rep_dir(doc_dir,fn)}.pdf #{fn}.dvi"
56
+ puts `dvipdfm -o #{rep_dir(doc_dir,fn)}.pdf #{fn}.dvi`
57
+ end
58
+ end
59
+ end
60
+
61
+ rule '.pdf' => [proc{|tn| File.join('source',File.basename(tn,'.pdf')+'.w')}] do |t|
62
+ w_to_pdf t.source
63
+ end
64
+
65
+ rule /\Alib\/.*\.rb/ =>[proc{|tn| tn.sub(/\Alib\//, 'source/lib/') }] do |t|
66
+ cp t.source, t.name if t.source
67
+ end
68
+
69
+
@@ -0,0 +1,17 @@
1
+ desc 'Generate website files'
2
+ task :website_generate => :ruby_env do
3
+ (Dir['website/**/*.txt'] - Dir['website/version*.txt']).each do |txt|
4
+ sh %{ #{RUBY_APP} script/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
5
+ end
6
+ end
7
+
8
+ desc 'Upload website files to rubyforge'
9
+ task :website_upload do
10
+ host = "#{rubyforge_username}@rubyforge.org"
11
+ remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/"
12
+ local_dir = 'website'
13
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
14
+ end
15
+
16
+ desc 'Generate and upload website files'
17
+ task :website => [:website_generate, :website_upload, :publish_docs]
data/test/data.yaml ADDED
@@ -0,0 +1,101 @@
1
+ ---
2
+ - A91C2BB757F72141
3
+ - D84014519CD8253F
4
+ - BF30A46FFF51ED3F
5
+ - 91D03B54623CC03E
6
+ - D403F243699B4B3F
7
+ - 6D1F99449A26D540
8
+ - 92817CDBC7715E3F
9
+ - 72696E8F13BB323F
10
+ - 683C16C10F79FE3E
11
+ - 22801BA12A074740
12
+ - 7081D3172A53E5BF
13
+ - C0DE790FC0FBE9BF
14
+ - 3AE5F0923E55D33F
15
+ - 92B6BEEC140A8FBF
16
+ - CE66ABACDBD80F3F
17
+ - ACC70B95DCFF6BC0
18
+ - 1461EF81707378BF
19
+ - AE09EC08D0F1ADBF
20
+ - 34DA3FFF72E9D83F
21
+ - C664AD3F9AC4BFBF
22
+ - 2101BE78DFDC74BE
23
+ - 7EE9B042D4628140
24
+ - 90368189CC8D753F
25
+ - 337C9D874DC4C5BE
26
+ - 0CFDA79A7FDCED3F
27
+ - EC94C739F2CFB3BF
28
+ - 1D5436848A1AEBBE
29
+ - B725B6C420034640
30
+ - 48C692FEAA59DEBF
31
+ - BC4CB816B0BAB5BF
32
+ - C2D835BC80B99A40
33
+ - 6A565C2E720847BF
34
+ - 050189AF84C84EC1
35
+ - 0060B28B63B5DC3F
36
+ - 18A387F2A64E5FC0
37
+ - FBC8B0D428E302C1
38
+ - A30DD43ED762B4C0
39
+ - 3D85522EC70AFDBE
40
+ - FCF62D2923F0583F
41
+ - DB588C2A9C26FBC0
42
+ - BA4E1863DBE6FCBE
43
+ - 5C34946E1A12D33F
44
+ - 90E63D7E81888740
45
+ - 82541C0DE9F1443F
46
+ - 9F2A7781EE4B683F
47
+ - 0082C4B6393EA7BF
48
+ - 768EDD6B97D19540
49
+ - 87BA76258BEBFBBE
50
+ - 7A6580A6639DD7BF
51
+ - 26DDD435F13B12BF
52
+ - 1AF857C69682DBBF
53
+ - E731C0DCDAAE51BF
54
+ - DB9DB0551A511F3F
55
+ - 04977B9C4C73D13F
56
+ - 44D5BB96C84587C0
57
+ - CE3DE8C2DF9942BF
58
+ - 23F12DE055F30C3F
59
+ - C10951998E1B7440
60
+ - 1CC3F51802F309C0
61
+ - FB97E7DB6B081DC1
62
+ - 5C811354280FEA3F
63
+ - A405ED0A749718BF
64
+ - 322939F71F7A51BF
65
+ - F3E06C6EEB2342BF
66
+ - 5A6D9DF3F879D3C0
67
+ - BC466E35C5AF9940
68
+ - EB4571E54EB7CD3F
69
+ - BBA9A8401CD4D13F
70
+ - 6A565DEA5E4F8A40
71
+ - 20586794310B93BF
72
+ - 80B8449C203B9BC0
73
+ - 0E9C461358F4AABF
74
+ - 469E0A1D42DEA73F
75
+ - 711DA636DD2C41BF
76
+ - 414A9E166BE2A0C0
77
+ - 407FF9F39253F2BE
78
+ - BAD158256942D4C0
79
+ - 2D4424FE12BE94BF
80
+ - 7F0F15B537E997C0
81
+ - 4ADF1BD93C66F1BF
82
+ - 2DAF002ADBC1E0BF
83
+ - 3739FF3C2BE601C0
84
+ - 38ACCEAE7B88083F
85
+ - 7C2D9D7FA150C23F
86
+ - B726087920E8EA3F
87
+ - C075B44F459250BE
88
+ - 9EDAB9C61F7D783F
89
+ - 2FE6362CACE5153F
90
+ - 98A24B93A2F4C33E
91
+ - 6FD39CEB5C7F15C0
92
+ - C5BE550840E4503F
93
+ - 214EA7E4F9619640
94
+ - 4DEF9253DE1C10C0
95
+ - B8C76C5B85E4FDBF
96
+ - 07D21EB7A400F13F
97
+ - DB103669202EAEBF
98
+ - 9F59E7B3EA71E63F
99
+ - 83712959474BF440
100
+ - 8D5B7FA245A650BF
101
+ - 16FAF25CCBB8ECBE
data/test/test_fmt.rb ADDED
@@ -0,0 +1,373 @@
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
+ require 'nio/rtnlzr'
13
+ require 'nio/repdec'
14
+ require 'nio/fmt'
15
+ include Nio
16
+ require 'yaml'
17
+
18
+ def neighbours(x)
19
+ f,e = Math.frexp(x)
20
+ e = Float::MIN_EXP if f==0
21
+ e = [Float::MIN_EXP,e].max
22
+ dx = Math.ldexp(1,e-Float::MANT_DIG) #Math.ldexp(Math.ldexp(1.0,-Float::MANT_DIG),e)
23
+ high = x + dx
24
+ if e==Float::MIN_EXP || f!=0.5 #0.5==Math.ldexp(2**(bits-1),-Float::MANT_DIG)
25
+ low = x - dx
26
+ else
27
+ low = x - dx/2 # x - Math.ldexp(Math.ldexp(1.0,-Float::MANT_DIG),e-1)
28
+ end
29
+ [low, high]
30
+ end
31
+
32
+ def prv(x)
33
+ neighbours(x)[0]
34
+ end
35
+ def nxt(x)
36
+ neighbours(x)[1]
37
+ end
38
+ MIN_N = Math.ldexp(0.5,Float::MIN_EXP) # == nxt(MAX_D) == Float::MIN
39
+ MAX_D = Math.ldexp(Math.ldexp(1,Float::MANT_DIG-1)-1,Float::MIN_EXP-Float::MANT_DIG)
40
+ MIN_D = Math.ldexp(1,Float::MIN_EXP-Float::MANT_DIG);
41
+
42
+ class TestFmt < Test::Unit::TestCase
43
+
44
+ def setup
45
+
46
+ $data = YAML.load(File.read(File.join(File.dirname(__FILE__) ,'data.yaml'))).collect{|x| [x].pack('H*').unpack('E')[0]}
47
+ $data << MIN_N
48
+ $data << MAX_D
49
+ $data << MIN_D
50
+
51
+ end
52
+
53
+ def teardown
54
+
55
+ Fmt.default = Fmt.new
56
+
57
+ end
58
+
59
+
60
+ def test_basic_fmt
61
+ # test correct rounding: 1.448997445238699 -> 6525704354437805*2^-52
62
+ assert_equal Rational(6525704354437805,4503599627370496), Float.nio_read('1.448997445238699').nio_xr
63
+
64
+ assert_equal "0",0.0.nio_write
65
+ assert_equal "0",0.nio_write
66
+ assert_equal "0",BigDecimal('0').nio_write
67
+ assert_equal "0",Rational(0,1).nio_write
68
+
69
+ assert_equal "123456789",123456789.0.nio_write
70
+ assert_equal "123456789",123456789.nio_write
71
+ assert_equal "123456789",BigDecimal('123456789').nio_write
72
+ assert_equal "123456789",Rational(123456789,1).nio_write
73
+ assert_equal "123456789.25",123456789.25.nio_write
74
+ assert_equal "123456789.25",BigDecimal('123456789.25').nio_write
75
+ assert_equal "123456789.25",(Rational(123456789)+Rational(1,4)).nio_write
76
+ end
77
+
78
+ def test_basic_fmt_float
79
+
80
+ assert_equal 2,Float::RADIX
81
+ assert_equal 53,Float::MANT_DIG
82
+
83
+ fmt = Fmt.new {|f|
84
+ f.rep! '[','','...',0,true
85
+ f.width! 20,:right,'*'
86
+ }
87
+ fmt.sep! '.',',',[3]
88
+
89
+ assert_equal "0.1",0.1.nio_write
90
+ assert_equal "0.10000000000000001",0.1.nio_write(Fmt.mode(:gen,:exact).show_all_digits)
91
+ assert_equal "0.10000000000000001",0.1.nio_write(Fmt.mode(:gen,:exact).show_all_digits(true))
92
+ assert_equal "0.10000000000000001",0.1.nio_write(Fmt.mode(:gen,:exact,:show_all_digits=>true))
93
+ assert_equal "0.1000000000000000055511151231257827021181583404541015625",0.1.nio_write(Fmt.mode(:gen,:exact,:approx=>:exact))
94
+
95
+
96
+ assert_equal "******643,454,333.32",fmt.nio_write_formatted(fmt.nio_read_formatted("643,454,333.32"))
97
+ assert_equal "******643.454.333,32",fmt.sep(',').nio_write_formatted(fmt.nio_read_formatted("643,454,333.32"))
98
+ fmt.pad0s! 10
99
+ num = fmt.nio_read_formatted("0.3333...")
100
+ assert_equal "0000000.[3",fmt.nio_write_formatted(num)
101
+ fmt.mode! :fix, 3
102
+ assert_equal "000000.333",fmt.nio_write_formatted(num)
103
+ num = fmt.nio_read_formatted("-0.666...")
104
+ fmt.prec! :exact
105
+ fmt.sep! ',','.'
106
+ assert_equal "-000000,[6",fmt.nio_write_formatted(num)
107
+ fmt.width! 20,:center,'*'
108
+ fmt.mode! :fix,3
109
+ assert_equal "*******-0,667*******",fmt.nio_write_formatted(num)
110
+ num = fmt.nio_read_formatted("0,5555")
111
+ fmt.prec! :exact
112
+ assert_equal "*******0,5555*******",fmt.nio_write_formatted(num)
113
+
114
+ Fmt.default = Fmt[:comma_th]
115
+ x = Float.nio_read("11123,2343")
116
+ assert_equal 11123.2343,x
117
+ assert_equal "11.123,2343", x.nio_write
118
+ assert_equal "11123,2343", x.nio_write(Fmt[:comma])
119
+
120
+ x = Float.nio_read("-1234,5678901234e-33")
121
+ # assert_equal -1.2345678901234e-030, x
122
+ assert_equal "-1,2345678901234E-30", x.nio_write()
123
+ assert_equal "-0,0000000000000000000000000000012346",x.nio_write(Fmt[:comma].mode(:sig,5))
124
+
125
+ assert_equal "0,333...",
126
+ (1.0/3).nio_write(Fmt.prec(:exact).show_all_digits(true).approx_mode(:simplify))
127
+
128
+ fmt = Fmt.default
129
+ assert_raises TypeError do fmt.prec! 4 end
130
+ fmt = Fmt.default {|f| f.prec! 4 }
131
+ assert_equal "1,235", 1.23456.nio_write(fmt)
132
+ assert_equal "1,23456", 1.23456.nio_write()
133
+
134
+ Fmt.default = Fmt.new
135
+ assert_equal '11123.2343', 11123.2343.nio_write
136
+ end
137
+
138
+ def test_tol_fmt_float
139
+ tol = Tolerance.new.decimals(12,:sig)
140
+ fmt = Fmt.prec(12,:sig)
141
+ $data.each do |x|
142
+ assert tol.equals?(x, Float.nio_read(x.nio_write(fmt),fmt)), "out of tolerance: #{x.inspect} #{Float.nio_read(x.nio_write(fmt),fmt)}"
143
+ assert tol.equals?(-x, Float.nio_read((-x).nio_write(fmt),fmt)), "out of tolerance: #{(-x).inspect} #{Float.nio_read((-x).nio_write(fmt),fmt)}"
144
+ end
145
+ end
146
+
147
+ def test_BigDec
148
+ assert_equal "0",BigDec(0).nio_write
149
+ fmt = Fmt.mode(:gen,:exact)
150
+ assert_equal "0",BigDec(0).nio_write(fmt)
151
+ $data.each do |x|
152
+ x = BigDecimal(x.to_s)
153
+ assert_equal x,BigDecimal.nio_read(x.nio_write(fmt),fmt)
154
+ end
155
+
156
+ end
157
+
158
+ def test_Rational
159
+ assert_equal "0",Rational(0,1).nio_write
160
+ fmt = Fmt.mode(:gen,:exact)
161
+ assert_equal "0",Rational(0,1).nio_write(fmt)
162
+ $data.each do |x|
163
+ x = x.nio_xr # nio_r
164
+ assert_equal x,Rational.nio_read(x.nio_write(fmt),fmt)
165
+ end
166
+ end
167
+
168
+ def test_float_bases
169
+ nfmt2 = Fmt[:comma].base(2).prec(:exact)
170
+ nfmt8 = Fmt[:comma].base(8).prec(:exact)
171
+ nfmt10 = Fmt[:comma].base(10).prec(:exact)
172
+ nfmt16 = Fmt[:comma].base(16).prec(:exact)
173
+ $data.each do |x|
174
+ assert_equal(x,Float.nio_read(x.nio_write(nfmt2),nfmt2))
175
+ assert_equal(x,Float.nio_read(x.nio_write(nfmt8),nfmt8))
176
+ assert_equal(x,Float.nio_read(x.nio_write(nfmt10),nfmt10))
177
+ assert_equal(x,Float.nio_read(x.nio_write(nfmt16),nfmt16))
178
+ assert_equal(-x,Float.nio_read((-x).nio_write(nfmt2),nfmt2))
179
+ assert_equal(-x,Float.nio_read((-x).nio_write(nfmt8),nfmt8))
180
+ assert_equal(-x,Float.nio_read((-x).nio_write(nfmt10),nfmt10))
181
+ assert_equal(-x,Float.nio_read((-x).nio_write(nfmt16),nfmt16))
182
+ end
183
+ end
184
+
185
+ def rational_bases
186
+ assert_equal "0.0001100110011...", (Rational(1)/10).nio_write(Fmt.new.base(2))
187
+ end
188
+
189
+ def test_big_decimal_bases
190
+
191
+ assert_equal "0.1999A",(BigDec(1)/10).nio_write(Fmt.new.base(16).prec(5))
192
+ assert_equal "0.1999...",(BigDec(1)/10).nio_write(Fmt.mode(:gen,:exact,:round=>:inf,:approx=>:simplify).base(16))
193
+
194
+ nfmt2 = Fmt[:comma].base(2).prec(:exact)
195
+ nfmt8 = Fmt[:comma].base(8).prec(:exact)
196
+ nfmt10 = Fmt[:comma].base(10).prec(:exact)
197
+ nfmt16 = Fmt[:comma].base(16).prec(:exact)
198
+ $data.each do |x|
199
+ x = BigDec(x.to_s)
200
+ xs,xdig,xb,xe = x.split
201
+ ndig = xdig.size
202
+ round_dig = ndig-xe
203
+ # note that BigDecimal.nio_read produces a BigDecimal with the exact value of the text representation
204
+ # since the representation here is only aproximate (because of the base difference), we must
205
+ # round the results to the precision of the original number
206
+ assert_equal(x,BigDecimal.nio_read(x.nio_write(nfmt2),nfmt2).round(round_dig))
207
+ assert_equal(x,BigDecimal.nio_read(x.nio_write(nfmt8),nfmt8).round(round_dig))
208
+ assert_equal(x,BigDecimal.nio_read(x.nio_write(nfmt10),nfmt10).round(round_dig))
209
+ assert_equal(x,BigDecimal.nio_read(x.nio_write(nfmt16),nfmt16).round(round_dig))
210
+ assert_equal(-x,BigDecimal.nio_read((-x).nio_write(nfmt2),nfmt2).round(round_dig))
211
+ assert_equal(-x,BigDecimal.nio_read((-x).nio_write(nfmt8),nfmt8).round(round_dig))
212
+ assert_equal(-x,BigDecimal.nio_read((-x).nio_write(nfmt10),nfmt10).round(round_dig))
213
+ assert_equal(-x,BigDecimal.nio_read((-x).nio_write(nfmt16),nfmt16).round(round_dig))
214
+ end
215
+ end
216
+
217
+ def test_exact_all_float
218
+ #fmt = Fmt.prec(:exact).show_all_digits(true).approx_mode(:exact)
219
+ fmt = Fmt.mode(:gen,:exact,:round=>:inf,:approx=>:exact)
220
+ assert_equal "0.1000000000000000055511151231257827021181583404541015625",Float.nio_read('0.1',fmt).nio_write(fmt)
221
+ assert_equal "64.099999999999994315658113919198513031005859375",Float.nio_read('64.1',fmt).nio_write(fmt)
222
+ assert_equal '0.5',Float.nio_read('0.5',fmt).nio_write(fmt)
223
+ assert_equal "0.333333333333333314829616256247390992939472198486328125", (1.0/3.0).nio_write(fmt)
224
+ assert_equal "0.66666666666666662965923251249478198587894439697265625", (2.0/3.0).nio_write(fmt)
225
+ assert_equal "-0.333333333333333314829616256247390992939472198486328125", (-1.0/3.0).nio_write(fmt)
226
+ assert_equal "-0.66666666666666662965923251249478198587894439697265625", (-2.0/3.0).nio_write(fmt)
227
+ assert_equal "1267650600228229401496703205376", (2.0**100).nio_write(fmt)
228
+ assert_equal "0.10000000000000001942890293094023945741355419158935546875", nxt(0.1).nio_write(fmt)
229
+ assert_equal "1023.9999999999998863131622783839702606201171875", prv(1024).nio_write(fmt)
230
+
231
+ assert_equal "2.225073858507201383090232717332404064219215980462331830553327416887204434813918195854283159012511020564067339731035811005152434161553460108856012385377718821130777993532002330479610147442583636071921565046942503734208375250806650616658158948720491179968591639648500635908770118304874799780887753749949451580451605050915399856582470818645113537935804992115981085766051992433352114352390148795699609591288891602992641511063466313393663477586513029371762047325631781485664350872122828637642044846811407613911477062801689853244110024161447421618567166150540154285084716752901903161322778896729707373123334086988983175067838846926092773977972858659654941091369095406136467568702398678315290680984617210924625396728515625E-308",
232
+ MIN_N.nio_write(fmt)
233
+ assert_equal "2.2250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375E-308",
234
+ MAX_D.nio_write(fmt)
235
+ assert_equal "2.225073858507200394958941034839315711081630244019587100433722188237675583642553194503268618595007289964394616459051051412023043270117998255542591673498126023581185971968246077878183766819774580380287229348978296356771103136809189170558146173902184049999817014701706089569539838241444028984739501272818269238398287937541863482503350197395249647392622007205322474852963190178391854932391064931720791430455764953943127215325436859833344767109289929102154994338687742727610729450624487971196675896144263447425089844325111161570498002959146187656616550482084690619235135756396957006047593447154776156167693340095043268338435252390549256952840748419828640113148805198563919935252207510837343961185884248936392555587988206944151446491086954182492263498716056346893310546875E-308",
236
+ prv(MAX_D).nio_write(fmt)
237
+ assert_equal "9.88131291682493088353137585736442744730119605228649528851171365001351014540417503730599672723271984759593129390891435461853313420711879592797549592021563756252601426380622809055691634335697964207377437272113997461446100012774818307129968774624946794546339230280063430770796148252477131182342053317113373536374079120621249863890543182984910658610913088802254960259419999083863978818160833126649049514295738029453560318710477223100269607052986944038758053621421498340666445368950667144166486387218476578691673612021202301233961950615668455463665849580996504946155275185449574931216955640746893939906729403594535543517025132110239826300978220290207572547633450191167477946719798732961988232841140527418055848553508913045817507736501283943653106689453125E-324",
238
+ nxt(MIN_D).nio_write(fmt)
239
+ assert_equal "4.940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625E-324",
240
+ MIN_D.nio_write(fmt)
241
+
242
+ end
243
+
244
+ def test_float_nonsig
245
+
246
+ assert_equal "100.000000000000000#####", 100.0.nio_write(Fmt.prec(20,:fix).insignificant_digits('#'))
247
+
248
+ fmt = Fmt.mode(:sci,20).insignificant_digits('#').sci_digits(1)
249
+ assert_equal "3.3333333333333331###E-1", (1.0/3).nio_write(fmt)
250
+ assert_equal "3.3333333333333335###E6", (1E7/3).nio_write(fmt)
251
+ assert_equal "3.3333333333333334###E-8", (1E-7/3).nio_write(fmt)
252
+ assert_equal "3.3333333333333333333E-1", Rational(1,3).nio_write(fmt)
253
+ assert_equal "3.3333333333333331###E-1", (1.0/3).nio_write(fmt.dup.sci_digits(1))
254
+ assert_equal "33333333333333331###.E-20", (1.0/3).nio_write(fmt.dup.sci_digits(-1))
255
+ assert_equal "33333333333333333333.E-20", (Rational(1,3)).nio_write(fmt.dup.sci_digits(-1))
256
+
257
+ fmt.sci_digits! :eng
258
+ assert_equal "333.33333333333331###E-3", (1.0/3).nio_write(fmt)
259
+ assert_equal "3.3333333333333335###E6", (1E7/3).nio_write(fmt)
260
+ assert_equal "33.333333333333334###E-9",(1E-7/3).nio_write(fmt)
261
+
262
+ fmt = Fmt[:comma].mode(:sci,20).insignificant_digits('#').sci_digits(0)
263
+ assert_equal "0,33333333333333331###E0",(1.0/3).nio_write(fmt)
264
+ assert_equal "0,33333333333333335###E7",(1E7/3).nio_write(fmt)
265
+ assert_equal "0,33333333333333334###E-7",(1E-7/3).nio_write(fmt)
266
+
267
+ fmt = Fmt.mode(:sci,20).insignificant_digits('#').sci_digits(0)
268
+ assert_equal "0.10000000000000001###E0",(1E-1).nio_write(fmt)
269
+ assert_equal "0.50000000000000000###E0",(0.5).nio_write(fmt)
270
+ assert_equal "0.49999999999999994###E0",prv(0.5).nio_write(fmt)
271
+ assert_equal "0.50000000000000011###E0",nxt(0.5).nio_write(fmt)
272
+ assert_equal "0.22250738585072014###E-307",MIN_N.nio_write(fmt)
273
+ assert_equal "0.22250738585072009###E-307",MAX_D.nio_write(fmt)
274
+ assert_equal "0.5###################E-323",MIN_D.nio_write(fmt)
275
+ assert_equal "0.64000000000000000###E2",(64.0).nio_write(fmt)
276
+ assert_equal "0.6400000000000001####E2",(nxt(64.0)).nio_write(fmt)
277
+ assert_equal "0.6409999999999999####E2",(64.1).nio_write(fmt)
278
+ assert_equal "0.6412312300000001####E2",(64.123123).nio_write(fmt)
279
+ assert_equal "0.10000000000000001###E0",(0.1).nio_write(fmt)
280
+ assert_equal "0.6338253001141148####E30",nxt(Math.ldexp(0.5,100)).nio_write(fmt)
281
+ assert_equal "0.39443045261050599###E-30",nxt(Math.ldexp(0.5,-100)).nio_write(fmt)
282
+ assert_equal "0.10##################E-322",nxt(MIN_D).nio_write(fmt)
283
+ assert_equal "0.15##################E-322",nxt(nxt(MIN_D)).nio_write(fmt)
284
+
285
+ # note: 1E23 is equidistant from 2 Floats; one or the other will be chosen based on the rounding mode
286
+ x = Float.nio_read('1E23',Fmt.prec(:exact,:gen,:round=>:even))
287
+ assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
288
+ assert_equal "9.999999999999999E22",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
289
+ assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
290
+
291
+ x = Float.nio_read('1E23',Fmt.prec(:exact,:gen,:round=>:zero))
292
+ assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
293
+ assert_equal "9.999999999999999E22",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
294
+ assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
295
+
296
+ x = Float.nio_read('1E23',Fmt.prec(:exact,:gen,:round=>:inf))
297
+ assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
298
+ assert_equal "1.0000000000000001E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
299
+ assert_equal "1.0000000000000001E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
300
+
301
+ x = Float.nio_read('-1E23',Fmt.prec(:exact,:gen,:round=>:even))
302
+ assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
303
+ assert_equal "-9.999999999999999E22",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
304
+ assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
305
+
306
+ x = Float.nio_read('-1E23',Fmt.prec(:exact,:gen,:round=>:zero))
307
+ assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
308
+ assert_equal "-9.999999999999999E22",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
309
+ assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
310
+
311
+ x = Float.nio_read('-1E23',Fmt.prec(:exact,:gen,:round=>:inf))
312
+ assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
313
+ assert_equal "-1.0000000000000001E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
314
+ assert_equal "-1.0000000000000001E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
315
+
316
+ # note: for 64.1 there's only one closest Float;
317
+ # but it can be univocally expressed in decimal either as 64.09999999999999 or 64.1
318
+ x = Float.nio_read('64.1',Fmt.prec(:exact,:gen,:round=>:even))
319
+ assert_equal "64.09999999999999",x.nio_write(Fmt.prec(:exact,:gen).show_all_digits(true))
320
+ assert_equal "64.1",x.nio_write(Fmt.prec(:exact,:gen))
321
+
322
+ # to do: exact conversion of Rational(32095022417, 54517) should throw and exception
323
+ # (unless RepDec.max_d is greater than 27300 or so)
324
+
325
+
326
+ end
327
+
328
+ def test_special
329
+ assert BigDecimal.nio_read("NaN").nan?
330
+ assert Float.nio_read("NaN").nan?
331
+ assert_equal "NAN", BigDec("NaN").nio_write.upcase
332
+ assert_equal "NAN", BigDecimal.nio_read("NaN").nio_write.upcase
333
+ assert_equal "NAN", Float.nio_read("NaN").nio_write.upcase
334
+ assert_raises ZeroDivisionError do Rational.nio_read("NaN") end
335
+
336
+ assert !BigDecimal.nio_read('Infinity').finite?
337
+ assert !BigDecimal.nio_read('+Infinity').finite?
338
+ assert !BigDecimal.nio_read('-Infinity').finite?
339
+ assert !Float.nio_read('Infinity').finite?
340
+ assert !Float.nio_read('+Infinity').finite?
341
+ assert !Float.nio_read('-Infinity').finite?
342
+ assert_raises ZeroDivisionError do Rational.nio_read("Infinity") end
343
+ assert_raises ZeroDivisionError do Rational.nio_read("+Infinity") end
344
+ assert_raises ZeroDivisionError do Rational.nio_read("-Infinity") end
345
+ assert_equal BigDec(1)/0, BigDecimal.nio_read('Infinity')
346
+ assert_equal BigDec(-1)/0, BigDecimal.nio_read('-Infinity')
347
+ assert_equal '+Infinity', BigDecimal.nio_read('Infinity').nio_write
348
+ assert_equal '+Infinity', BigDecimal.nio_read('+Infinity').nio_write
349
+ assert_equal '-Infinity', BigDecimal.nio_read('-Infinity').nio_write
350
+ assert_equal '+Infinity', Float.nio_read('Infinity').nio_write
351
+ assert_equal '+Infinity', Float.nio_read('+Infinity').nio_write
352
+ assert_equal '-Infinity', Float.nio_read('-Infinity').nio_write
353
+
354
+ end
355
+
356
+ def test_conversions
357
+ x_txt = '1.234567890123456'
358
+ x_d = BigDecimal.nio_read(x_txt)
359
+ x_f = Float.nio_read(x_txt)
360
+ assert_equal 1.234567890123456, x_f
361
+ assert_equal BigDecimal(x_txt), x_d
362
+ assert_equal Nio.convert(x_d,Float,:exact), x_f
363
+ assert_equal Nio.convert(x_d,Float,:approx), x_f
364
+
365
+ x_d = BigDec(355)/226
366
+ x_f = Float(355)/226
367
+ assert_equal Nio.convert(x_d,Float,:exact), x_f
368
+ assert_equal Nio.convert(x_d,Float,:approx), x_f
369
+
370
+ end
371
+
372
+
373
+ end
@@ -0,0 +1,32 @@
1
+ require 'test/unit'
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