nio 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +8 -0
- data/Manifest.txt +1 -2
- data/README.txt +519 -560
- data/Rakefile +1 -0
- data/lib/nio.rb +6 -7
- data/lib/nio/fmt.rb +378 -437
- data/lib/nio/repdec.rb +31 -31
- data/lib/nio/rtnlzr.rb +88 -81
- data/lib/nio/sugar.rb +69 -99
- data/lib/nio/version.rb +1 -1
- data/tasks/nuweb.rake +53 -33
- data/test/{test_helper.rb → helper.rb} +5 -0
- data/test/test_fmt.rb +98 -100
- data/test/test_repdec.rb +3 -5
- data/test/test_rtnlzr.rb +66 -44
- data/test/test_tools.rb +2 -2
- metadata +17 -7
- data/lib/nio/flttol.rb +0 -669
data/lib/nio/sugar.rb
CHANGED
@@ -1,99 +1,69 @@
|
|
1
|
-
# This file provides some syntactic sugar for the Nio module.
|
2
|
-
# Some methods here: #to_r(), #to_xr, may collide with methods in other
|
3
|
-
# libraries.
|
4
|
-
#
|
5
|
-
# This non mondule-function is equivalent to +Nio::Fmt.convert+
|
6
|
-
# Nio.convert(x, type, arpx=true)
|
7
|
-
# There's also a module-function synonim useful for including the Nio namespace:
|
8
|
-
# Nio.nio_convert(x, type, aprx=true)
|
9
|
-
# (the convert() method seems too likely for name collisions)
|
10
|
-
# Some aliases for nio_write and nio_read:
|
11
|
-
# fmt << x -> x.nio_write(fmt)
|
12
|
-
# fmt.write(x) -> x.nio_write(fmt)
|
13
|
-
# Fmt << x -> x.nio_write()
|
14
|
-
# Fmt.write(x) -> x.nio_write()
|
15
|
-
# fmt >> [cls,txt] -> cls.nio_read(txt, fmt)
|
16
|
-
# fmt.read(cls,txt) -> cls.nio_read(txt, fmt)
|
17
|
-
# Fmt >> [cls,txt] -> cls.nio_read(txt)
|
18
|
-
# Fmt.read(cls,txt) -> cls.nio_read(txt)
|
19
|
-
# Also methods #to_r and #to_xr are added to Float,BigDecimal, etc. as
|
20
|
-
# synonims for #nio_r, #nio_xr
|
21
|
-
|
22
|
-
require 'nio/rtnlzr'
|
23
|
-
require 'nio/fmt'
|
24
|
-
|
25
|
-
# This is not a module function: this provides a shorthand access to Nio::Fmt.convert
|
26
|
-
def Nio.convert(x, type, mode=:approx)
|
27
|
-
Nio::Fmt.convert x, type, mode
|
28
|
-
end
|
29
|
-
|
30
|
-
module Nio
|
31
|
-
module_function
|
32
|
-
# This module function can be used after <tt>import Nio</tt>
|
33
|
-
def nio_convert(x, type, mode=:approx)
|
34
|
-
Nio::Fmt.convert x, type, mode
|
35
|
-
end
|
36
|
-
# :stopdoc:
|
37
|
-
class Fmt
|
38
|
-
def <<(x)
|
39
|
-
x.nio_write(self)
|
40
|
-
end
|
41
|
-
def write(x)
|
42
|
-
x.nio_write(self)
|
43
|
-
end
|
44
|
-
def Fmt.<<(x)
|
45
|
-
x.nio_write
|
46
|
-
end
|
47
|
-
def Fmt.write(x)
|
48
|
-
x.nio_write
|
49
|
-
end
|
50
|
-
def >>(cls_txt)
|
51
|
-
cls,txt = cls_txt
|
52
|
-
cls.nio_read(txt,self)
|
53
|
-
end
|
54
|
-
def read(cls,txt)
|
55
|
-
cls.nio_read(txt,self)
|
56
|
-
end
|
57
|
-
def Fmt.>>(cls_txt)
|
58
|
-
cls,txt = cls_txt
|
59
|
-
cls.nio_read(txt)
|
60
|
-
end
|
61
|
-
def Fmt.read(cls,txt)
|
62
|
-
cls.nio_read(txt)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
# :startdoc:
|
66
|
-
end
|
67
|
-
|
68
|
-
# to be considered: for cls in [Float,BigDecimal,Integer,Rational]
|
69
|
-
# def cls.<<(txt_fmt); txt,fmt=txt_fmt; cls.nio_read(txt,fmt); end
|
70
|
-
|
71
|
-
# :stopdoc:
|
72
|
-
|
73
|
-
#~ class Numeric
|
74
|
-
#~ def to_xr
|
75
|
-
#~ nio_xr
|
76
|
-
#~ end
|
77
|
-
#~ end
|
78
|
-
|
79
|
-
for cls in [Integer,Rational,Float,BigDecimal]
|
80
|
-
cls.class_eval {
|
81
|
-
def to_xr
|
82
|
-
nio_xr
|
83
|
-
end
|
84
|
-
}
|
85
|
-
end
|
86
|
-
|
87
|
-
class Float
|
88
|
-
def to_r(tol = Nio::Tolerance.big_epsilon)
|
89
|
-
nio_r(tol)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
class BigFloat
|
93
|
-
def to_r(tol = nil)
|
94
|
-
nio_r(tol)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
# :startdoc:
|
99
|
-
|
1
|
+
# This file provides some syntactic sugar for the Nio module.
|
2
|
+
# Some methods here: #to_r(), #to_xr, may collide with methods in other
|
3
|
+
# libraries.
|
4
|
+
#
|
5
|
+
# This non mondule-function is equivalent to +Nio::Fmt.convert+
|
6
|
+
# Nio.convert(x, type, arpx=true)
|
7
|
+
# There's also a module-function synonim useful for including the Nio namespace:
|
8
|
+
# Nio.nio_convert(x, type, aprx=true)
|
9
|
+
# (the convert() method seems too likely for name collisions)
|
10
|
+
# Some aliases for nio_write and nio_read:
|
11
|
+
# fmt << x -> x.nio_write(fmt)
|
12
|
+
# fmt.write(x) -> x.nio_write(fmt)
|
13
|
+
# Fmt << x -> x.nio_write()
|
14
|
+
# Fmt.write(x) -> x.nio_write()
|
15
|
+
# fmt >> [cls,txt] -> cls.nio_read(txt, fmt)
|
16
|
+
# fmt.read(cls,txt) -> cls.nio_read(txt, fmt)
|
17
|
+
# Fmt >> [cls,txt] -> cls.nio_read(txt)
|
18
|
+
# Fmt.read(cls,txt) -> cls.nio_read(txt)
|
19
|
+
# Also methods #to_r and #to_xr are added to Float,BigDecimal, etc. as
|
20
|
+
# synonims for #nio_r, #nio_xr
|
21
|
+
|
22
|
+
require 'nio/rtnlzr'
|
23
|
+
require 'nio/fmt'
|
24
|
+
|
25
|
+
# This is not a module function: this provides a shorthand access to Nio::Fmt.convert
|
26
|
+
def Nio.convert(x, type, mode=:approx)
|
27
|
+
Nio::Fmt.convert x, type, mode
|
28
|
+
end
|
29
|
+
|
30
|
+
module Nio
|
31
|
+
module_function
|
32
|
+
# This module function can be used after <tt>import Nio</tt>
|
33
|
+
def nio_convert(x, type, mode=:approx)
|
34
|
+
Nio::Fmt.convert x, type, mode
|
35
|
+
end
|
36
|
+
# :stopdoc:
|
37
|
+
class Fmt
|
38
|
+
def <<(x)
|
39
|
+
x.nio_write(self)
|
40
|
+
end
|
41
|
+
def write(x)
|
42
|
+
x.nio_write(self)
|
43
|
+
end
|
44
|
+
def Fmt.<<(x)
|
45
|
+
x.nio_write
|
46
|
+
end
|
47
|
+
def Fmt.write(x)
|
48
|
+
x.nio_write
|
49
|
+
end
|
50
|
+
def >>(cls_txt)
|
51
|
+
cls,txt = cls_txt
|
52
|
+
cls.nio_read(txt,self)
|
53
|
+
end
|
54
|
+
def read(cls,txt)
|
55
|
+
cls.nio_read(txt,self)
|
56
|
+
end
|
57
|
+
def Fmt.>>(cls_txt)
|
58
|
+
cls,txt = cls_txt
|
59
|
+
cls.nio_read(txt)
|
60
|
+
end
|
61
|
+
def Fmt.read(cls,txt)
|
62
|
+
cls.nio_read(txt)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
# :startdoc:
|
66
|
+
end
|
67
|
+
|
68
|
+
# to be considered: for cls in [Float,BigDecimal,Integer,Rational]
|
69
|
+
# def cls.<<(txt_fmt); txt,fmt=txt_fmt; cls.nio_read(txt,fmt); end
|
data/lib/nio/version.rb
CHANGED
data/tasks/nuweb.rake
CHANGED
@@ -1,37 +1,67 @@
|
|
1
1
|
# nuweb build tasks
|
2
2
|
namespace :nuweb do
|
3
3
|
|
4
|
+
NUWEB_PRODUCTS = []
|
5
|
+
NUWEB_SOURCES = Dir['source/**/*.w']
|
6
|
+
|
7
|
+
NUWEB_SOURCES.each do |input_fn|
|
8
|
+
products = []
|
9
|
+
File.open(input_fn) do |input|
|
10
|
+
meta = '@'
|
11
|
+
input.each_line do |line|
|
12
|
+
if /^\s*%#{meta}r(.)%/.match(line)
|
13
|
+
meta = $1
|
14
|
+
elsif /^\s*[^#{meta}]?#{meta}(?:o|O)\s*(\S.*)\s*$/.match(line)
|
15
|
+
products << $1
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
NUWEB_PRODUCTS.concat products
|
20
|
+
products.each do |product|
|
21
|
+
file product => [input_fn] do |t|
|
22
|
+
puts "nuweb -t #{input_fn}"
|
23
|
+
puts `nuweb -t #{input_fn}`
|
24
|
+
touch product
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
%w{lib test}.each do |dir|
|
30
|
+
sources = Dir["source/#{dir}/**/*"]
|
31
|
+
NUWEB_SOURCES.concat sources
|
32
|
+
NUWEB_PRODUCTS.concat sources.map{|s| s.sub("source/#{dir}/","#{dir}/")}
|
33
|
+
rule(/\A#{dir}\/.*/ =>[proc{|tn| tn.sub(/\A#{dir}\//, "source/#{dir}/") }]) do |t|
|
34
|
+
if t.source
|
35
|
+
if File.directory?(t.source)
|
36
|
+
cp_r t.source, t.name
|
37
|
+
else
|
38
|
+
cp t.source, t.name
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
4
44
|
desc "Generate Ruby code from nuweb source"
|
5
|
-
task :tangle =>
|
6
|
-
Dir['source/lib/**/*.rb'].collect{|fn| fn.gsub('source/lib/','lib/')}+
|
7
|
-
Dir['source/test/**/*'].collect{|fn| fn.gsub('source/test/','test/')}+
|
8
|
-
[:test]
|
45
|
+
task :tangle => NUWEB_PRODUCTS + [:test]
|
9
46
|
|
10
47
|
# directory 'lib'
|
11
48
|
# directory 'lib/nio'
|
12
49
|
# directory 'source/pdf'
|
13
50
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
puts `nuweb -t #{t.source}`
|
18
|
-
File.open(t.name,'w'){|f| f.puts "sentinel"}
|
19
|
-
end
|
20
|
-
|
21
|
-
clean_exts = ['*.tex','*.dvi','*.log','*.aux','*.out','*.ws']
|
22
|
-
clobber_dirs = ['lib', 'source/pdf', 'test']
|
23
|
-
clobber_exceptions = ['test/data.yaml', 'test/test_helper.rb']
|
51
|
+
clean_exts = ['*.tex','*.dvi','*.log','*.aux','*.out']
|
52
|
+
clobber_exts = []
|
53
|
+
generated_dirs = ['lib', 'test', 'source/pdf']
|
24
54
|
|
25
55
|
desc "Remove all nuweb generated files"
|
26
|
-
task :clobber=>[
|
27
|
-
|
56
|
+
task :clobber=>['^clobber'] do |t|
|
57
|
+
generated_dirs.map{|dir| Dir["#{dir}/**/*"]}.flatten.each do |fn|
|
28
58
|
rm fn unless File.directory?(fn)
|
29
59
|
end
|
30
60
|
end
|
31
61
|
|
32
|
-
desc "Clean up nuweb temporary files"
|
62
|
+
desc "Clean up nuweb weave temporary files"
|
33
63
|
task :clean do |t|
|
34
|
-
rm_r clean_exts.collect{|x| Dir.glob('
|
64
|
+
rm_r clean_exts.collect{|x| Dir.glob('source/*'+x)+Dir.glob('source/pdf/*'+x)}.flatten
|
35
65
|
end
|
36
66
|
|
37
67
|
desc "Generate nuweb source code documentation"
|
@@ -75,22 +105,14 @@ namespace :nuweb do
|
|
75
105
|
w_to_pdf t.source
|
76
106
|
end
|
77
107
|
|
78
|
-
rule /\Alib\/.*\.rb/ =>[proc{|tn| tn.sub(/\Alib\//, 'source/lib/') }] do |t|
|
79
|
-
cp t.source, t.name if t.source
|
80
|
-
end
|
81
|
-
|
82
|
-
rule /\Atest\/.*/ =>[proc{|tn| tn.sub(/\Atest\//, 'source/test/') }] do |t|
|
83
|
-
cp t.source, t.name if t.source
|
84
|
-
end
|
85
|
-
|
86
108
|
namespace :docs do
|
87
109
|
|
88
|
-
task :package=>['nuweb:weave']
|
89
110
|
Rake::PackageTask.new('nio-source-pdf', Nio::VERSION::STRING) do |p|
|
90
111
|
# generate same formats as for the gem contents
|
91
112
|
p.need_tar = PROJ.gem.need_tar
|
92
113
|
p.need_zip = PROJ.gem.need_zip
|
93
|
-
|
114
|
+
pdf_files = Dir['source/**/*.w'].map{|fn| File.join 'source','pdf',File.basename(fn,'.w')+'.pdf'}
|
115
|
+
p.package_files.include *pdf_files
|
94
116
|
end
|
95
117
|
|
96
118
|
end
|
@@ -113,13 +135,11 @@ namespace :nuweb do
|
|
113
135
|
|
114
136
|
end
|
115
137
|
|
116
|
-
task :clobber=>'nuweb:
|
138
|
+
task :clobber=>'nuweb:clean'
|
117
139
|
task :clean=>'nuweb:clean'
|
118
140
|
|
119
|
-
Rake::Task['gem:package'].
|
120
|
-
Rake::Task['gem:
|
141
|
+
gem_package_prerequisites = Rake::Task['gem:package'].prerequisites.dup
|
142
|
+
Rake::Task['gem:package'].clear_prerequisites.enhance ['nuweb:tangle']+gem_package_prerequisites
|
121
143
|
|
122
144
|
desc 'Generate code and documentation from nuweb sources'
|
123
145
|
task :nuweb => ['nuweb:tangle', 'nuweb:weave']
|
124
|
-
|
125
|
-
STDERR.puts "TTT #{Rake::Task['gem:package'].prerequisites.inspect}"
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require File.dirname(__FILE__) + '/../lib/nio'
|
3
|
+
require File.dirname(__FILE__) + '/../lib/nio/sugar'
|
3
4
|
require 'yaml'
|
4
5
|
|
5
6
|
module PrepareData
|
@@ -30,3 +31,7 @@ module PrepareData
|
|
30
31
|
end
|
31
32
|
|
32
33
|
PrepareData.init
|
34
|
+
|
35
|
+
def BigDec(x)
|
36
|
+
BigDecimal.new(x.to_s)
|
37
|
+
end
|
data/test/test_fmt.rb
CHANGED
@@ -7,26 +7,24 @@
|
|
7
7
|
# as published by the Free Software Foundation; either version 2
|
8
8
|
# of the License, or (at your option) any later version.
|
9
9
|
|
10
|
-
|
10
|
+
require File.dirname(__FILE__) + '/helper.rb'
|
11
11
|
require 'test/unit'
|
12
|
-
require '
|
13
|
-
require 'nio/repdec'
|
14
|
-
require 'nio/fmt'
|
12
|
+
require 'flt/bigdecimal'
|
15
13
|
include Nio
|
16
14
|
require 'yaml'
|
17
15
|
|
18
16
|
def neighbours(x)
|
19
|
-
f,e = Math.frexp(x)
|
17
|
+
f,e = Math.frexp(x)
|
20
18
|
e = Float::MIN_EXP if f==0
|
21
19
|
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)
|
20
|
+
dx = Math.ldexp(1,e-Float::MANT_DIG) #Math.ldexp(Math.ldexp(1.0,-Float::MANT_DIG),e)
|
23
21
|
high = x + dx
|
24
22
|
if e==Float::MIN_EXP || f!=0.5 #0.5==Math.ldexp(2**(bits-1),-Float::MANT_DIG)
|
25
23
|
low = x - dx
|
26
24
|
else
|
27
|
-
low = x - dx/2 # x - Math.ldexp(Math.ldexp(1.0,-Float::MANT_DIG),e-1)
|
25
|
+
low = x - dx/2 # x - Math.ldexp(Math.ldexp(1.0,-Float::MANT_DIG),e-1)
|
28
26
|
end
|
29
|
-
[low, high]
|
27
|
+
[low, high]
|
30
28
|
end
|
31
29
|
|
32
30
|
def prv(x)
|
@@ -55,59 +53,59 @@ class TestFmt < Test::Unit::TestCase
|
|
55
53
|
Fmt.default = Fmt.new
|
56
54
|
|
57
55
|
end
|
58
|
-
|
56
|
+
|
59
57
|
|
60
58
|
def test_basic_fmt
|
61
59
|
# test correct rounding: 1.448997445238699 -> 6525704354437805*2^-52
|
62
60
|
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
|
61
|
+
|
62
|
+
assert_equal "0",0.0.nio_write
|
63
|
+
assert_equal "0",0.nio_write
|
64
|
+
assert_equal "0",BigDecimal('0').nio_write
|
65
|
+
assert_equal "0",Rational(0,1).nio_write
|
66
|
+
|
67
|
+
assert_equal "123456789",123456789.0.nio_write
|
68
|
+
assert_equal "123456789",123456789.nio_write
|
69
|
+
assert_equal "123456789",BigDecimal('123456789').nio_write
|
70
|
+
assert_equal "123456789",Rational(123456789,1).nio_write
|
71
|
+
assert_equal "123456789.25",123456789.25.nio_write
|
72
|
+
assert_equal "123456789.25",BigDecimal('123456789.25').nio_write
|
73
|
+
assert_equal "123456789.25",(Rational(123456789)+Rational(1,4)).nio_write
|
76
74
|
end
|
77
75
|
|
78
76
|
def test_basic_fmt_float
|
79
77
|
|
80
78
|
assert_equal 2,Float::RADIX
|
81
79
|
assert_equal 53,Float::MANT_DIG
|
82
|
-
|
80
|
+
|
83
81
|
fmt = Fmt.new {|f|
|
84
|
-
f.rep! '[','','...',0,true
|
82
|
+
f.rep! '[','','...',0,true
|
85
83
|
f.width! 20,:right,'*'
|
86
84
|
}
|
87
|
-
fmt.sep! '.',',',[3]
|
88
|
-
|
85
|
+
fmt.sep! '.',',',[3]
|
86
|
+
|
89
87
|
assert_equal "0.1",0.1.nio_write
|
90
88
|
assert_equal "0.10000000000000001",0.1.nio_write(Fmt.mode(:gen,:exact).show_all_digits)
|
91
89
|
assert_equal "0.10000000000000001",0.1.nio_write(Fmt.mode(:gen,:exact).show_all_digits(true))
|
92
90
|
assert_equal "0.10000000000000001",0.1.nio_write(Fmt.mode(:gen,:exact,:show_all_digits=>true))
|
93
91
|
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"))
|
92
|
+
|
93
|
+
|
94
|
+
assert_equal "******643,454,333.32",fmt.nio_write_formatted(fmt.nio_read_formatted("643,454,333.32"))
|
95
|
+
assert_equal "******643.454.333,32",fmt.sep(',').nio_write_formatted(fmt.nio_read_formatted("643,454,333.32"))
|
98
96
|
fmt.pad0s! 10
|
99
97
|
num = fmt.nio_read_formatted("0.3333...")
|
100
98
|
assert_equal "0000000.[3",fmt.nio_write_formatted(num)
|
101
99
|
fmt.mode! :fix, 3
|
102
100
|
assert_equal "000000.333",fmt.nio_write_formatted(num)
|
103
|
-
num = fmt.nio_read_formatted("-0.666...")
|
101
|
+
num = fmt.nio_read_formatted("-0.666...")
|
104
102
|
fmt.prec! :exact
|
105
103
|
fmt.sep! ',','.'
|
106
104
|
assert_equal "-000000,[6",fmt.nio_write_formatted(num)
|
107
105
|
fmt.width! 20,:center,'*'
|
108
106
|
fmt.mode! :fix,3
|
109
107
|
assert_equal "*******-0,667*******",fmt.nio_write_formatted(num)
|
110
|
-
num = fmt.nio_read_formatted("0,5555")
|
108
|
+
num = fmt.nio_read_formatted("0,5555")
|
111
109
|
fmt.prec! :exact
|
112
110
|
assert_equal "*******0,5555*******",fmt.nio_write_formatted(num)
|
113
111
|
|
@@ -116,15 +114,15 @@ class TestFmt < Test::Unit::TestCase
|
|
116
114
|
assert_equal 11123.2343,x
|
117
115
|
assert_equal "11.123,2343", x.nio_write
|
118
116
|
assert_equal "11123,2343", x.nio_write(Fmt[:comma])
|
119
|
-
|
117
|
+
|
120
118
|
x = Float.nio_read("-1234,5678901234e-33")
|
121
119
|
# assert_equal -1.2345678901234e-030, x
|
122
120
|
assert_equal "-1,2345678901234E-30", x.nio_write()
|
123
121
|
assert_equal "-0,0000000000000000000000000000012346",x.nio_write(Fmt[:comma].mode(:sig,5))
|
124
122
|
|
125
|
-
assert_equal "0,333...",
|
123
|
+
assert_equal "0,333...",
|
126
124
|
(1.0/3).nio_write(Fmt.prec(:exact).show_all_digits(true).approx_mode(:simplify))
|
127
|
-
|
125
|
+
|
128
126
|
fmt = Fmt.default
|
129
127
|
if RUBY_VERSION>='1.9.0'
|
130
128
|
assert_raises RuntimeError do fmt.prec! 4 end
|
@@ -134,42 +132,28 @@ class TestFmt < Test::Unit::TestCase
|
|
134
132
|
fmt = Fmt.default {|f| f.prec! 4 }
|
135
133
|
assert_equal "1,235", 1.23456.nio_write(fmt)
|
136
134
|
assert_equal "1,23456", 1.23456.nio_write()
|
137
|
-
|
135
|
+
|
138
136
|
Fmt.default = Fmt.new
|
139
137
|
assert_equal '11123.2343', 11123.2343.nio_write
|
140
138
|
end
|
141
139
|
|
142
140
|
def test_tol_fmt_float
|
143
|
-
tol = Tolerance
|
141
|
+
tol = Flt.Tolerance(12, :sig_decimals)
|
144
142
|
fmt = Fmt.prec(12,:sig)
|
145
143
|
$data.each do |x|
|
146
|
-
assert tol.
|
147
|
-
assert tol.
|
144
|
+
assert tol.eq?(x, Float.nio_read(x.nio_write(fmt),fmt)), "out of tolerance: #{x.inspect} #{Float.nio_read(x.nio_write(fmt),fmt)}"
|
145
|
+
assert tol.eq?(-x, Float.nio_read((-x).nio_write(fmt),fmt)), "out of tolerance: #{(-x).inspect} #{Float.nio_read((-x).nio_write(fmt),fmt)}"
|
148
146
|
end
|
149
147
|
end
|
150
148
|
|
151
|
-
def test_BigDec
|
152
|
-
assert_equal "0",BigDec(0).nio_write
|
153
|
-
fmt = Fmt.mode(:gen,:exact)
|
154
|
-
assert_equal "0",BigDec(0).nio_write(fmt)
|
155
|
-
$data.each do |x|
|
156
|
-
x = BigDecimal(x.to_s)
|
157
|
-
assert_equal x,BigDecimal.nio_read(x.nio_write(fmt),fmt)
|
158
|
-
end
|
159
|
-
assert_equal "1E500",BigDec('1E500').nio_write
|
160
|
-
assert_equal "1E-500",BigDec('1E-500').nio_write
|
161
|
-
assert_equal "-1E500",BigDec('-1E500').nio_write
|
162
|
-
assert_equal "-1E-500",BigDec('-1E-500').nio_write
|
163
|
-
end
|
164
|
-
|
165
149
|
def test_Rational
|
166
|
-
assert_equal "0",Rational(0,1).nio_write
|
150
|
+
assert_equal "0",Rational(0,1).nio_write
|
167
151
|
fmt = Fmt.mode(:gen,:exact)
|
168
152
|
assert_equal "0",Rational(0,1).nio_write(fmt)
|
169
153
|
$data.each do |x|
|
170
154
|
x = x.nio_xr # nio_r
|
171
155
|
assert_equal x,Rational.nio_read(x.nio_write(fmt),fmt)
|
172
|
-
end
|
156
|
+
end
|
173
157
|
end
|
174
158
|
|
175
159
|
def test_float_bases
|
@@ -185,7 +169,7 @@ class TestFmt < Test::Unit::TestCase
|
|
185
169
|
assert_equal(-x,Float.nio_read((-x).nio_write(nfmt2),nfmt2))
|
186
170
|
assert_equal(-x,Float.nio_read((-x).nio_write(nfmt8),nfmt8))
|
187
171
|
assert_equal(-x,Float.nio_read((-x).nio_write(nfmt10),nfmt10))
|
188
|
-
assert_equal(-x,Float.nio_read((-x).nio_write(nfmt16),nfmt16))
|
172
|
+
assert_equal(-x,Float.nio_read((-x).nio_write(nfmt16),nfmt16))
|
189
173
|
end
|
190
174
|
end
|
191
175
|
|
@@ -194,31 +178,29 @@ class TestFmt < Test::Unit::TestCase
|
|
194
178
|
end
|
195
179
|
|
196
180
|
def test_big_decimal_bases
|
197
|
-
|
198
|
-
assert_equal "0.1999A",(
|
199
|
-
assert_equal "0.1999...",(
|
200
|
-
|
181
|
+
|
182
|
+
assert_equal "0.1999A",(Flt.DecNum(1)/10).normalize.nio_write(Fmt.new.base(16).prec(5))
|
183
|
+
assert_equal "0.1999...",(Flt.DecNum(1)/10).nio_write(Fmt.mode(:gen,:exact,:round=>:inf,:approx=>:simplify).base(16))
|
184
|
+
|
201
185
|
nfmt2 = Fmt[:comma].base(2).prec(:exact)
|
202
186
|
nfmt8 = Fmt[:comma].base(8).prec(:exact)
|
203
187
|
nfmt10 = Fmt[:comma].base(10).prec(:exact)
|
204
188
|
nfmt16 = Fmt[:comma].base(16).prec(:exact)
|
205
|
-
$data.each do |x|
|
206
|
-
x =
|
207
|
-
|
208
|
-
ndig = xdig.size
|
209
|
-
round_dig = ndig-xe
|
189
|
+
$data.each do |x|
|
190
|
+
x = Flt.DecNum(x.to_s)
|
191
|
+
round_dig = x.number_of_digits - x.adjusted_exponent - 1
|
210
192
|
# note that BigDecimal.nio_read produces a BigDecimal with the exact value of the text representation
|
211
193
|
# since the representation here is only aproximate (because of the base difference), we must
|
212
194
|
# round the results to the precision of the original number
|
213
|
-
assert_equal(x,
|
214
|
-
assert_equal(x,
|
215
|
-
assert_equal(x,
|
216
|
-
assert_equal(x,
|
217
|
-
assert_equal(-x,
|
218
|
-
assert_equal(-x,
|
219
|
-
assert_equal(-x,
|
220
|
-
assert_equal(-x,
|
221
|
-
end
|
195
|
+
assert_equal(x,Flt::DecNum.nio_read(x.nio_write(nfmt2),nfmt2).round(round_dig))
|
196
|
+
assert_equal(x,Flt::DecNum.nio_read(x.nio_write(nfmt8),nfmt8).round(round_dig))
|
197
|
+
assert_equal(x,Flt::DecNum.nio_read(x.nio_write(nfmt10),nfmt10).round(round_dig))
|
198
|
+
assert_equal(x,Flt::DecNum.nio_read(x.nio_write(nfmt16),nfmt16).round(round_dig))
|
199
|
+
assert_equal(-x,Flt::DecNum.nio_read((-x).nio_write(nfmt2),nfmt2).round(round_dig))
|
200
|
+
assert_equal(-x,Flt::DecNum.nio_read((-x).nio_write(nfmt8),nfmt8).round(round_dig))
|
201
|
+
assert_equal(-x,Flt::DecNum.nio_read((-x).nio_write(nfmt10),nfmt10).round(round_dig))
|
202
|
+
assert_equal(-x,Flt::DecNum.nio_read((-x).nio_write(nfmt16),nfmt16).round(round_dig))
|
203
|
+
end
|
222
204
|
end
|
223
205
|
|
224
206
|
def test_exact_all_float
|
@@ -231,10 +213,10 @@ class TestFmt < Test::Unit::TestCase
|
|
231
213
|
assert_equal "0.66666666666666662965923251249478198587894439697265625", (2.0/3.0).nio_write(fmt)
|
232
214
|
assert_equal "-0.333333333333333314829616256247390992939472198486328125", (-1.0/3.0).nio_write(fmt)
|
233
215
|
assert_equal "-0.66666666666666662965923251249478198587894439697265625", (-2.0/3.0).nio_write(fmt)
|
234
|
-
assert_equal "1267650600228229401496703205376", (2.0**100).nio_write(fmt)
|
216
|
+
assert_equal "1267650600228229401496703205376", (2.0**100).nio_write(fmt)
|
235
217
|
assert_equal "0.10000000000000001942890293094023945741355419158935546875", nxt(0.1).nio_write(fmt)
|
236
218
|
assert_equal "1023.9999999999998863131622783839702606201171875", prv(1024).nio_write(fmt)
|
237
|
-
|
219
|
+
|
238
220
|
assert_equal "2.225073858507201383090232717332404064219215980462331830553327416887204434813918195854283159012511020564067339731035811005152434161553460108856012385377718821130777993532002330479610147442583636071921565046942503734208375250806650616658158948720491179968591639648500635908770118304874799780887753749949451580451605050915399856582470818645113537935804992115981085766051992433352114352390148795699609591288891602992641511063466313393663477586513029371762047325631781485664350872122828637642044846811407613911477062801689853244110024161447421618567166150540154285084716752901903161322778896729707373123334086988983175067838846926092773977972858659654941091369095406136467568702398678315290680984617210924625396728515625E-308",
|
239
221
|
MIN_N.nio_write(fmt)
|
240
222
|
assert_equal "2.2250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375E-308",
|
@@ -248,8 +230,24 @@ class TestFmt < Test::Unit::TestCase
|
|
248
230
|
|
249
231
|
end
|
250
232
|
|
233
|
+
def test_float_bin_num_coherence
|
234
|
+
Flt::BinNum.context(Flt::BinNum::FloatContext) do
|
235
|
+
[0.1, Float::MIN_D, Float::MIN_N, Float::MAX, 0.0, 1.0, 1.0/3].each do |x|
|
236
|
+
y = Flt::BinNum(x)
|
237
|
+
c = Float.context
|
238
|
+
assert_equal c.split(x), c.split(y) unless x.zero?
|
239
|
+
assert_equal x.nio_write(Fmt.prec(:exact)), y.nio_write(Fmt.prec(:exact))
|
240
|
+
assert_equal x.nio_write(Fmt.prec(:exact).show_all_digits), y.nio_write(Fmt.prec(:exact).show_all_digits)
|
241
|
+
assert_equal x.nio_write(Fmt.prec(:exact).approx_mode(:exact)), y.nio_write(Fmt.prec(:exact).approx_mode(:exact))
|
242
|
+
assert_equal x.nio_write(Fmt.mode(:fix,20).insignificant_digits('#')), y.nio_write(Fmt.mode(:fix,20).insignificant_digits('#'))
|
243
|
+
assert_equal x.nio_write(Fmt.mode(:fix,20)), y.nio_write(Fmt.mode(:fix,20))
|
244
|
+
assert_equal x.nio_write(Fmt.mode(:fix,20,:approx_mode=>:exact)), y.nio_write(Fmt.mode(:fix,20,:approx_mode=>:exact))
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
251
249
|
def test_float_nonsig
|
252
|
-
|
250
|
+
|
253
251
|
assert_equal "100.000000000000000#####", 100.0.nio_write(Fmt.prec(20,:fix).insignificant_digits('#'))
|
254
252
|
|
255
253
|
fmt = Fmt.mode(:sci,20).insignificant_digits('#').sci_digits(1)
|
@@ -265,12 +263,12 @@ class TestFmt < Test::Unit::TestCase
|
|
265
263
|
assert_equal "333.33333333333331###E-3", (1.0/3).nio_write(fmt)
|
266
264
|
assert_equal "3.3333333333333335###E6", (1E7/3).nio_write(fmt)
|
267
265
|
assert_equal "33.333333333333334###E-9",(1E-7/3).nio_write(fmt)
|
268
|
-
|
266
|
+
|
269
267
|
fmt = Fmt[:comma].mode(:sci,20).insignificant_digits('#').sci_digits(0)
|
270
268
|
assert_equal "0,33333333333333331###E0",(1.0/3).nio_write(fmt)
|
271
269
|
assert_equal "0,33333333333333335###E7",(1E7/3).nio_write(fmt)
|
272
270
|
assert_equal "0,33333333333333334###E-7",(1E-7/3).nio_write(fmt)
|
273
|
-
|
271
|
+
|
274
272
|
fmt = Fmt.mode(:sci,20).insignificant_digits('#').sci_digits(0)
|
275
273
|
assert_equal "0.10000000000000001###E0",(1E-1).nio_write(fmt)
|
276
274
|
assert_equal "0.50000000000000000###E0",(0.5).nio_write(fmt)
|
@@ -281,65 +279,65 @@ class TestFmt < Test::Unit::TestCase
|
|
281
279
|
assert_equal "0.5###################E-323",MIN_D.nio_write(fmt)
|
282
280
|
assert_equal "0.64000000000000000###E2",(64.0).nio_write(fmt)
|
283
281
|
assert_equal "0.6400000000000001####E2",(nxt(64.0)).nio_write(fmt)
|
284
|
-
assert_equal "0.6409999999999999####E2",(64.1).nio_write(fmt)
|
282
|
+
assert_equal "0.6409999999999999####E2",(64.1).nio_write(fmt)
|
285
283
|
assert_equal "0.6412312300000001####E2",(64.123123).nio_write(fmt)
|
286
284
|
assert_equal "0.10000000000000001###E0",(0.1).nio_write(fmt)
|
287
|
-
assert_equal "0.6338253001141148####E30",nxt(Math.ldexp(0.5,100)).nio_write(fmt)
|
285
|
+
assert_equal "0.6338253001141148####E30",nxt(Math.ldexp(0.5,100)).nio_write(fmt)
|
288
286
|
assert_equal "0.39443045261050599###E-30",nxt(Math.ldexp(0.5,-100)).nio_write(fmt)
|
289
287
|
assert_equal "0.10##################E-322",nxt(MIN_D).nio_write(fmt)
|
290
288
|
assert_equal "0.15##################E-322",nxt(nxt(MIN_D)).nio_write(fmt)
|
291
|
-
|
289
|
+
|
292
290
|
# note: 1E23 is equidistant from 2 Floats; one or the other will be chosen based on the rounding mode
|
293
291
|
x = Float.nio_read('1E23',Fmt.prec(:exact,:gen,:round=>:even))
|
294
292
|
assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
|
295
293
|
assert_equal "9.999999999999999E22",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
|
296
|
-
assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
294
|
+
assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
297
295
|
|
298
296
|
x = Float.nio_read('1E23',Fmt.prec(:exact,:gen,:round=>:zero))
|
299
297
|
assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
|
300
298
|
assert_equal "9.999999999999999E22",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
|
301
|
-
assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
299
|
+
assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
302
300
|
|
303
301
|
x = Float.nio_read('1E23',Fmt.prec(:exact,:gen,:round=>:inf))
|
304
302
|
assert_equal "1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
|
305
303
|
assert_equal "1.0000000000000001E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
|
306
|
-
assert_equal "1.0000000000000001E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
304
|
+
assert_equal "1.0000000000000001E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
307
305
|
|
308
306
|
x = Float.nio_read('-1E23',Fmt.prec(:exact,:gen,:round=>:even))
|
309
307
|
assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
|
310
308
|
assert_equal "-9.999999999999999E22",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
|
311
|
-
assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
309
|
+
assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
312
310
|
|
313
311
|
x = Float.nio_read('-1E23',Fmt.prec(:exact,:gen,:round=>:zero))
|
314
312
|
assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
|
315
313
|
assert_equal "-9.999999999999999E22",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
|
316
|
-
assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
314
|
+
assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
317
315
|
|
318
316
|
x = Float.nio_read('-1E23',Fmt.prec(:exact,:gen,:round=>:inf))
|
319
317
|
assert_equal "-1E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:inf))
|
320
318
|
assert_equal "-1.0000000000000001E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:zero))
|
321
|
-
assert_equal "-1.0000000000000001E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
322
|
-
|
323
|
-
# note: for 64.1 there's only one closest Float;
|
319
|
+
assert_equal "-1.0000000000000001E23",x.nio_write(Fmt.prec(:exact,:gen,:round=>:even))
|
320
|
+
|
321
|
+
# note: for 64.1 there's only one closest Float;
|
324
322
|
# but it can be univocally expressed in decimal either as 64.09999999999999 or 64.1
|
325
323
|
x = Float.nio_read('64.1',Fmt.prec(:exact,:gen,:round=>:even))
|
326
324
|
assert_equal "64.09999999999999",x.nio_write(Fmt.prec(:exact,:gen).show_all_digits(true))
|
327
325
|
assert_equal "64.1",x.nio_write(Fmt.prec(:exact,:gen))
|
328
|
-
|
326
|
+
|
329
327
|
# to do: exact conversion of Rational(32095022417, 54517) should throw and exception
|
330
328
|
# (unless RepDec.max_d is greater than 27300 or so)
|
331
|
-
|
332
|
-
|
329
|
+
|
330
|
+
|
333
331
|
end
|
334
332
|
|
335
333
|
def test_special
|
336
334
|
assert BigDecimal.nio_read("NaN").nan?
|
337
335
|
assert Float.nio_read("NaN").nan?
|
338
|
-
assert_equal "NAN",
|
336
|
+
assert_equal "NAN", Flt.DecNum("NaN").nio_write.upcase
|
339
337
|
assert_equal "NAN", BigDecimal.nio_read("NaN").nio_write.upcase
|
340
338
|
assert_equal "NAN", Float.nio_read("NaN").nio_write.upcase
|
341
339
|
assert_raises ZeroDivisionError do Rational.nio_read("NaN") end
|
342
|
-
|
340
|
+
|
343
341
|
assert !BigDecimal.nio_read('Infinity').finite?
|
344
342
|
assert !BigDecimal.nio_read('+Infinity').finite?
|
345
343
|
assert !BigDecimal.nio_read('-Infinity').finite?
|
@@ -357,7 +355,7 @@ class TestFmt < Test::Unit::TestCase
|
|
357
355
|
assert_equal '+Infinity', Float.nio_read('Infinity').nio_write
|
358
356
|
assert_equal '+Infinity', Float.nio_read('+Infinity').nio_write
|
359
357
|
assert_equal '-Infinity', Float.nio_read('-Infinity').nio_write
|
360
|
-
|
358
|
+
|
361
359
|
end
|
362
360
|
|
363
361
|
def test_conversions
|
@@ -368,12 +366,12 @@ class TestFmt < Test::Unit::TestCase
|
|
368
366
|
assert_equal BigDecimal(x_txt), x_d
|
369
367
|
assert_equal Fmt.convert(x_d,Float,:exact), x_f
|
370
368
|
assert_equal Fmt.convert(x_d,Float,:approx), x_f
|
371
|
-
|
372
|
-
x_d =
|
369
|
+
|
370
|
+
x_d = BigDecimal('355')/226
|
373
371
|
x_f = Float(355)/226
|
374
372
|
assert_equal Fmt.convert(x_d,Float,:exact), x_f
|
375
373
|
assert_equal Fmt.convert(x_d,Float,:approx), x_f
|
376
|
-
|
374
|
+
|
377
375
|
end
|
378
376
|
|
379
377
|
def test_sign
|
@@ -405,6 +403,6 @@ class TestFmt < Test::Unit::TestCase
|
|
405
403
|
assert_equal '+1.23E+5', 1.23E5.nio_write(Fmt.mode(:sci).show_exp_plus.show_plus)
|
406
404
|
assert_equal '-1.23E+5', -1.23E5.nio_write(Fmt.mode(:sci).show_exp_plus.show_plus)
|
407
405
|
end
|
408
|
-
|
406
|
+
|
409
407
|
|
410
408
|
end
|