numerals 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +149 -5
- data/lib/numerals/conversions/bigdecimal.rb +209 -9
- data/lib/numerals/conversions/context_conversion.rb +40 -0
- data/lib/numerals/conversions/float.rb +106 -71
- data/lib/numerals/conversions/flt.rb +115 -44
- data/lib/numerals/conversions/integer.rb +32 -3
- data/lib/numerals/conversions/rational.rb +27 -3
- data/lib/numerals/conversions.rb +74 -33
- data/lib/numerals/digits.rb +8 -5
- data/lib/numerals/format/base_scaler.rb +160 -0
- data/lib/numerals/format/exp_setter.rb +218 -0
- data/lib/numerals/format/format.rb +257 -0
- data/lib/numerals/format/input.rb +140 -0
- data/lib/numerals/format/mode.rb +157 -0
- data/lib/numerals/format/notation.rb +51 -0
- data/lib/numerals/format/notations/html.rb +53 -0
- data/lib/numerals/format/notations/latex.rb +48 -0
- data/lib/numerals/format/notations/text.rb +141 -0
- data/lib/numerals/format/output.rb +167 -0
- data/lib/numerals/format/symbols.rb +565 -0
- data/lib/numerals/format/text_parts.rb +35 -0
- data/lib/numerals/format.rb +25 -0
- data/lib/numerals/formatting_aspect.rb +36 -0
- data/lib/numerals/numeral.rb +34 -21
- data/lib/numerals/repeat_detector.rb +99 -0
- data/lib/numerals/rounding.rb +340 -181
- data/lib/numerals/version.rb +1 -1
- data/lib/numerals.rb +4 -2
- data/numerals.gemspec +1 -1
- data/test/test_base_scaler.rb +189 -0
- data/test/test_big_conversions.rb +105 -0
- data/test/test_digits_definition.rb +23 -28
- data/test/test_exp_setter.rb +732 -0
- data/test/test_float_conversions.rb +48 -30
- data/test/test_flt_conversions.rb +476 -80
- data/test/test_format.rb +124 -0
- data/test/test_format_input.rb +226 -0
- data/test/test_format_mode.rb +124 -0
- data/test/test_format_output.rb +789 -0
- data/test/test_integer_conversions.rb +22 -22
- data/test/test_numeral.rb +35 -0
- data/test/test_rational_conversions.rb +28 -28
- data/test/test_repeat_detector.rb +72 -0
- data/test/test_rounding.rb +158 -0
- data/test/test_symbols.rb +32 -0
- metadata +38 -5
- data/lib/numerals/formatting/digits_definition.rb +0 -75
@@ -0,0 +1,789 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
|
2
|
+
include Numerals
|
3
|
+
require 'yaml'
|
4
|
+
require 'tempfile'
|
5
|
+
|
6
|
+
class TestFormatOutput < Test::Unit::TestCase # < Minitest::Test
|
7
|
+
|
8
|
+
def test_write_float_dec
|
9
|
+
assert_equal '1', Format[rounding: :short].write(1.0)
|
10
|
+
assert_equal '1', Format[].write(1.0)
|
11
|
+
assert_equal '1.00', Format[Rounding[precision: 3]].write(1.0)
|
12
|
+
assert_equal '1.000', Format[Rounding[places: 3]].write(1.0)
|
13
|
+
assert_equal '1234567.1234', Format[rounding: :short].write(1234567.1234)
|
14
|
+
assert_equal '1234567.123', Format[Rounding[places: 3]].write(1234567.1234)
|
15
|
+
grouping = Format::Symbols[grouping: [3]]
|
16
|
+
assert_equal '1,234,567.123', Format[Rounding[places: 3], grouping].write(1234567.1234)
|
17
|
+
|
18
|
+
assert_equal '0.1', Format[].write(0.1)
|
19
|
+
assert_equal '0.1', Format[rounding: :short].write(0.1)
|
20
|
+
|
21
|
+
unless Float::RADIX == 2 && Float::MANT_DIG == 53
|
22
|
+
skip "Non IEEE Float unsupported for some tests"
|
23
|
+
return
|
24
|
+
end
|
25
|
+
|
26
|
+
assert_equal '0', Format[rounding: :short].write(0.0)
|
27
|
+
assert_equal '0e-17', Format[rounding: :free].write(0.0)
|
28
|
+
|
29
|
+
assert_equal '0.10000000000000001', Format[rounding: :free].write(0.1)
|
30
|
+
assert_equal '0.100', Format[Rounding[precision: 3]].write(0.1)
|
31
|
+
assert_equal '0.1000000000000000', Format[Rounding[precision: 16]].write(0.1)
|
32
|
+
assert_equal '0.10000000000000001', Format[Rounding[precision: 17]].write(0.1)
|
33
|
+
assert_equal '0.10000000000000001', Format[Rounding[precision: 18]].write(0.1)
|
34
|
+
|
35
|
+
assert_equal '0.1000000000000000055511151231257827021181583404541015625',
|
36
|
+
Format[Rounding[:short], exact_input: true].write(0.1)
|
37
|
+
|
38
|
+
assert_equal '0.1000000000000000055511151231257827021181583404541015625',
|
39
|
+
Format[Rounding[:free], exact_input: true].write(0.1)
|
40
|
+
|
41
|
+
assert_equal '0.100000000000000005551115123125782702118158340454101562500000',
|
42
|
+
Format[Rounding[precision:60], exact_input: true].write(0.1)
|
43
|
+
|
44
|
+
assert_equal '0.100000000000000005551115123125782702118158340454101562',
|
45
|
+
Format[Rounding[precision:54], exact_input: true].write(0.1)
|
46
|
+
assert_equal '0.100000000000000005551115123125782702118158340454101563',
|
47
|
+
Format[Rounding[:half_up, precision:54], exact_input: true].write(0.1)
|
48
|
+
assert_equal '0.100000000000000006', Format[Rounding[precision:18], exact_input: true].write(0.1)
|
49
|
+
assert_equal '0.10000000000000001', Format[Rounding[precision:17], exact_input: true].write(0.1)
|
50
|
+
assert_equal '0.1000000000000000', Format[Rounding[precision:16], exact_input: true].write(0.1)
|
51
|
+
assert_equal '0.100', Format[Rounding[precision: 3], exact_input: true].write(0.1)
|
52
|
+
|
53
|
+
fmt = Format[:exact_input]
|
54
|
+
assert_equal "64.099999999999994315658113919198513031005859375", fmt.write(64.1)
|
55
|
+
assert_equal '0.5', fmt.write(0.5)
|
56
|
+
assert_equal "0.333333333333333314829616256247390992939472198486328125", fmt.write(1.0/3.0)
|
57
|
+
assert_equal "0.66666666666666662965923251249478198587894439697265625", fmt.write(2.0/3.0)
|
58
|
+
assert_equal "-0.333333333333333314829616256247390992939472198486328125", fmt.write(-1.0/3.0)
|
59
|
+
assert_equal "-0.66666666666666662965923251249478198587894439697265625", fmt.write(-2.0/3.0)
|
60
|
+
assert_equal "1267650600228229401496703205376", fmt.write(2.0**100)
|
61
|
+
assert_equal "0.10000000000000001942890293094023945741355419158935546875", fmt.write(Float.context.next_plus(0.1))
|
62
|
+
assert_equal "1023.9999999999998863131622783839702606201171875", fmt.write(Float.context.next_minus(1024))
|
63
|
+
|
64
|
+
assert_equal "2.225073858507201383090232717332404064219215980462331830553327416887204434813918195854283159012511020564067339731035811005152434161553460108856012385377718821130777993532002330479610147442583636071921565046942503734208375250806650616658158948720491179968591639648500635908770118304874799780887753749949451580451605050915399856582470818645113537935804992115981085766051992433352114352390148795699609591288891602992641511063466313393663477586513029371762047325631781485664350872122828637642044846811407613911477062801689853244110024161447421618567166150540154285084716752901903161322778896729707373123334086988983175067838846926092773977972858659654941091369095406136467568702398678315290680984617210924625396728515625e-308",
|
65
|
+
fmt.write(Float.context.minimum_normal)
|
66
|
+
assert_equal "2.2250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375e-308",
|
67
|
+
fmt.write(Float.context.maximum_subnormal)
|
68
|
+
assert_equal "2.225073858507200394958941034839315711081630244019587100433722188237675583642553194503268618595007289964394616459051051412023043270117998255542591673498126023581185971968246077878183766819774580380287229348978296356771103136809189170558146173902184049999817014701706089569539838241444028984739501272818269238398287937541863482503350197395249647392622007205322474852963190178391854932391064931720791430455764953943127215325436859833344767109289929102154994338687742727610729450624487971196675896144263447425089844325111161570498002959146187656616550482084690619235135756396957006047593447154776156167693340095043268338435252390549256952840748419828640113148805198563919935252207510837343961185884248936392555587988206944151446491086954182492263498716056346893310546875e-308",
|
69
|
+
fmt.write(Float.context.next_minus(Float.context.maximum_subnormal))
|
70
|
+
assert_equal "9.88131291682493088353137585736442744730119605228649528851171365001351014540417503730599672723271984759593129390891435461853313420711879592797549592021563756252601426380622809055691634335697964207377437272113997461446100012774818307129968774624946794546339230280063430770796148252477131182342053317113373536374079120621249863890543182984910658610913088802254960259419999083863978818160833126649049514295738029453560318710477223100269607052986944038758053621421498340666445368950667144166486387218476578691673612021202301233961950615668455463665849580996504946155275185449574931216955640746893939906729403594535543517025132110239826300978220290207572547633450191167477946719798732961988232841140527418055848553508913045817507736501283943653106689453125e-324",
|
71
|
+
fmt.write(Float.context.next_plus(Float.context.minimum_nonzero))
|
72
|
+
assert_equal "4.940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625e-324",
|
73
|
+
fmt.write(Float.context.minimum_nonzero)
|
74
|
+
|
75
|
+
|
76
|
+
assert_equal '0', Format[:short].write(0.0)
|
77
|
+
assert_equal '0e-17', Format[:free].write(0.0)
|
78
|
+
|
79
|
+
assert_equal '1', Format[:short].write(1.0)
|
80
|
+
assert_equal '1.0000000000000000', Format[:free].write(1.0)
|
81
|
+
|
82
|
+
assert_equal '1.23', Format[:short].write(1.23)
|
83
|
+
assert_equal '1.2300000000000000', Format[:free].write(1.23)
|
84
|
+
assert_equal '1.229999999999999982236431605997495353221893310546875',
|
85
|
+
Format[:exact_input].write(1.23)
|
86
|
+
|
87
|
+
assert_equal '0.1', Format[:short].write(0.1)
|
88
|
+
assert_equal '0.10000000000000001', Format[:free].write(0.1)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_write_decnum_dec
|
92
|
+
assert_equal '1', Format[rounding: :short].write(Flt::DecNum('1.0'))
|
93
|
+
assert_equal '1.0', Format[rounding: :free].write(Flt::DecNum('1.0'))
|
94
|
+
assert_equal '1', Format[].write(Flt::DecNum('1.0'))
|
95
|
+
|
96
|
+
assert_equal '1', Format[rounding: :short].write(Flt::DecNum('1.00'))
|
97
|
+
assert_equal '1.00', Format[rounding: :free].write(Flt::DecNum('1.00'))
|
98
|
+
assert_equal '1', Format[].write(Flt::DecNum('1.00'))
|
99
|
+
|
100
|
+
# Note that insignificant digits are not shown by default
|
101
|
+
# (note also the 'additional' 0 is not insignificant, since it could only
|
102
|
+
# be 0-5 without altering the rounded value 1.00)
|
103
|
+
assert_equal '1.000', Format[Rounding[precision: 10]].write(Flt::DecNum('1.00'))
|
104
|
+
assert_equal '1.000', Format[Rounding[places: 5]].write(Flt::DecNum('1.00'))
|
105
|
+
|
106
|
+
assert_equal '1.000', Format[Rounding[precision: 4]].write(Flt::DecNum('1.00'))
|
107
|
+
assert_equal '1.00', Format[Rounding[precision: 3]].write(Flt::DecNum('1.00'))
|
108
|
+
assert_equal '1.0', Format[Rounding[precision: 2]].write(Flt::DecNum('1.00'))
|
109
|
+
assert_equal '1.000', Format[Rounding[places: 3]].write(Flt::DecNum('1.00'))
|
110
|
+
assert_equal '1.00', Format[Rounding[places: 2]].write(Flt::DecNum('1.00'))
|
111
|
+
assert_equal '1.0', Format[Rounding[places: 1]].write(Flt::DecNum('1.00'))
|
112
|
+
|
113
|
+
|
114
|
+
assert_equal '1.000000000', Format[Rounding[precision: 10], exact_input: true].write(Flt::DecNum('1.00'))
|
115
|
+
assert_equal '1.00000', Format[Rounding[places: 5], exact_input: true].write(Flt::DecNum('1.00'))
|
116
|
+
|
117
|
+
assert_equal '1.000', Format[Rounding[precision: 4], exact_input: true].write(Flt::DecNum('1.00'))
|
118
|
+
assert_equal '1.00', Format[Rounding[precision: 3], exact_input: true].write(Flt::DecNum('1.00'))
|
119
|
+
assert_equal '1.0', Format[Rounding[precision: 2], exact_input: true].write(Flt::DecNum('1.00'))
|
120
|
+
assert_equal '1.000', Format[Rounding[places: 3], exact_input: true].write(Flt::DecNum('1.00'))
|
121
|
+
assert_equal '1.00', Format[Rounding[places: 2], exact_input: true].write(Flt::DecNum('1.00'))
|
122
|
+
assert_equal '1.0', Format[Rounding[places: 1], exact_input: true].write(Flt::DecNum('1.00'))
|
123
|
+
|
124
|
+
|
125
|
+
assert_equal '1.00', Format[Rounding[precision: 3], exact_input: true].write(Flt::DecNum('1.0'))
|
126
|
+
assert_equal '1.000', Format[Rounding[places: 3], exact_input: true].write(Flt::DecNum('1.0'))
|
127
|
+
|
128
|
+
assert_equal '1234567.1234', Format[rounding: :short].write(Flt::DecNum('1234567.1234'))
|
129
|
+
|
130
|
+
assert_equal '1234567.123', Format[Rounding[places: 3]].write(Flt::DecNum('1234567.1234'))
|
131
|
+
grouping = Format::Symbols[grouping: [3]]
|
132
|
+
assert_equal '1,234,567.123', Format[Rounding[places: 3], grouping].write(Flt::DecNum('1234567.1234'))
|
133
|
+
|
134
|
+
assert_equal '0', Format[rounding: :short].write(Flt::DecNum('0'))
|
135
|
+
assert_equal '0', Format[rounding: :short].write(Flt::DecNum('0.0'))
|
136
|
+
assert_equal '0', Format[rounding: :short].write(Flt::DecNum('0.0000'))
|
137
|
+
assert_equal '0', Format[rounding: :short].write(Flt::DecNum('0E-15'))
|
138
|
+
|
139
|
+
assert_equal '0', Format[rounding: :free].write(Flt::DecNum('0'))
|
140
|
+
assert_equal '0.0', Format[rounding: :free].write(Flt::DecNum('0.0'))
|
141
|
+
assert_equal '0.0000', Format[rounding: :free].write(Flt::DecNum('0.0000'))
|
142
|
+
assert_equal '0e-15', Format[rounding: :free].write(Flt::DecNum('0E-15'))
|
143
|
+
|
144
|
+
assert_equal '0.1', Format[:short].write(Flt::DecNum('0.100'))
|
145
|
+
assert_equal '0.100', Format[:free].write(Flt::DecNum('0.100'))
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
def test_write_binnum_dec
|
150
|
+
context = Flt::BinNum::IEEEDoubleContext
|
151
|
+
x = Flt::BinNum('1.0', :fixed, context: context)
|
152
|
+
assert_equal '1', Format[rounding: :short].write(x)
|
153
|
+
assert_equal '1', Format[].write(x)
|
154
|
+
assert_equal '1.00', Format[Rounding[precision: 3]].write(x)
|
155
|
+
assert_equal '1.000', Format[Rounding[places: 3]].write(x)
|
156
|
+
x = Flt::BinNum('1234567.1234', :fixed, context: context)
|
157
|
+
assert_equal '1234567.1234', Format[rounding: :short].write(x)
|
158
|
+
assert_equal '1234567.123', Format[Rounding[places: 3]].write(x)
|
159
|
+
grouping = Format::Symbols[grouping: [3]]
|
160
|
+
assert_equal '1,234,567.123', Format[Rounding[places: 3], grouping].write(x)
|
161
|
+
|
162
|
+
unless Float::RADIX == 2 && Float::MANT_DIG == 53
|
163
|
+
skip "Non IEEE Float unsupported for some tests"
|
164
|
+
return
|
165
|
+
end
|
166
|
+
|
167
|
+
x = Flt::BinNum('0.1', :fixed, context: context)
|
168
|
+
assert_equal '0.1', Format[].write(x)
|
169
|
+
assert_equal '0.1', Format[rounding: :short].write(x)
|
170
|
+
assert_equal '0.100', Format[Rounding[precision: 3]].write(x)
|
171
|
+
assert_equal '0.1000000000000000', Format[Rounding[precision: 16]].write(x)
|
172
|
+
assert_equal '0.10000000000000001', Format[Rounding[precision: 17]].write(x)
|
173
|
+
assert_equal '0.10000000000000001', Format[Rounding[precision: 18]].write(x)
|
174
|
+
|
175
|
+
assert_equal '0.1000000000000000055511151231257827021181583404541015625',
|
176
|
+
Format[Rounding[:short], exact_input: true].write(x)
|
177
|
+
|
178
|
+
assert_equal '0.1000000000000000055511151231257827021181583404541015625',
|
179
|
+
Format[Rounding[:free], exact_input: true].write(x)
|
180
|
+
|
181
|
+
assert_equal '0.100000000000000005551115123125782702118158340454101562500000',
|
182
|
+
Format[Rounding[precision:60], exact_input: true].write(x)
|
183
|
+
|
184
|
+
assert_equal '0.100000000000000005551115123125782702118158340454101562',
|
185
|
+
Format[Rounding[precision:54], exact_input: true].write(x)
|
186
|
+
assert_equal '0.100000000000000005551115123125782702118158340454101563',
|
187
|
+
Format[Rounding[:half_up, precision:54], exact_input: true].write(x)
|
188
|
+
assert_equal '0.100000000000000006', Format[Rounding[precision:18], exact_input: true].write(x)
|
189
|
+
assert_equal '0.10000000000000001', Format[Rounding[precision:17], exact_input: true].write(x)
|
190
|
+
assert_equal '0.1000000000000000', Format[Rounding[precision:16], exact_input: true].write(x)
|
191
|
+
assert_equal '0.100', Format[Rounding[precision: 3], exact_input: true].write(x)
|
192
|
+
end
|
193
|
+
|
194
|
+
|
195
|
+
def test_write_decnum_bin
|
196
|
+
sym = Format::Symbols[repeat_delimited: true]
|
197
|
+
assert_equal '0.0<0011>', Format[sym, exact_input: true, base: 2].write(Flt::DecNum('0.1'))
|
198
|
+
end
|
199
|
+
|
200
|
+
|
201
|
+
def test_write_rational_dec
|
202
|
+
assert_equal '0.333...', Format[].write(Rational(1,3))
|
203
|
+
sym = Format::Symbols[repeat_delimited: true]
|
204
|
+
assert_equal '0.<3>', Format[sym].write(Rational(1,3))
|
205
|
+
assert_equal '0.0<0011>', Format[sym, base: 2].write(Rational(1,10))
|
206
|
+
end
|
207
|
+
|
208
|
+
|
209
|
+
def test_write_float_bin
|
210
|
+
x = 0.1
|
211
|
+
assert_equal '0.0001100110011001100110011001100110011001100110011001101',
|
212
|
+
Format[base: 2, rounding: :short].write(x)
|
213
|
+
|
214
|
+
assert_equal '0.00011001100110011001100110011001100110011001100110011010',
|
215
|
+
Format[base: 2, rounding: :free].write(x)
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_write_binnum_bin
|
219
|
+
context = Flt::BinNum::IEEEDoubleContext
|
220
|
+
x = Flt::BinNum('0.1', :fixed, context: context)
|
221
|
+
assert_equal '0.0001100110011001100110011001100110011001100110011001101',
|
222
|
+
Format[base: 2, rounding: :short].write(x)
|
223
|
+
|
224
|
+
assert_equal '0.00011001100110011001100110011001100110011001100110011010',
|
225
|
+
Format[base: 2, rounding: :free].write(x)
|
226
|
+
end
|
227
|
+
|
228
|
+
|
229
|
+
def test_write_binnum_hex
|
230
|
+
context = Flt::BinNum::IEEEDoubleContext
|
231
|
+
x = Flt::BinNum('0.1', :fixed, context: context)
|
232
|
+
assert_equal "1.999999999999Ap-4", Format[:hexbin].write(x)
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_write_to_file
|
236
|
+
file = Tempfile.new('numerals')
|
237
|
+
Format[Rounding[places: 3]].write(1.0, output: file)
|
238
|
+
file.close
|
239
|
+
assert_equal '1.000', File.read(file.path)
|
240
|
+
file.unlink
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_modes
|
244
|
+
f = Format[mode: [:general, max_leading: 3, max_trailing: 0], symbols: [uppercase: true]]
|
245
|
+
assert_equal '123.45', f.write(Flt::DecNum('123.45'))
|
246
|
+
assert_equal '0.00012345', f.write(Flt::DecNum('0.00012345'))
|
247
|
+
assert_equal '1.2345E-5', f.write(Flt::DecNum('0.000012345'))
|
248
|
+
assert_equal '1.2345E-6', f.write(Flt::DecNum('0.0000012345'))
|
249
|
+
assert_equal '1234.5', f.write(Flt::DecNum('1234.5'))
|
250
|
+
assert_equal '12345', f.write(Flt::DecNum('12345.0'))
|
251
|
+
assert_equal '1.2345E5', f.write(Flt::DecNum('12345E1'))
|
252
|
+
assert_equal '1.2345E6', f.write(Flt::DecNum('12345E2'))
|
253
|
+
|
254
|
+
f = Format[mode: [:scientific, max_leading: 3, max_trailing: 0], symbols: [uppercase: true]]
|
255
|
+
assert_equal '1.2345E2', f.write(Flt::DecNum('123.45'))
|
256
|
+
assert_equal '1.2345E-4', f.write(Flt::DecNum('0.00012345'))
|
257
|
+
assert_equal '1.2345E-5', f.write(Flt::DecNum('0.000012345'))
|
258
|
+
assert_equal '1.2345E-6', f.write(Flt::DecNum('0.0000012345'))
|
259
|
+
assert_equal '1.2345E3', f.write(Flt::DecNum('1234.5'))
|
260
|
+
assert_equal '1.2345E4', f.write(Flt::DecNum('12345.0'))
|
261
|
+
assert_equal '1.2345E5', f.write(Flt::DecNum('12345E1'))
|
262
|
+
assert_equal '1.2345E6', f.write(Flt::DecNum('12345E2'))
|
263
|
+
|
264
|
+
f = Format[mode: [:fixed, max_leading: 3, max_trailing: 0], symbols: [uppercase: true]]
|
265
|
+
assert_equal '123.45', f.write(Flt::DecNum('123.45'))
|
266
|
+
assert_equal '0.00012345', f.write(Flt::DecNum('0.00012345'))
|
267
|
+
assert_equal '0.000012345', f.write(Flt::DecNum('0.000012345'))
|
268
|
+
assert_equal '0.0000012345', f.write(Flt::DecNum('0.0000012345'))
|
269
|
+
assert_equal '1234.5', f.write(Flt::DecNum('1234.5'))
|
270
|
+
assert_equal '12345', f.write(Flt::DecNum('12345.0'))
|
271
|
+
assert_equal '123450', f.write(Flt::DecNum('12345E1'))
|
272
|
+
assert_equal '1234500', f.write(Flt::DecNum('12345E2'))
|
273
|
+
|
274
|
+
f = Format[mode: :engineering, symbols: [uppercase: true]]
|
275
|
+
assert_equal '123.45E0', f.write(Flt::DecNum('123.45'))
|
276
|
+
assert_equal '123.45E-6', f.write(Flt::DecNum('0.00012345'))
|
277
|
+
assert_equal '12.345E-6', f.write(Flt::DecNum('0.000012345'))
|
278
|
+
assert_equal '1.2345E-6', f.write(Flt::DecNum('0.0000012345'))
|
279
|
+
assert_equal '1.2345E3', f.write(Flt::DecNum('1234.5'))
|
280
|
+
assert_equal '12.345E3', f.write(Flt::DecNum('12345.0'))
|
281
|
+
assert_equal '123.45E3', f.write(Flt::DecNum('12345E1'))
|
282
|
+
assert_equal '1.2345E6', f.write(Flt::DecNum('12345E2'))
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_insignificant_fractional
|
286
|
+
fmt = Format[symbols: [insignificant_digit: '?']]
|
287
|
+
|
288
|
+
assert_equal '1.000??????', fmt[Rounding[precision: 10]].write(Flt::DecNum('1.00'))
|
289
|
+
assert_equal '1.000??', fmt[Rounding[places: 5]].write(Flt::DecNum('1.00'))
|
290
|
+
assert_equal '1.000', fmt[Rounding[precision: 4]].write(Flt::DecNum('1.00'))
|
291
|
+
assert_equal '1.00', fmt[Rounding[precision: 3]].write(Flt::DecNum('1.00'))
|
292
|
+
|
293
|
+
assert_equal '1.000??', fmt[Rounding[places: 5]].write(Flt::DecNum('1.00'))
|
294
|
+
assert_equal '1.000?', fmt[Rounding[places: 4]].write(Flt::DecNum('1.00'))
|
295
|
+
assert_equal '1.000', fmt[Rounding[places: 3]].write(Flt::DecNum('1.00'))
|
296
|
+
assert_equal '1.00', fmt[Rounding[places: 2]].write(Flt::DecNum('1.00'))
|
297
|
+
assert_equal '1.0', fmt[Rounding[places: 1]].write(Flt::DecNum('1.00'))
|
298
|
+
|
299
|
+
assert_equal '1.000?', fmt[Rounding[precision: 5]].write(Flt::DecNum('1.00'))
|
300
|
+
assert_equal '1.000', fmt[Rounding[precision: 4]].write(Flt::DecNum('1.00'))
|
301
|
+
assert_equal '1.00', fmt[Rounding[precision: 3]].write(Flt::DecNum('1.00'))
|
302
|
+
assert_equal '1.0', fmt[Rounding[precision: 2]].write(Flt::DecNum('1.00'))
|
303
|
+
assert_equal '1.000', fmt[Rounding[places: 3]].write(Flt::DecNum('1.00'))
|
304
|
+
assert_equal '1.00', fmt[Rounding[places: 2]].write(Flt::DecNum('1.00'))
|
305
|
+
assert_equal '1.0', fmt[Rounding[places: 1]].write(Flt::DecNum('1.00'))
|
306
|
+
|
307
|
+
fmt = Format[symbols: [insignificant_digit: 0]]
|
308
|
+
|
309
|
+
assert_equal '1.000000000', fmt[Rounding[precision: 10]].write(Flt::DecNum('1.00'))
|
310
|
+
assert_equal '1.00000', fmt[Rounding[places: 5]].write(Flt::DecNum('1.00'))
|
311
|
+
assert_equal '1.000', fmt[Rounding[precision: 4]].write(Flt::DecNum('1.00'))
|
312
|
+
assert_equal '1.00', fmt[Rounding[precision: 3]].write(Flt::DecNum('1.00'))
|
313
|
+
|
314
|
+
assert_equal '1.00000', fmt[Rounding[places: 5]].write(Flt::DecNum('1.00'))
|
315
|
+
assert_equal '1.0000', fmt[Rounding[places: 4]].write(Flt::DecNum('1.00'))
|
316
|
+
assert_equal '1.000', fmt[Rounding[places: 3]].write(Flt::DecNum('1.00'))
|
317
|
+
assert_equal '1.00', fmt[Rounding[places: 2]].write(Flt::DecNum('1.00'))
|
318
|
+
assert_equal '1.0', fmt[Rounding[places: 1]].write(Flt::DecNum('1.00'))
|
319
|
+
|
320
|
+
assert_equal '1.0000', fmt[Rounding[precision: 5]].write(Flt::DecNum('1.00'))
|
321
|
+
assert_equal '1.000', fmt[Rounding[precision: 4]].write(Flt::DecNum('1.00'))
|
322
|
+
assert_equal '1.00', fmt[Rounding[precision: 3]].write(Flt::DecNum('1.00'))
|
323
|
+
assert_equal '1.0', fmt[Rounding[precision: 2]].write(Flt::DecNum('1.00'))
|
324
|
+
assert_equal '1.000', fmt[Rounding[places: 3]].write(Flt::DecNum('1.00'))
|
325
|
+
assert_equal '1.00', fmt[Rounding[places: 2]].write(Flt::DecNum('1.00'))
|
326
|
+
assert_equal '1.0', fmt[Rounding[places: 1]].write(Flt::DecNum('1.00'))
|
327
|
+
|
328
|
+
fmt = Format[symbols: [insignificant_digit: '?']]
|
329
|
+
context = Flt::BinNum::IEEEDoubleContext
|
330
|
+
x = Flt::BinNum('0.1', :fixed, context: context)
|
331
|
+
assert_equal '0.100000000000000', fmt[rounding: [precision: 15]].write(x)
|
332
|
+
assert_equal '0.1000000000000000', fmt[rounding: [precision: 16]].write(x)
|
333
|
+
assert_equal '0.10000000000000001', fmt[rounding: [precision: 17]].write(x)
|
334
|
+
assert_equal '0.10000000000000001?', fmt[rounding: [precision: 18]].write(x)
|
335
|
+
assert_equal '0.10000000000000001??', fmt[rounding: [precision: 19]].write(x)
|
336
|
+
assert_equal '0.10000000000000001???', fmt[rounding: [precision: 20]].write(x)
|
337
|
+
|
338
|
+
fmt = fmt[exact_input: true]
|
339
|
+
assert_equal '0.100000000000000', fmt[rounding: [precision: 15]].write(x)
|
340
|
+
assert_equal '0.1000000000000000', fmt[rounding: [precision: 16]].write(x)
|
341
|
+
assert_equal '0.10000000000000001', fmt[rounding: [precision: 17]].write(x)
|
342
|
+
assert_equal '0.100000000000000006', fmt[rounding: [precision: 18]].write(x)
|
343
|
+
assert_equal '0.1000000000000000056', fmt[rounding: [precision: 19]].write(x)
|
344
|
+
assert_equal '0.10000000000000000555', fmt[rounding: [precision: 20]].write(x)
|
345
|
+
end
|
346
|
+
|
347
|
+
def test_insignificant_integral
|
348
|
+
fmt = Format[mode: :fixed]
|
349
|
+
x = Flt::DecNum('1234E5')
|
350
|
+
assert_equal '123400000', fmt[rounding: [precision: 7]].write(x)
|
351
|
+
assert_equal '123400000', fmt[rounding: [precision: 15]].write(x)
|
352
|
+
fmt = fmt[symbols: [insignificant_digit: '?']]
|
353
|
+
assert_equal '12340????.??????', fmt[rounding: [precision: 15]].write(x)
|
354
|
+
assert_equal '12340????', fmt[rounding: [precision: 7]].write(x)
|
355
|
+
fmt = fmt[symbols: [grouping: [3]]]
|
356
|
+
assert_equal '123,40?,???.??????', fmt[rounding: [precision: 15]].write(x)
|
357
|
+
end
|
358
|
+
|
359
|
+
def test_basic_fmt
|
360
|
+
fmt = Format[]
|
361
|
+
assert_equal "0", fmt.write(0.0)
|
362
|
+
assert_equal "0", fmt.write(0)
|
363
|
+
assert_equal "0", fmt.write(BigDecimal('0'))
|
364
|
+
assert_equal "0", fmt.write(Rational(0,1))
|
365
|
+
|
366
|
+
assert_equal "123456789", fmt.write(123456789.0)
|
367
|
+
assert_equal "123456789", fmt.write(123456789)
|
368
|
+
assert_equal "123456789", fmt.write(BigDecimal('123456789'))
|
369
|
+
assert_equal "123456789", fmt.write(Rational(123456789,1))
|
370
|
+
assert_equal "123456789.25", fmt.write(123456789.25)
|
371
|
+
assert_equal "123456789.25", fmt.write(BigDecimal('123456789.25'))
|
372
|
+
assert_equal "123456789.25", fmt.write((Rational(123456789)+Rational(1,4)))
|
373
|
+
|
374
|
+
assert_equal '0.25', Format[:short].write(Rational(1,4))
|
375
|
+
assert_equal '0.25', Format[:free].write(Rational(1,4))
|
376
|
+
|
377
|
+
fmt = Format[symbols: [repeat_delimited: true]]
|
378
|
+
assert_equal '1.10e14', fmt[2, precision: 3].write(23433)
|
379
|
+
assert_equal '1.011100e14', fmt[2, precision: 7].write(23433)
|
380
|
+
assert_equal '101101110001001', fmt[2].write(23433)
|
381
|
+
assert_equal '12.<2>', fmt[4].write(Rational(20,3))
|
382
|
+
assert_equal '3.<3>', fmt[:short].write(Rational(10,3))
|
383
|
+
assert_equal '3.<3>', fmt[:free].write(Rational(10,3))
|
384
|
+
assert_equal '0.3', fmt[:short].write(0.3)
|
385
|
+
assert_equal '0.29999999999999999', fmt[:free].write(0.3)
|
386
|
+
assert_equal '0.299999999999999988897769753748434595763683319091796875',
|
387
|
+
fmt[:exact_input].write(0.3)
|
388
|
+
end
|
389
|
+
|
390
|
+
def test_optional_mode_prec_parameters
|
391
|
+
x = 0.1
|
392
|
+
fmt = Format[]
|
393
|
+
assert_equal '0.1000000000', fmt[rounding: [precision: 10]].write(x)
|
394
|
+
assert_equal '1.000000000e-1', fmt[rounding: [precision: 10], mode: :scientific].write(x)
|
395
|
+
assert_equal '0.1', fmt[rounding: :short] .write(x)
|
396
|
+
assert_equal '0.10000', fmt[rounding: [precision: 5]].write(x)
|
397
|
+
assert_equal '1.0000e-1', fmt[rounding: [precision: 5], mode: :scientific].write(x)
|
398
|
+
end
|
399
|
+
|
400
|
+
|
401
|
+
def test_float_nonsig
|
402
|
+
fmt = Format[symbols: [insignificant_digit: '#', uppercase: true], exact_input: false]
|
403
|
+
assert_equal "100.000000000000000##", fmt[mode: :fixed, rounding: [precision: 20]].write(100.0)
|
404
|
+
assert_equal "100.000000000000000#####", fmt[mode: :fixed, rounding: [places: 20]].write(100.0)
|
405
|
+
|
406
|
+
unless Float::RADIX == 2 && Float::MANT_DIG == 53
|
407
|
+
skip "Non IEEE Float unsupported for some tests"
|
408
|
+
return
|
409
|
+
end
|
410
|
+
|
411
|
+
fmt = fmt[mode: :scientific, rounding: [precision: 20]]
|
412
|
+
assert_equal "3.3333333333333331###E-1", fmt.write(1.0/3)
|
413
|
+
assert_equal "3.3333333333333335###E6", fmt.write(1E7/3)
|
414
|
+
assert_equal "3.3333333333333334###E-8", fmt.write(1E-7/3)
|
415
|
+
assert_equal "3.3333333333333333333E-1", fmt.write(Rational(1,3))
|
416
|
+
assert_equal "3.3333333333333331###E-1", fmt[mode: [sci_int_digits: 1]].write(1.0/3)
|
417
|
+
assert_equal "33333333333333331###E-20", fmt[mode: [sci_int_digits: :all]].write(1.0/3)
|
418
|
+
assert_equal "33333333333333331###.E-20", fmt[mode: [sci_int_digits: :all], symbols: [show_point: true]].write(1.0/3)
|
419
|
+
assert_equal "33333333333333333333E-20", fmt[mode: [sci_int_digits: :all]].write(Rational(1,3))
|
420
|
+
|
421
|
+
fmt = fmt[mode: [sci_int_digits: :eng]]
|
422
|
+
assert_equal "333.33333333333331###E-3", fmt.write(1.0/3)
|
423
|
+
assert_equal "3.3333333333333335###E6", fmt.write(1E7/3)
|
424
|
+
assert_equal "33.333333333333334###E-9",fmt.write(1E-7/3)
|
425
|
+
|
426
|
+
fmt = fmt[symbols: [point: ','], mode: [:scientific, sci_int_digits: 0], rounding: [precision: 20]]
|
427
|
+
assert_equal "0,33333333333333331###E0",fmt.write(1.0/3)
|
428
|
+
assert_equal "0,33333333333333335###E7",fmt.write(1E7/3)
|
429
|
+
assert_equal "0,33333333333333334###E-7",fmt.write(1E-7/3)
|
430
|
+
|
431
|
+
fmt = fmt[mode: [sci_int_digits: 0], symbols: [point: '.']]
|
432
|
+
assert_equal "0.10000000000000001###E0",fmt.write(1E-1)
|
433
|
+
assert_equal "0.50000000000000000###E0",fmt.write(0.5)
|
434
|
+
assert_equal "0.49999999999999994###E0",fmt.write(Float.context.next_minus(0.5))
|
435
|
+
assert_equal "0.50000000000000011###E0",fmt.write(Float.context.next_plus(0.5))
|
436
|
+
assert_equal "0.22250738585072014###E-307",fmt.write(Float.context.minimum_normal)
|
437
|
+
assert_equal "0.22250738585072009###E-307",fmt.write(Float.context.maximum_subnormal)
|
438
|
+
assert_equal "0.5###################E-323",fmt.write(Float.context.minimum_nonzero)
|
439
|
+
assert_equal "0.64000000000000000###E2",fmt.write(64.0)
|
440
|
+
assert_equal "0.6400000000000001####E2",fmt.write(Float.context.next_plus(64.0))
|
441
|
+
assert_equal "0.6409999999999999####E2",fmt.write(64.1)
|
442
|
+
assert_equal "0.6412312300000001####E2",fmt.write(64.123123)
|
443
|
+
assert_equal "0.10000000000000001###E0",fmt.write(0.1)
|
444
|
+
assert_equal "0.6338253001141148####E30", fmt.write(Float.context.next_plus(Math.ldexp(0.5,100)))
|
445
|
+
assert_equal "0.39443045261050599###E-30",fmt.write(Float.context.next_plus(Math.ldexp(0.5,-100)))
|
446
|
+
assert_equal "0.10##################E-322",fmt.write(Float.context.next_plus(Float.context.minimum_nonzero))
|
447
|
+
assert_equal "0.15##################E-322",fmt.write(Float.context.next_plus(Float.context.next_plus(Float.context.minimum_nonzero)))
|
448
|
+
end
|
449
|
+
|
450
|
+
def test_flt_equidistant_nearest
|
451
|
+
# In IEEEDoubleContext
|
452
|
+
# 1E23 is equidistant from 2 Floats: lo & hi
|
453
|
+
# one or the other will be chosen based on the rounding mode
|
454
|
+
|
455
|
+
context = Flt::BinNum::IEEEDoubleContext
|
456
|
+
|
457
|
+
lo = hi = nil
|
458
|
+
Flt::BinNum.context(context) do
|
459
|
+
lo = Flt::BinNum('0x1.52d02c7e14af6p+76', :fixed) # 9.999999999999999E22
|
460
|
+
hi = Flt::BinNum('0x1.52d02c7e14af7p+76', :fixed) # 1.0000000000000001E23
|
461
|
+
end
|
462
|
+
|
463
|
+
fmt = Format[rounding: :short, exact_input: false, mode: :general, symbols: [uppercase: true]]
|
464
|
+
|
465
|
+
assert_equal "1E23", fmt[input_rounding: :half_down].write(lo)
|
466
|
+
assert_equal "9.999999999999999E22", fmt[input_rounding: :half_up].write(lo)
|
467
|
+
assert_equal "1E23", fmt[input_rounding: :half_even].write(lo)
|
468
|
+
|
469
|
+
assert_equal "1E23", fmt[input_rounding: :half_up].write(hi)
|
470
|
+
assert_equal "1.0000000000000001E23", fmt[input_rounding: :half_down].write(hi)
|
471
|
+
assert_equal "1.0000000000000001E23", fmt[input_rounding: :half_even].write(hi)
|
472
|
+
|
473
|
+
assert_equal "-1E23", fmt[input_rounding: :half_down].write(-lo)
|
474
|
+
assert_equal "-9.999999999999999E22", fmt[input_rounding: :half_up].write(-lo)
|
475
|
+
assert_equal "-1E23", fmt[input_rounding: :half_even].write(-lo)
|
476
|
+
|
477
|
+
assert_equal "-1E23", fmt[input_rounding: :half_up].write(-hi)
|
478
|
+
assert_equal "-1.0000000000000001E23", fmt[input_rounding: :half_down].write(-hi)
|
479
|
+
assert_equal "-1.0000000000000001E23", fmt[input_rounding: :half_even].write(-hi)
|
480
|
+
|
481
|
+
Flt::BinNum.context(context, rounding: :half_down) do
|
482
|
+
assert_equal "1E23", fmt[input_rounding: :context].write(lo)
|
483
|
+
end
|
484
|
+
|
485
|
+
Flt::BinNum.context(context, rounding: :half_up) do
|
486
|
+
assert_equal "9.999999999999999E22", fmt[input_rounding: :context].write(lo)
|
487
|
+
end
|
488
|
+
|
489
|
+
Flt::BinNum.context(context, rounding: :half_even) do
|
490
|
+
assert_equal "1E23", fmt[input_rounding: :context].write(lo)
|
491
|
+
end
|
492
|
+
|
493
|
+
Flt::BinNum.context(context, rounding: :half_up) do
|
494
|
+
assert_equal "1E23", fmt[input_rounding: :context].write(hi)
|
495
|
+
end
|
496
|
+
|
497
|
+
Flt::BinNum.context(context, rounding: :half_down) do
|
498
|
+
assert_equal "1.0000000000000001E23", fmt[input_rounding: :context].write(hi)
|
499
|
+
end
|
500
|
+
|
501
|
+
Flt::BinNum.context(context, rounding: :half_even) do
|
502
|
+
assert_equal "1.0000000000000001E23", fmt[input_rounding: :context].write(hi)
|
503
|
+
end
|
504
|
+
|
505
|
+
Flt::BinNum.context(context, rounding: :half_down) do
|
506
|
+
assert_equal "-1E23", fmt[input_rounding: :context].write(-lo)
|
507
|
+
end
|
508
|
+
|
509
|
+
Flt::BinNum.context(context, rounding: :half_up) do
|
510
|
+
assert_equal "-9.999999999999999E22", fmt[input_rounding: :context].write(-lo)
|
511
|
+
end
|
512
|
+
|
513
|
+
Flt::BinNum.context(context, rounding: :half_even) do
|
514
|
+
assert_equal "-1E23", fmt[input_rounding: :context].write(-lo)
|
515
|
+
end
|
516
|
+
|
517
|
+
Flt::BinNum.context(context, rounding: :half_up) do
|
518
|
+
assert_equal "-1E23", fmt[input_rounding: :context].write(-hi)
|
519
|
+
end
|
520
|
+
|
521
|
+
Flt::BinNum.context(context, rounding: :half_down) do
|
522
|
+
assert_equal "-1.0000000000000001E23", fmt[input_rounding: :context].write(-hi)
|
523
|
+
end
|
524
|
+
|
525
|
+
Flt::BinNum.context(context, rounding: :half_even) do
|
526
|
+
assert_equal "-1.0000000000000001E23", fmt[input_rounding: :context].write(-hi)
|
527
|
+
end
|
528
|
+
|
529
|
+
assert_equal "1E23", fmt[rounding: :half_down].write(lo)
|
530
|
+
assert_equal "9.999999999999999E22", fmt[rounding: :half_up].write(lo)
|
531
|
+
assert_equal "1E23", fmt[rounding: :half_even].write(lo)
|
532
|
+
|
533
|
+
assert_equal "1E23", fmt[rounding: :half_up].write(hi)
|
534
|
+
assert_equal "1.0000000000000001E23", fmt[rounding: :half_down].write(hi)
|
535
|
+
assert_equal "1.0000000000000001E23", fmt[rounding: :half_even].write(hi)
|
536
|
+
|
537
|
+
assert_equal "-1E23", fmt[rounding: :half_down].write(-lo)
|
538
|
+
assert_equal "-9.999999999999999E22", fmt[rounding: :half_up].write(-lo)
|
539
|
+
assert_equal "-1E23", fmt[rounding: :half_even].write(-lo)
|
540
|
+
|
541
|
+
assert_equal "-1E23", fmt[rounding: :half_up].write(-hi)
|
542
|
+
assert_equal "-1.0000000000000001E23", fmt[rounding: :half_down].write(-hi)
|
543
|
+
assert_equal "-1.0000000000000001E23", fmt[rounding: :half_even].write(-hi)
|
544
|
+
|
545
|
+
end
|
546
|
+
|
547
|
+
def test_float_equidistiant_nearest
|
548
|
+
unless Float::RADIX == 2 && Float::MANT_DIG == 53
|
549
|
+
skip "Non IEEE Float unsupported for some tests"
|
550
|
+
return
|
551
|
+
end
|
552
|
+
|
553
|
+
context = Float.context
|
554
|
+
|
555
|
+
lo = Float('0x1.52d02c7e14af6p+76')
|
556
|
+
hi = Float('0x1.52d02c7e14af7p+76')
|
557
|
+
|
558
|
+
txt = '1E23'
|
559
|
+
txt_lo = '9.999999999999999E22'
|
560
|
+
txt_hi = '1.0000000000000001E23'
|
561
|
+
|
562
|
+
fmt = Format[rounding: :short, exact_input: false, mode: :general]
|
563
|
+
fmt = fmt[symbols: [uppercase: true]]
|
564
|
+
assert_equal txt, fmt[input_rounding: :half_down].write(lo)
|
565
|
+
assert_equal txt_lo, fmt[input_rounding: :half_up].write(lo)
|
566
|
+
assert_equal txt, fmt[input_rounding: :half_even].write(lo)
|
567
|
+
|
568
|
+
assert_equal txt, fmt[input_rounding: :half_up].write(hi)
|
569
|
+
assert_equal txt_hi, fmt[input_rounding: :half_down].write(hi)
|
570
|
+
assert_equal txt_hi, fmt[input_rounding: :half_even].write(hi)
|
571
|
+
|
572
|
+
assert_equal "-#{txt}", fmt[input_rounding: :half_down].write(-lo)
|
573
|
+
assert_equal "-#{txt_lo}", fmt[input_rounding: :half_up].write(-lo)
|
574
|
+
assert_equal "-#{txt}", fmt[input_rounding: :half_even].write(-lo)
|
575
|
+
|
576
|
+
assert_equal "-#{txt}", fmt[input_rounding: :half_up].write(-hi)
|
577
|
+
assert_equal "-#{txt_hi}", fmt[input_rounding: :half_down].write(-hi)
|
578
|
+
assert_equal "-#{txt_hi}", fmt[input_rounding: :half_even].write(-hi)
|
579
|
+
end
|
580
|
+
|
581
|
+
def test_flt_single_nearest
|
582
|
+
|
583
|
+
# In IEEEDoubleContext
|
584
|
+
# 64.1 between the floats lo, hi, but is closer to lo
|
585
|
+
# So there's a closet Float that should be chosen for rounding
|
586
|
+
|
587
|
+
context = Flt::BinNum::IEEEDoubleContext
|
588
|
+
|
589
|
+
lo = hi = nil
|
590
|
+
Flt::BinNum.context(context) do
|
591
|
+
lo = Flt::BinNum('0x1.0066666666666p+6', :fixed) # this is nearer to the 64.1 Float
|
592
|
+
hi = Flt::BinNum('0x1.0066666666667p+6', :fixed)
|
593
|
+
end
|
594
|
+
|
595
|
+
fmt = Format[mode: :general, rounding: :short, exact_input: true]
|
596
|
+
assert_equal '64.099999999999994315658113919198513031005859375', fmt.write(lo)
|
597
|
+
fmt = fmt[exact_input: false]
|
598
|
+
assert_equal "64.09999999999999", fmt[rounding: :free].write(lo)
|
599
|
+
assert_equal "64.1", fmt[rounding: :short].write(lo)
|
600
|
+
assert_equal "64.1", fmt[input_rounding: :half_even].write(lo)
|
601
|
+
assert_equal "64.1", fmt[input_rounding: :half_up].write(lo)
|
602
|
+
assert_equal "64.1", fmt[input_rounding: :half_down].write(lo)
|
603
|
+
assert_equal "64.09999999999999", fmt[input_rounding: :up].write(lo)
|
604
|
+
|
605
|
+
assert_equal "-64.09999999999999", fmt[rounding: :free].write(-lo)
|
606
|
+
assert_equal "-64.1", fmt[rounding: :short].write(-lo)
|
607
|
+
assert_equal "-64.1", fmt[input_rounding: :half_even].write(-lo)
|
608
|
+
assert_equal "-64.1", fmt[input_rounding: :half_up].write(-lo)
|
609
|
+
assert_equal "-64.1", fmt[input_rounding: :half_down].write(-lo)
|
610
|
+
assert_equal "-64.09999999999999", fmt[input_rounding: :up].write(-lo)
|
611
|
+
end
|
612
|
+
|
613
|
+
|
614
|
+
def test_float_single_nearest
|
615
|
+
unless Float::RADIX == 2 && Float::MANT_DIG == 53
|
616
|
+
skip "Non IEEE Float unsupported for some tests"
|
617
|
+
return
|
618
|
+
end
|
619
|
+
|
620
|
+
context = Float.context
|
621
|
+
|
622
|
+
lo = Float('0x1.0066666666666p+6') # this is nearer to the 64.1 Float
|
623
|
+
hi = Float('0x1.0066666666667p+6')
|
624
|
+
fmt = Format[mode: :general, rounding: :short, exact_input: true]
|
625
|
+
assert_equal '64.099999999999994315658113919198513031005859375', fmt.write(lo)
|
626
|
+
fmt = fmt[exact_input: false]
|
627
|
+
assert_equal "64.09999999999999", fmt[rounding: :free].write(lo)
|
628
|
+
assert_equal "64.1", fmt[rounding: :short].write(lo)
|
629
|
+
assert_equal "64.1", fmt[input_rounding: :half_even].write(lo)
|
630
|
+
assert_equal "64.1", fmt[input_rounding: :half_up].write(lo)
|
631
|
+
assert_equal "64.1", fmt[input_rounding: :half_down].write(lo)
|
632
|
+
assert_equal "64.09999999999999", fmt[input_rounding: :up].write(lo)
|
633
|
+
|
634
|
+
|
635
|
+
assert_equal "-64.09999999999999", fmt[rounding: :free].write(-lo)
|
636
|
+
assert_equal "-64.1", fmt[rounding: :short].write(-lo)
|
637
|
+
assert_equal "-64.1", fmt[input_rounding: :half_even].write(-lo)
|
638
|
+
assert_equal "-64.1", fmt[input_rounding: :half_up].write(-lo)
|
639
|
+
assert_equal "-64.1", fmt[input_rounding: :half_down].write(-lo)
|
640
|
+
assert_equal "-64.09999999999999", fmt[input_rounding: :up].write(-lo)
|
641
|
+
|
642
|
+
end
|
643
|
+
|
644
|
+
def test_special
|
645
|
+
fmt = Format[]
|
646
|
+
assert_equal 'NaN', fmt.write(Float.context.nan)
|
647
|
+
assert_equal 'NaN', fmt.write(Flt::DecNum.context.nan)
|
648
|
+
assert_equal 'NaN', fmt.write(BigDecimal.context.nan)
|
649
|
+
assert_equal 'Infinity', fmt.write(Float.context.infinity)
|
650
|
+
assert_equal 'Infinity', fmt.write(Flt::DecNum.context.infinity)
|
651
|
+
assert_equal 'Infinity', fmt.write(BigDecimal.context.infinity)
|
652
|
+
assert_equal '-Infinity', fmt.write(Float.context.infinity(-1))
|
653
|
+
assert_equal '-Infinity', fmt.write(Flt::DecNum.context.infinity(-1))
|
654
|
+
assert_equal '-Infinity', fmt.write(BigDecimal.context.infinity(-1))
|
655
|
+
|
656
|
+
fmt = fmt[symbols: [uppercase: true]]
|
657
|
+
assert_equal 'NAN', fmt.write(Float.context.nan)
|
658
|
+
assert_equal 'NAN', fmt.write(Flt::DecNum.context.nan)
|
659
|
+
assert_equal 'NAN', fmt.write(BigDecimal.context.nan)
|
660
|
+
assert_equal 'INFINITY', fmt.write(Float.context.infinity)
|
661
|
+
assert_equal 'INFINITY', fmt.write(Flt::DecNum.context.infinity)
|
662
|
+
assert_equal 'INFINITY', fmt.write(BigDecimal.context.infinity)
|
663
|
+
assert_equal '-INFINITY', fmt.write(Float.context.infinity(-1))
|
664
|
+
assert_equal '-INFINITY', fmt.write(Flt::DecNum.context.infinity(-1))
|
665
|
+
assert_equal '-INFINITY', fmt.write(BigDecimal.context.infinity(-1))
|
666
|
+
|
667
|
+
fmt = fmt[symbols: [show_plus: true]]
|
668
|
+
assert_equal 'NAN', fmt.write(Float.context.nan)
|
669
|
+
assert_equal 'NAN', fmt.write(Flt::DecNum.context.nan)
|
670
|
+
assert_equal 'NAN', fmt.write(BigDecimal.context.nan)
|
671
|
+
assert_equal '+INFINITY', fmt.write(Float.context.infinity)
|
672
|
+
assert_equal '+INFINITY', fmt.write(Flt::DecNum.context.infinity)
|
673
|
+
assert_equal '+INFINITY', fmt.write(BigDecimal.context.infinity)
|
674
|
+
assert_equal '-INFINITY', fmt.write(Float.context.infinity(-1))
|
675
|
+
assert_equal '-INFINITY', fmt.write(Flt::DecNum.context.infinity(-1))
|
676
|
+
assert_equal '-INFINITY', fmt.write(BigDecimal.context.infinity(-1))
|
677
|
+
|
678
|
+
fmt = Format[symbols: [nan: '??', infinity: 'oo', show_plus: true]]
|
679
|
+
assert_equal '??', fmt.write(Float.context.nan)
|
680
|
+
assert_equal '??', fmt.write(Flt::DecNum.context.nan)
|
681
|
+
assert_equal '??', fmt.write(BigDecimal.context.nan)
|
682
|
+
assert_equal '+oo', fmt.write(Float.context.infinity)
|
683
|
+
assert_equal '+oo', fmt.write(Flt::DecNum.context.infinity)
|
684
|
+
assert_equal '+oo', fmt.write(BigDecimal.context.infinity)
|
685
|
+
assert_equal '-oo', fmt.write(Float.context.infinity(-1))
|
686
|
+
assert_equal '-oo', fmt.write(Flt::DecNum.context.infinity(-1))
|
687
|
+
assert_equal '-oo', fmt.write(BigDecimal.context.infinity(-1))
|
688
|
+
end
|
689
|
+
|
690
|
+
def test_float_sign
|
691
|
+
fmt = Format[symbols:[uppercase: true]]
|
692
|
+
fmt_plus = fmt[symbols: [show_plus: true]]
|
693
|
+
assert_equal fmt_plus, fmt.set_plus(true)
|
694
|
+
fmt_plus_sp = fmt_plus[symbols: [plus: ' ']]
|
695
|
+
assert_equal fmt_plus_sp, fmt.set_plus(' ')
|
696
|
+
fmt_exp_plus = fmt[symbols: [show_exponent_plus: true]]
|
697
|
+
assert_equal fmt_exp_plus, fmt.set_plus(true, :exp)
|
698
|
+
assert_equal fmt_exp_plus, fmt.set_plus(:exp)
|
699
|
+
fmt_exp_plus_sp = fmt[symbols: [show_exponent_plus: true, plus: ' ']]
|
700
|
+
assert_equal fmt_exp_plus_sp, fmt.set_plus(' ', :exp)
|
701
|
+
fmt_both_plus = fmt[symbols: [show_exponent_plus: true, show_plus: true]]
|
702
|
+
assert_equal fmt_both_plus, fmt.set_plus(:all)
|
703
|
+
fmt_both_plus_sp = fmt[symbols: [show_exponent_plus: true, show_plus: true, plus: ' ']]
|
704
|
+
assert_equal fmt_both_plus_sp, fmt.set_plus(' ', :all)
|
705
|
+
|
706
|
+
assert_equal '1.25', fmt.write(1.25)
|
707
|
+
assert_equal '+1.25', fmt_plus.write(1.25)
|
708
|
+
assert_equal ' 1.25', fmt_plus_sp.write(1.25)
|
709
|
+
assert_equal '-1.25', fmt.write(-1.25)
|
710
|
+
assert_equal '-1.25', fmt_plus.write(-1.25)
|
711
|
+
assert_equal '1.25E5', fmt[mode: :sci].write(1.25E5)
|
712
|
+
assert_equal '-1.25E5', fmt[mode: :sci].write(-1.25E5)
|
713
|
+
assert_equal '1.25E-5', fmt[mode: :sci].write(1.25E-5)
|
714
|
+
assert_equal '-1.25E-5', fmt[mode: :sci].write(-1.25E-5)
|
715
|
+
assert_equal '+1.25E5', fmt_plus[mode: :sci].write(1.25E5)
|
716
|
+
assert_equal '-1.25E5', fmt_plus[mode: :sci].write(-1.25E5)
|
717
|
+
assert_equal ' 1.25E5', fmt_plus_sp[mode: :sci].write(1.25E5)
|
718
|
+
assert_equal '-1.25E5', fmt_plus_sp[mode: :sci].write(-1.25E5)
|
719
|
+
assert_equal '1.25E+5', fmt_exp_plus[mode: :sci].write(1.25E5)
|
720
|
+
assert_equal '-1.25E+5', fmt_exp_plus[mode: :sci].write(-1.25E5)
|
721
|
+
assert_equal '1.25E 5', fmt_exp_plus_sp[mode: :sci].write(1.25E5)
|
722
|
+
assert_equal '-1.25E 5', fmt_exp_plus_sp[mode: :sci].write(-1.25E5)
|
723
|
+
assert_equal '1.25E-5', fmt_exp_plus_sp[mode: :sci].write(1.25E-5)
|
724
|
+
assert_equal '-1.25E-5', fmt_exp_plus_sp[mode: :sci].write(-1.25E-5)
|
725
|
+
|
726
|
+
assert_equal ' 1.25E-5', fmt_both_plus_sp[mode: :sci].write(1.25E-5)
|
727
|
+
assert_equal '-1.25E-5', fmt_both_plus_sp[mode: :sci].write(-1.25E-5)
|
728
|
+
assert_equal ' 1.25E 5', fmt_both_plus_sp[mode: :sci].write(1.25E5)
|
729
|
+
assert_equal '-1.25E 5', fmt_both_plus_sp[mode: :sci].write(-1.25E5)
|
730
|
+
assert_equal '+1.25E-5', fmt_both_plus[mode: :sci].write(1.25E-5)
|
731
|
+
assert_equal '-1.25E-5', fmt_both_plus[mode: :sci].write(-1.25E-5)
|
732
|
+
assert_equal '+1.25E+5', fmt_both_plus[mode: :sci].write(1.25E5)
|
733
|
+
assert_equal '-1.25E+5', fmt_both_plus[mode: :sci].write(-1.25E+5)
|
734
|
+
end
|
735
|
+
|
736
|
+
def test_write_bigdecimal_dec
|
737
|
+
assert_equal '1', Format[rounding: :short].write(BigDecimal('1.0'))
|
738
|
+
assert_equal '1', Format[rounding: :free].write(BigDecimal('1.0'))
|
739
|
+
|
740
|
+
assert_equal '0', Format[rounding: :short].write(BigDecimal('0'))
|
741
|
+
assert_equal '0', Format[rounding: :short].write(BigDecimal('0.0'))
|
742
|
+
assert_equal '0', Format[rounding: :short].write(BigDecimal('0.0000'))
|
743
|
+
assert_equal '0', Format[rounding: :short].write(BigDecimal('0E-15'))
|
744
|
+
|
745
|
+
assert_equal '0.0', Format[rounding: :free].write(BigDecimal('0'))
|
746
|
+
assert_equal '0.0', Format[rounding: :free].write(BigDecimal('0.0'))
|
747
|
+
assert_equal '0.0', Format[rounding: :free].write(BigDecimal('0.0000'))
|
748
|
+
assert_equal '0.0', Format[rounding: :free].write(BigDecimal('0E-15'))
|
749
|
+
|
750
|
+
assert_equal '0.1', Format[:short].write(BigDecimal('0.100'))
|
751
|
+
assert_equal '0.1', Format[:free].write(BigDecimal('0.100'))
|
752
|
+
end
|
753
|
+
|
754
|
+
def test_write_numeral
|
755
|
+
assert_equal '1', Format[].write(Numeral[1, point: 1])
|
756
|
+
assert_equal '1.23', Format[].write(Numeral[1, 2, 3, point: 1])
|
757
|
+
assert_equal '-1.23', Format[].write(Numeral[1, 2, 3, point: 1, sign: -1])
|
758
|
+
assert_equal '-1.2333...', Format[].write(Numeral[1, 2, 3, point: 1, repeat: 2, sign: -1])
|
759
|
+
assert_equal '-1.23e10', Format[].write(Numeral[1, 2, 3, point: 11, sign: -1])
|
760
|
+
|
761
|
+
n = Numeral[1,2,3,4,5,6,7,1,2,3, point: 0, repeat: 7]
|
762
|
+
assert_equal '0.1234567<123>', Format[symbols: [repeat_delimited: true]].write(n)
|
763
|
+
assert_equal '0.1234567123123123...', Format[symbols: [repeat_count: 3]].write(n)
|
764
|
+
assert_equal '0.1234567123123...', Format[symbols: [repeat_count: 2]].write(n)
|
765
|
+
# Next numeral cannot be formatted showing just two occurrences of the repeat
|
766
|
+
n = Numeral[4,5,1,2,3,1,2,3,4,5,1,2,3, repeat: 10, point: 0]#
|
767
|
+
assert_equal '0.4512312345<123>', Format[symbols: [repeat_delimited: true]].write(n)
|
768
|
+
assert_equal '0.4512312345123123123...', Format[symbols: [repeat_count: 3]].write(n)
|
769
|
+
assert_equal '0.4512312345123123123...', Format[symbols: [repeat_count: 2]].write(n)
|
770
|
+
end
|
771
|
+
|
772
|
+
def test_repeating_error
|
773
|
+
assert_raises Format::InvalidRepeatingNumeral do
|
774
|
+
Format[:free, repeating: false].write(Rational(1,3))
|
775
|
+
end
|
776
|
+
assert_raises Format::InvalidRepeatingNumeral do
|
777
|
+
Format[:short, repeating: false].write(Rational(1,3))
|
778
|
+
end
|
779
|
+
assert_nothing_raised do
|
780
|
+
Format[precision: 10, repeating: false].write(Rational(1,3))
|
781
|
+
end
|
782
|
+
assert_nothing_raised do
|
783
|
+
Format[:free, repeating: false].write(Rational(2469,200))
|
784
|
+
end
|
785
|
+
assert_nothing_raised do
|
786
|
+
Format[:short, repeating: false].write(Rational(2469,200))
|
787
|
+
end
|
788
|
+
end
|
789
|
+
end
|