float-formats 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +27 -32
- data/Manifest.txt +10 -18
- data/README.txt +330 -327
- data/Rakefile +38 -4
- data/lib/float-formats.rb +9 -9
- data/lib/float-formats/bytes.rb +463 -303
- data/lib/float-formats/classes.rb +1950 -1605
- data/lib/float-formats/formats.rb +633 -669
- data/lib/float-formats/native.rb +144 -272
- data/lib/float-formats/version.rb +4 -4
- data/tasks/ann.rake +80 -0
- data/tasks/bones.rake +20 -0
- data/tasks/gem.rake +192 -0
- data/tasks/git.rake +40 -0
- data/tasks/manifest.rake +48 -0
- data/tasks/notes.rake +27 -0
- data/tasks/post_load.rake +39 -0
- data/tasks/rdoc.rake +50 -0
- data/tasks/rubyforge.rake +55 -0
- data/tasks/setup.rb +279 -0
- data/tasks/spec.rake +54 -0
- data/tasks/svn.rake +47 -0
- data/tasks/test.rake +40 -0
- data/test/gen_test_data.rb +119 -119
- data/test/test_arithmetic.rb +36 -0
- data/test/test_bytes.rb +43 -0
- data/test/test_data.yaml +1667 -1667
- data/test/test_float_formats.rb +208 -0
- data/test/test_helper.rb +2 -1
- data/test/test_native-float.rb +84 -77
- metadata +123 -60
- data/config/hoe.rb +0 -73
- data/config/requirements.rb +0 -17
- data/script/destroy +0 -14
- data/script/destroy.cmd +0 -1
- data/script/generate +0 -14
- data/script/generate.cmd +0 -1
- data/script/txt2html +0 -74
- data/script/txt2html.cmd +0 -1
- data/tasks/deployment.rake +0 -34
- data/tasks/environment.rake +0 -7
- data/tasks/website.rake +0 -17
- data/test/test_float-formats.rb +0 -169
data/History.txt
CHANGED
@@ -1,35 +1,30 @@
|
|
1
|
-
== 0.
|
2
|
-
|
3
|
-
*
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
*
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
*
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
*
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
*
|
22
|
-
|
23
|
-
* Bug fixes
|
24
|
-
- Fix the encoding-decoding of nan and infinity in Decimal format.
|
25
|
-
- Fix the decoding of NaN in Binary & Hexadecimal
|
26
|
-
- The definition of IEEE_binary128 was not correct
|
27
|
-
|
28
|
-
and there is a hidden bit, then minimum nonzero significand is radix*(prec-1)+1
|
29
|
-
rather than radix*(prec-1); the latter value could be computed in ratio_float
|
30
|
-
and then packed in the representation, being replaced by zero. This would
|
31
|
-
result in an incorrect encoding of the minimum nonzero value.
|
32
|
-
|
1
|
+
== 0.2.0 2009-08-06
|
2
|
+
|
3
|
+
* New redefined interface
|
4
|
+
* The sign in splitted form (sign, significand, exponent) now
|
5
|
+
have the sign as an integer +1,-1 rather than using the
|
6
|
+
internal representation (0, 1, etc.)
|
7
|
+
* Arithmetic is possible on FP values
|
8
|
+
* Requires Flt 1.0.0 and Nio 0.2.4
|
9
|
+
|
10
|
+
== 0.1.1 2007-11-15
|
11
|
+
|
12
|
+
* HP-71B formats defined
|
13
|
+
* Add half precision IEEE format (binary16)
|
14
|
+
* New names for IEEE formats
|
15
|
+
* Add some IEEE 754r interchange formats
|
16
|
+
* new methods hex_to_float, hex_from_float in float-formats/native
|
17
|
+
* Allow non-bcd values in fields of BCD formats by passing
|
18
|
+
hex values as Strings; allow such values to be used for
|
19
|
+
nan/infinity exponents.
|
20
|
+
* Nio 0.2.1 is now reqiured
|
21
|
+
* Handle special values (Infinities and NaN) in #from_fmt, #from_number
|
22
|
+
* Add ulp methods to Value and FP classes and to Float
|
23
|
+
* Bug fixes
|
24
|
+
- Fix the encoding-decoding of nan and infinity in Decimal format.
|
25
|
+
- Fix the decoding of NaN in Binary & Hexadecimal
|
26
|
+
- The definition of IEEE_binary128 was not correct
|
27
|
+
|
33
28
|
== 0.1.0 2007-11-04
|
34
29
|
|
35
30
|
* Initial release
|
data/Manifest.txt
CHANGED
@@ -3,26 +3,18 @@ License.txt
|
|
3
3
|
Manifest.txt
|
4
4
|
README.txt
|
5
5
|
Rakefile
|
6
|
-
config/hoe.rb
|
7
|
-
config/requirements.rb
|
8
6
|
lib/float-formats.rb
|
9
7
|
lib/float-formats/version.rb
|
10
|
-
lib/float-formats/bytes.rb
|
11
|
-
lib/float-formats/classes.rb
|
12
|
-
lib/float-formats/formats.rb
|
8
|
+
lib/float-formats/bytes.rb
|
9
|
+
lib/float-formats/classes.rb
|
10
|
+
lib/float-formats/formats.rb
|
13
11
|
lib/float-formats/native.rb
|
14
|
-
script/destroy
|
15
|
-
script/destroy.cmd
|
16
|
-
script/generate
|
17
|
-
script/generate.cmd
|
18
|
-
script/txt2html
|
19
|
-
script/txt2html.cmd
|
20
12
|
setup.rb
|
21
|
-
|
22
|
-
tasks/environment.rake
|
23
|
-
tasks/website.rake
|
24
|
-
test/test_float-formats.rb
|
13
|
+
test/test_float_formats.rb
|
25
14
|
test/test_helper.rb
|
26
|
-
test/test_data.yaml
|
27
|
-
test/gen_test_data.rb
|
28
|
-
test/test_native-float.rb
|
15
|
+
test/test_data.yaml
|
16
|
+
test/gen_test_data.rb
|
17
|
+
test/test_native-float.rb
|
18
|
+
test/test_arithmetic.rb
|
19
|
+
test/test_bytes.rb
|
20
|
+
test/gen_test_data.rb
|
data/README.txt
CHANGED
@@ -1,327 +1,330 @@
|
|
1
|
-
=Introduction
|
2
|
-
|
3
|
-
Float-Formats is a Ruby package with methods to handle diverse floating-point formats.
|
4
|
-
These are some of the things that can be done with it:
|
5
|
-
|
6
|
-
* Enconding and decoding numerical values in specific floating point representations.
|
7
|
-
* Conversion of floating-point data between different formats.
|
8
|
-
* Obtaining properties of floating-point formats (ranges, precision, etc.)
|
9
|
-
* Exploring and learning about floating point representations.
|
10
|
-
* Definition and testing of new floating-point formats.
|
11
|
-
|
12
|
-
=Installation
|
13
|
-
|
14
|
-
The easiest way to install Nio is using gems:
|
15
|
-
|
16
|
-
<tt> gem install --remote float-formats -y</tt>
|
17
|
-
|
18
|
-
==Requirements
|
19
|
-
|
20
|
-
Nio[http://nio.rubyforge.org/] 0.2.
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
==Downloads
|
26
|
-
|
27
|
-
The latest version of Float-Formats and its source code can be downloaded from
|
28
|
-
* http://rubyforge.org/project/showfiles.php?group_id=4684
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
==Legacy
|
54
|
-
Formats of historical interest, some of which are found
|
55
|
-
in file formats still in use.
|
56
|
-
|
57
|
-
<b>Mainframe/supercomputer</b> formats:
|
58
|
-
Univac 1100 (UNIVAC_SINGLE, UNIVAC_DOUBLE),
|
59
|
-
IBM 360 etc. (IBM32, IBM64 and IBM128),
|
60
|
-
CDC 6600/7600: (CDC_SINGLE, CDC_DOUBLE),
|
61
|
-
Cray-1: (CRAY).
|
62
|
-
|
63
|
-
<b>Minis</b>: PDP11 and Vaxes: (PDP11_F, PDP11_D, VAX_F, VAX_D, VAX_G and VAX_H),
|
64
|
-
HP3000: (XS256, XS256_DOUBLE),
|
65
|
-
Wang 2200: (WANG2200).
|
66
|
-
|
67
|
-
<b>Microcomputers</b> (software implementations):
|
68
|
-
Apple II: (APPLE),
|
69
|
-
Microsoft Basic, Spectrum, etc.: (XS128),
|
70
|
-
Microsoft Quickbasic: (MBF_SINGLE, MBF_DOUBLE),
|
71
|
-
Borland Pascal: (BORLAND48).
|
72
|
-
|
73
|
-
<b>Embedded systems</b>:
|
74
|
-
Formats used in the Intel 8051 by the C51 compiler:
|
75
|
-
(C51_BCD_FLOAT, C51_BCD_DOUBLE and C51_BCD_LONG_DOUBLE).
|
76
|
-
|
77
|
-
|
78
|
-
==Calculators
|
79
|
-
Formats used in HP RPL calculators: (RPL, RPL_X),
|
80
|
-
HP-71B formats (HP71B, HP71B_X)
|
81
|
-
and classic HP 10 digit calculators: (HP_CLASSIC).
|
82
|
-
|
83
|
-
|
84
|
-
=Using the pre-defined formats
|
85
|
-
|
86
|
-
require 'rubygems'
|
87
|
-
require 'float-formats'
|
88
|
-
include
|
89
|
-
|
90
|
-
The properties of the floating point formats can be queried (which can be
|
91
|
-
used for tables or reports comparing different formats):
|
92
|
-
|
93
|
-
Size in bits of the representations:
|
94
|
-
puts
|
95
|
-
|
96
|
-
Numeric radix:
|
97
|
-
puts
|
98
|
-
|
99
|
-
Digits of precision (radix-based)
|
100
|
-
puts
|
101
|
-
|
102
|
-
Minimum and maximum values of the radix-based exponent:
|
103
|
-
puts
|
104
|
-
puts
|
105
|
-
|
106
|
-
Decimal precision
|
107
|
-
puts
|
108
|
-
puts
|
109
|
-
|
110
|
-
Minimum and maximum decimal exponents:
|
111
|
-
puts
|
112
|
-
puts
|
113
|
-
|
114
|
-
==Encode and decode numbers
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
*
|
119
|
-
the sign (
|
120
|
-
and the exponent.
|
121
|
-
*
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
puts IEEE_SINGLE.
|
155
|
-
puts IEEE_SINGLE.
|
156
|
-
puts IEEE_SINGLE.
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
v =
|
162
|
-
v = v.convert_to(
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
puts
|
175
|
-
puts
|
176
|
-
|
177
|
-
puts
|
178
|
-
puts
|
179
|
-
puts
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
(
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
[<i>
|
238
|
-
http://
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
[<i>
|
246
|
-
http://www2.hursley.ibm.com/decimal/
|
247
|
-
|
248
|
-
[<i>Packed Decimal
|
249
|
-
http://
|
250
|
-
|
251
|
-
[<i>
|
252
|
-
http://
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
Description of
|
263
|
-
[<i>
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
Available
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
[<i>
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
1
|
+
=Introduction
|
2
|
+
|
3
|
+
Float-Formats is a Ruby package with methods to handle diverse floating-point formats.
|
4
|
+
These are some of the things that can be done with it:
|
5
|
+
|
6
|
+
* Enconding and decoding numerical values in specific floating point representations.
|
7
|
+
* Conversion of floating-point data between different formats.
|
8
|
+
* Obtaining properties of floating-point formats (ranges, precision, etc.)
|
9
|
+
* Exploring and learning about floating point representations.
|
10
|
+
* Definition and testing of new floating-point formats.
|
11
|
+
|
12
|
+
=Installation
|
13
|
+
|
14
|
+
The easiest way to install Nio is using gems:
|
15
|
+
|
16
|
+
<tt> gem install --remote float-formats -y</tt>
|
17
|
+
|
18
|
+
==Requirements
|
19
|
+
|
20
|
+
Nio[http://nio.rubyforge.org/] 0.2.4 or later
|
21
|
+
and Flt[http://flt.rubyforge.org/] 1.0.0 or later are needed. These
|
22
|
+
can be installed as gems and should be automatically
|
23
|
+
installed by the command shown above to install float-formats.
|
24
|
+
|
25
|
+
==Downloads
|
26
|
+
|
27
|
+
The latest version of Float-Formats and its source code can be downloaded from
|
28
|
+
* http://rubyforge.org/project/showfiles.php?group_id=4684
|
29
|
+
|
30
|
+
You can find the code also in GitHub:
|
31
|
+
* http://github.com/jgoizueta/float-formats/
|
32
|
+
|
33
|
+
=Predefined formats
|
34
|
+
|
35
|
+
A number of common formats are defined as constants in the Flt module:
|
36
|
+
|
37
|
+
==IEEE 754-2008
|
38
|
+
<b>binary</b> floating point representations in little endian order:
|
39
|
+
IEEE_binary16 (half precision),
|
40
|
+
IEEE_binary32 (single precision),
|
41
|
+
IEEE_binary64 (double precision),
|
42
|
+
IEEE_binary80 (extended), IEEE_binary128 (quadruple precision) and
|
43
|
+
as little endian: IEEE_binary16_BE, etc.
|
44
|
+
|
45
|
+
<b>decimal</b> formats (using DPD):
|
46
|
+
IEEE_decimal32, IEEE_decimal64 and IEEE_decimal128.
|
47
|
+
|
48
|
+
<b>interchange binary & decimal</b> formats:
|
49
|
+
IEEE_binary256, IEEE_binary512, IEEE_binary1024, IEEE_decimal192, IEEE_decimal256.
|
50
|
+
Others can be defined with IEEE.interchange_binary and IEEE.interchange_decimal
|
51
|
+
(see the IEEE module).
|
52
|
+
|
53
|
+
==Legacy
|
54
|
+
Formats of historical interest, some of which are found
|
55
|
+
in file formats still in use.
|
56
|
+
|
57
|
+
<b>Mainframe/supercomputer</b> formats:
|
58
|
+
Univac 1100 (UNIVAC_SINGLE, UNIVAC_DOUBLE),
|
59
|
+
IBM 360 etc. (IBM32, IBM64 and IBM128),
|
60
|
+
CDC 6600/7600: (CDC_SINGLE, CDC_DOUBLE),
|
61
|
+
Cray-1: (CRAY).
|
62
|
+
|
63
|
+
<b>Minis</b>: PDP11 and Vaxes: (PDP11_F, PDP11_D, VAX_F, VAX_D, VAX_G and VAX_H),
|
64
|
+
HP3000: (XS256, XS256_DOUBLE),
|
65
|
+
Wang 2200: (WANG2200).
|
66
|
+
|
67
|
+
<b>Microcomputers</b> (software implementations):
|
68
|
+
Apple II: (APPLE),
|
69
|
+
Microsoft Basic, Spectrum, etc.: (XS128),
|
70
|
+
Microsoft Quickbasic: (MBF_SINGLE, MBF_DOUBLE),
|
71
|
+
Borland Pascal: (BORLAND48).
|
72
|
+
|
73
|
+
<b>Embedded systems</b>:
|
74
|
+
Formats used in the Intel 8051 by the C51 compiler:
|
75
|
+
(C51_BCD_FLOAT, C51_BCD_DOUBLE and C51_BCD_LONG_DOUBLE).
|
76
|
+
|
77
|
+
|
78
|
+
==Calculators
|
79
|
+
Formats used in HP RPL calculators: (RPL, RPL_X),
|
80
|
+
HP-71B formats (HP71B, HP71B_X)
|
81
|
+
and classic HP 10 digit calculators: (HP_CLASSIC).
|
82
|
+
|
83
|
+
|
84
|
+
=Using the pre-defined formats
|
85
|
+
|
86
|
+
require 'rubygems'
|
87
|
+
require 'float-formats'
|
88
|
+
include Flt
|
89
|
+
|
90
|
+
The properties of the floating point formats can be queried (which can be
|
91
|
+
used for tables or reports comparing different formats):
|
92
|
+
|
93
|
+
Size in bits of the representations:
|
94
|
+
puts IEEE_binary32.total_bits # -> 32
|
95
|
+
|
96
|
+
Numeric radix:
|
97
|
+
puts IEEE_binary32.radix # -> 2
|
98
|
+
|
99
|
+
Digits of precision (radix-based)
|
100
|
+
puts IEEE_binary32.significand_digits # -> 24
|
101
|
+
|
102
|
+
Minimum and maximum values of the radix-based exponent:
|
103
|
+
puts IEEE_binary32.radix_min_exp # -> -126
|
104
|
+
puts IEEE_binary32.radix_max_exp # -> 127
|
105
|
+
|
106
|
+
Decimal precision
|
107
|
+
puts IEEE_binary32.decimal_digits_stored # -> 6
|
108
|
+
puts IEEE_binary32.decimal_digits_necessary # -> 9
|
109
|
+
|
110
|
+
Minimum and maximum decimal exponents:
|
111
|
+
puts IEEE_binary32.decimal_min_exp # -> -37
|
112
|
+
puts IEEE_binary32.decimal_max_exp # -> 38
|
113
|
+
|
114
|
+
==Encode and decode numbers
|
115
|
+
|
116
|
+
For each floating-point format class there is a constructor method with the same
|
117
|
+
name which can build a floating-point value from a variety of parameters:
|
118
|
+
* Using three integers:
|
119
|
+
the sign (+1 for +, -1 for -), the significand (coefficient or mantissa)
|
120
|
+
and the exponent.
|
121
|
+
* From a text numeral (with an optional Nio format specifier)
|
122
|
+
* From a number : converts a numerical value
|
123
|
+
to a floating point representation.
|
124
|
+
|
125
|
+
File.open('binary_file.dat','wb'){|f| f.write IEEE_binary80('0.1').to_bytes}
|
126
|
+
|
127
|
+
puts IEEE_binary80('0.1').to_hex(true) # -> CD CC CC CC CC CC CC CC FB 3F
|
128
|
+
puts IEEE_binary80(0.1).to_hex(true) # -> CD CC CC CC CC CC CC CC FB 3F
|
129
|
+
puts IEEE_binary80(+1,123,-2).to_hex(true) # -> 00 00 00 00 00 00 00 F6 03 40
|
130
|
+
puts IEEE_decimal32('1.234').to_hex(true) # -> 22 20 05 34
|
131
|
+
|
132
|
+
A floating-point encoded value can be converted to useful formats with the to_ and similar methods:
|
133
|
+
* <tt>split</tt> (split as integral sign, significand, exponent)
|
134
|
+
* <tt>to_text</tt>
|
135
|
+
* <tt>to(num_class)</tt>
|
136
|
+
|
137
|
+
v = IEEE_binary80.from_bytes(File.read('binary_file.dat'))
|
138
|
+
puts v.to(Rational) # -> 1/10
|
139
|
+
puts v.split.inspect # -> [1, 14757395258967641293, -67]
|
140
|
+
puts v.to_text # -> 0.1
|
141
|
+
puts v.to(Float) # -> 0.1
|
142
|
+
puts v.to_hex # -> CDCCCCCCCCCCCCCCFB3F
|
143
|
+
puts v.to_bits # -> 00111111111110111100110011001100110011001100110011001100110011001100110011001101
|
144
|
+
puts v.to_bits_text(16) # -> 3ffbcccccccccccccccd
|
145
|
+
|
146
|
+
==Special values:
|
147
|
+
|
148
|
+
Let's show the decimal expression of some interesting values using
|
149
|
+
3 significative digits:
|
150
|
+
|
151
|
+
fmt = Nio::Fmt.mode(:gen,3)
|
152
|
+
|
153
|
+
puts IEEE_SINGLE.min_value.to_text(fmt) # -> 2E-45
|
154
|
+
puts IEEE_SINGLE.min_normalized_value.to_text(fmt) # -> 1.18E-38
|
155
|
+
puts IEEE_SINGLE.max_value.to_text(fmt) # -> 3.4E38
|
156
|
+
puts IEEE_SINGLE.epsilon.to_text(fmt) # -> 1.19E-7
|
157
|
+
|
158
|
+
==Convert between formats
|
159
|
+
|
160
|
+
v = IEEE_EXTENDED.from_text('1.1')
|
161
|
+
v = v.convert_to(IEEE_SINGLE)
|
162
|
+
v = v.convert_to(IEEE_DEC64)
|
163
|
+
|
164
|
+
|
165
|
+
=Tools for the native floating point format
|
166
|
+
This is an optional module to perform conversions and manipulate the native Float format.
|
167
|
+
|
168
|
+
require 'float-formats/native'
|
169
|
+
include Flt
|
170
|
+
|
171
|
+
puts float_shortest_dec(0.1) # -> 0.1
|
172
|
+
puts float_significant_dec(0.1) # -> 0.10000000000000001
|
173
|
+
puts float_dec(0.1) # -> 0.1000000000000000055511151231257827021181583404541015625
|
174
|
+
puts float_bin(0.1) # -> 1.100110011001100110011001100110011001100110011001101E-4
|
175
|
+
puts hex_from_float(0.1) # -> 0x1999999999999ap-56
|
176
|
+
|
177
|
+
puts float_significant_dec(Float::MIN_D) # -> 5E-324
|
178
|
+
puts float_significant_dec(Float::MAX_D) # -> 2.2250738585072009E-308
|
179
|
+
puts float_significant_dec(Float::MIN_N) # -> 2.2250738585072014E-308
|
180
|
+
|
181
|
+
|
182
|
+
Together with flt/sugar (from Flt) can be use to explore or work with Floats:
|
183
|
+
|
184
|
+
require 'flt/sugar'
|
185
|
+
|
186
|
+
puts 1.0.next_plus-1 == Float::EPSILON # -> true
|
187
|
+
puts float_shortest_dec(1.0.next_plus) # -> 1.0000000000000002
|
188
|
+
puts float_dec(1.0.next_minus) # -> 0.99999999999999988897769753748434595763683319091796875
|
189
|
+
puts float_dec(1.0.next_plus) # -> 1.0000000000000002220446049250313080847263336181640625
|
190
|
+
puts float_bin(1.0.next_plus) # -> 1.0000000000000000000000000000000000000000000000000001E0
|
191
|
+
puts float_bin(1.0.next_minus) # -> 1.1111111111111111111111111111111111111111111111111111E-1
|
192
|
+
|
193
|
+
puts float_significant_dec(Float::MIN_D.next_plus) # -> 1.0E-323
|
194
|
+
puts float_significant_dec(Float::MAX_D.next_minus) # -> 2.2250738585072004E-308
|
195
|
+
|
196
|
+
=Defining new formats
|
197
|
+
|
198
|
+
New formats are defined using one of the classes defined in float-formats/classes.rb
|
199
|
+
and passing the necessary parameters in a hash to the constructor.
|
200
|
+
|
201
|
+
For example, here we define a binary floating point 32-bits format with
|
202
|
+
22 bits for the significand, 9 for the exponent and 1 for the sign
|
203
|
+
(these fields are allocated from least to most significant bits).
|
204
|
+
We'll use excess notation with bias 127 for the exponent, interpreting
|
205
|
+
the significand bits as a fractional number with the radix point after
|
206
|
+
the first bit, which will be hidden:
|
207
|
+
Flt.define(:MY_FP, BinaryFormat,
|
208
|
+
:fields=>[:significand,22,:exponent,9,:sign,1],
|
209
|
+
:bias=>127, :bias_mode=>:scientific_significand,
|
210
|
+
:hidden_bit=>true)
|
211
|
+
Now we can encode values in this format, decode values, convet to other
|
212
|
+
formats, query it's range, etc:
|
213
|
+
|
214
|
+
puts MY_FP('0.1').to_bits_text(16) # -> 1ee66666
|
215
|
+
puts MY_FP.max_value.to_text(Nio::Fmt.prec(3)) # -> 7.88E115
|
216
|
+
|
217
|
+
You can look at float-formats/formats.rb to see how the built-in formats
|
218
|
+
are defined.
|
219
|
+
|
220
|
+
=License
|
221
|
+
|
222
|
+
This code is free to use under the terms of the GNU GENERAL PUBLIC LICENSE.
|
223
|
+
|
224
|
+
=References
|
225
|
+
|
226
|
+
|
227
|
+
[<i>Floating Point Representations.</i> C.B. Silio.]
|
228
|
+
http://www.ece.umd.edu/class/enpm607.S2000/fltngpt.pdf
|
229
|
+
Description of formats used in UNIVAC 1100, CDC 6600/7600, PDP-11, IEEE754, IBM360/370
|
230
|
+
|
231
|
+
[<i>Floating-Point Formats.</i> John Savard.]
|
232
|
+
http://www.quadibloc.com/comp/cp0201.htm
|
233
|
+
Description of formats used in VAX and PDF-11
|
234
|
+
|
235
|
+
|
236
|
+
===IEEE754 binary formats
|
237
|
+
[<i>IEEE-754 References.</i> Christopher Vickery.]
|
238
|
+
http://babbage.cs.qc.edu/courses/cs341/IEEE-754references.html
|
239
|
+
|
240
|
+
[<i>What Every Computer Scientist Should Know About Floating-Point Arithmetic.</i> David Goldberg.]
|
241
|
+
http://docs.sun.com/source/806-3568/ncg_goldberg.html
|
242
|
+
|
243
|
+
|
244
|
+
===DPD/IEEE754r decimal formats
|
245
|
+
[<i>Decimal Arithmetic Encoding. Strawman 4d.</i> Mike Cowlishaw.]
|
246
|
+
http://www2.hursley.ibm.com/decimal/decbits.pdf
|
247
|
+
|
248
|
+
[<i>A Summary of Densely Packed Decimal encoding.</i> Mike Cowlishaw.]
|
249
|
+
http://www2.hursley.ibm.com/decimal/DPDecimal.html
|
250
|
+
|
251
|
+
[<i>Packed Decimal Encoding IEEE-754-r.</i> J.H.M. Bonten.]
|
252
|
+
http://home.hetnet.nl/mr_1/81/jhm.bonten/computers/bitsandbytes/wordsizes/ibmpde.htm
|
253
|
+
|
254
|
+
[<i>DRAFT Standard for Floating-Point Arithmetic P754.</i> IEEE.]
|
255
|
+
http://www.validlab.com/754R/drafts/archive/2007-10-05.pdf
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
===HP 10 digits calculators
|
260
|
+
|
261
|
+
[<i>HP CPU and Programming</i>. David G.Hicks.]
|
262
|
+
http://www.hpmuseum.org/techcpu.htm Description of calculator CPUs from the Museum of HP Calculators.
|
263
|
+
[<i>HP 35 ROM step by step.</i> Jacques Laporte]
|
264
|
+
http://www.jacques-laporte.org/HP35%20ROM.htm
|
265
|
+
Description of HP35 registers.
|
266
|
+
[<i>Scientific Pocket Calculator Extends Range of Built-In Functions.</i> Eric A. Evett, Paul J. McClellan, Joseph P. Tanzini.]
|
267
|
+
Hewlett Packard Journal 1983-05 pgs 27-28. Describes format used in HP-15C.
|
268
|
+
|
269
|
+
|
270
|
+
===HP 12 digits calculators
|
271
|
+
[<i>Software Internal Design Specification Volume I For the HP-71</i>. Hewlett Packard.]
|
272
|
+
Available from http://www.hpmuseum.org/cd/cddesc.htm
|
273
|
+
[<i>RPL PROGRAMMING GUIDE</i>]
|
274
|
+
Excerpted from <i>RPL: A Mathematical Control Language</i>. by W. C. Wickes.
|
275
|
+
Available at http://www.hpcalc.org/details.php?id=1743
|
276
|
+
|
277
|
+
===HP-3000
|
278
|
+
[<i>A Pocket Calculator for Computer Science Professionals.</i> Eric A. Evett.]
|
279
|
+
Hewlett Packard Journal 1983-05 pg 37. Describes format used in HP-3000
|
280
|
+
|
281
|
+
===IBM
|
282
|
+
[<i>IBM Floating Point Architecture.</i> Wikipedia.]
|
283
|
+
http://en.wikipedia.org/wiki/IBM_Floating_Point_Architecture
|
284
|
+
[<i>The IBM eServer z990 floating-point unit</i>. G. Gerwig, H. Wetter, E. M. Schwarz, J. Haess, C. A. Krygowski, B. M. Fleischer and M. Kroener.]
|
285
|
+
http://www.research.ibm.com/journal/rd/483/gerwig.html
|
286
|
+
|
287
|
+
===MBF
|
288
|
+
[<i>Microsoft Knowledbase Article 35826</i>]
|
289
|
+
http://support.microsoft.com/?scid=kb%3Ben-us%3B35826&x=17&y=12
|
290
|
+
[<i>Microsoft MBF2IEEE library</i>]
|
291
|
+
http://download.microsoft.com/download/vb30/install/1/win98/en-us/mbf2ieee.exe
|
292
|
+
|
293
|
+
===Borland
|
294
|
+
[<i>An Overview of Floating Point Numbers.</i> Borland Developer Support Staff]
|
295
|
+
|
296
|
+
[<i>Pascal Floating-Point Page.<i> J R Stockton.]
|
297
|
+
http://www.merlyn.demon.co.uk/pas-real.htm
|
298
|
+
|
299
|
+
===8-bit micros
|
300
|
+
This is the MS Basic format (BASIC09 for TRS-80 Color Computer, Dragon),
|
301
|
+
also used in the Sinclair Spectrum.
|
302
|
+
|
303
|
+
[<i>Numbers are followed by information not in listings</i>]
|
304
|
+
Sinclair User October 1983 http://www.sincuser.f9.co.uk/019/helplne.htm
|
305
|
+
|
306
|
+
[<i>Sinclair ZX Spectrum / Basic Programming.</i>. Steven Vickers.]
|
307
|
+
Chapter 24. http://www.worldofspectrum.org/ZXBasicManual/zxmanchap24.html
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
===Apple II
|
312
|
+
[<i>Floating Point Routines for the 6502</i> Roy Rankin and Steve Wozniak.]
|
313
|
+
Dr. Dobb's Journal, August 1976, pages 17-19.
|
314
|
+
|
315
|
+
===C51
|
316
|
+
[<i>Advanced Development System</i> Franklin Software, Inc.]
|
317
|
+
http://www.fsinc.com/reference/html/com9anm.htm
|
318
|
+
|
319
|
+
===CDC6600
|
320
|
+
[<i>CONTROL DATA 6400/6500/6600 COMPUTER SYSTEMS Reference Manual</i>]
|
321
|
+
Manuals available at http://bitsavers.org/
|
322
|
+
|
323
|
+
|
324
|
+
===Cray
|
325
|
+
[<i>CRAY-1 COMPUTER SYSTEM Hardware Reference Manual</i>]
|
326
|
+
See pg 3-20 from 2240004 or pg 4-30 from HR-0808 or pg 4-21 from HP-0032.
|
327
|
+
Manuals available at http://bitsavers.org/
|
328
|
+
|
329
|
+
===Wang 2200
|
330
|
+
[<i>Internal Floating Point Representation</i>] http://www.wang2200.org/fp_format.html
|