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 CHANGED
@@ -1,35 +1,30 @@
1
- == 0.1.1 2007-12-15
2
-
3
- * HP-71B formats defined
4
-
5
- * Add half precision IEEE format (binary16)
6
-
7
- * New names for IEEE formats
8
-
9
- * Add some IEEE 754r interchange formats
10
-
11
- * new methods hex_to_float, hex_from_float in float-formats/native
12
-
13
- * Allow non-bcd values in fields of BCD formats by passing
14
- hex values as Strings; allow such values to be used for
15
- nan/infinity exponents.
16
-
17
- * Nio 0.2.1 is now required
18
-
19
- * Handle special values (Infinities and NaN) in #from_fmt, #from_number
20
-
21
- * Add ulp methods to Value and FP classes and to Float
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
- - In formats such as XS256 where the minimum exponent is not used only for zero
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
- tasks/deployment.rake
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.0 or later is needed. This
21
- can be installed as a gem and should be automatically
22
- installed by the command shown above to install float-formats.
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
- =Predefined formats
33
-
34
- A number of common formats are defined as constants in the FltPnt module:
35
-
36
- ==IEEE 754r
37
- <b>binary</b> floating point representations in little endian order:
38
- IEEE_binary16 (half precision),
39
- IEEE_binary32 (single precision),
40
- IEEE_binary64 (double precision),
41
- IEEE_binary80 (extended), IEEE_binary128 (quadruple precision) and
42
- as little endian: IEEE_binary16_BE, etc.
43
-
44
- <b>decimal</b> formats (using DPD):
45
- IEEE_decimal32, IEEE_decimal64 and IEEE_decimal128.
46
-
47
- <b>interchange binary & decimal</b> formats:
48
- IEEE_binary256, IEEE_binary512, IEEE_binary1024, IEEE_decimal192, IEEE_decimal256.
49
- Others can be defined with IEEE.interchange_binary and IEEE.interchange_decimal
50
- (see the IEEE module).
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 FltPnt
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_SINGLE.total_bits -> 32
95
-
96
- Numeric radix:
97
- puts IEEE_SINGLE.radix -> 2
98
-
99
- Digits of precision (radix-based)
100
- puts IEEE_SINGLE.significand_digits -> 24
101
-
102
- Minimum and maximum values of the radix-based exponent:
103
- puts IEEE_SINGLE.radix_min_exp -> -126
104
- puts IEEE_SINGLE.radix_max_exp -> 127
105
-
106
- Decimal precision
107
- puts IEEE_SINGLE.decimal_digits_stored -> 6
108
- puts IEEE_SINGLE.decimal_digits_necessary -> 9
109
-
110
- Minimum and maximum decimal exponents:
111
- puts IEEE_SINGLE.decimal_min_exp -> -37
112
- puts IEEE_SINGLE.decimal_max_exp -> 38
113
-
114
- ==Encode and decode numbers
115
-
116
- The <tt>from_</tt> methods of the floating-format classes generate a floating point value
117
- stored in a byte string from a variety of definitions:
118
- * <tt>from_integral_sign_significand_exponent</tt> defines the value by three integers:
119
- the sign (0 for +, 1 for -), the significand (coefficient or mantissa)
120
- and the exponent.
121
- * <tt>from_fmt</tt> : converts a text numeral (with an optional Nio format specifier)
122
- to a floating point value
123
- * <tt>from_number</tt> : converts a numerical value
124
- to a floating point representation.
125
-
126
- All these methods return an object of type Value that contains the encoded value (#bytes)
127
- and the Floating point format class (#fp_format).
128
-
129
- File.open('binary_file.dat','wb'){|f| f.write IEEE_EXTENDED.from_fmt('0.1').bytes}
130
-
131
- puts IEEE_EXTENDED.from_fmt('0.1').to_hex(true) -> CD CC CC CC CC CC CC CC FB 3F
132
- puts IEEE_EXTENDED.from_number(0.1).to_hex(true) -> CD CC CC CC CC CC CC CC FB 3F
133
- puts IEEE_EXTENDED.from_integral_sign_significand_exponent(0,123,-2).to_hex(true) -> 00 00 00 00 00 00 00 F6 03 40
134
- puts IEEE_DEC32.from_fmt('1.234').to_hex(true) -> 22 20 05 34
135
-
136
- A floating-point encoded value can be converted to useful formats wit the to_ methods:
137
- * <tt>to_integral_sign_significand_exponent</tt>
138
- * <tt>to_fmt</tt>
139
- * <tt>to_number</tt>
140
-
141
- puts IEEE_EXTENDED.to_number(File.read('binary_file.dat'))
142
- v = IEEE_EXTENDED.from_fmt('0.1')
143
- puts v.to_integral_sign_significand_exponent.inspect
144
- puts v.to_fmt
145
- puts v.to_number(Float)
146
-
147
- ==Special values:
148
-
149
- Let's show the decimal expression of some interesting values using
150
- 3 significative digits:
151
-
152
- fmt = Nio::Fmt.mode(:gen,3)
153
-
154
- puts IEEE_SINGLE.min_value.to_fmt(fmt) -> 1.4E-45
155
- puts IEEE_SINGLE.min_normalized_value.to_fmt(fmt) -> 1.18E-38
156
- puts IEEE_SINGLE.max_value.to_fmt(fmt) -> 3.4E38
157
- puts IEEE_SINGLE.epsilon.to_fmt(fmt) -> 1.19E-7
158
-
159
- ==Convert between formats
160
-
161
- v = IEEE_EXTENDED.from_fmt('1.1')
162
- v = v.convert_to(IEEE_SINGLE)
163
- v = v.convert_to(IEEE_DEC64)
164
-
165
-
166
- =Tools for the native floating point format
167
- This is an optional module (must be loaded explicitely because
168
- is somewhat intrusive; it adds methods to Float)
169
- that useful to explore or manipulate the native Float format.
170
-
171
- require 'float-formats/native'
172
- include FltPnt
173
-
174
- puts float_shortest_dec(1.0.next) -> 1.0000000000000002
175
- puts float_dec(1.0.prev) -> 0.99999999999999988897769753748434595763683319091796875
176
- puts float_dec(1.0.next) -> 1.0000000000000002220446049250313080847263336181640625
177
- puts float_dec(1.0.prev) -> 0.99999999999999988897769753748434595763683319091796875
178
- puts float_bin(1.0.next) -> 1.0000000000000000000000000000000000000000000000000001E0
179
- puts 1.0.next-1 == Float::EPSILON -> true
180
- puts float_significant_dec(Float::MIN_D) -> 5E-324
181
- puts float_significant_dec(Float::MIN_D.next) -> 1.0E-323
182
- puts float_significant_dec(Float::MAX_D.prev) -> 2.2250738585072004E-308
183
- puts float_significant_dec(Float::MAX_D) -> 2.2250738585072009E-308
184
- puts float_significant_dec(Float::MIN_N) -> 2.2250738585072014E-308
185
-
186
-
187
- =Defining new formats
188
-
189
- New formats are defined using one of the classes defined in float-formats/classes.rb
190
- and passing the necessary parameters in a hash to the constructor.
191
-
192
- For example, here we define a binary floating point 32-bits format with
193
- 22 bits for the significand, 9 for the exponent and 1 for the sign
194
- (these fields are allocated from least to most significant bits).
195
- We'll use excess notation with bias 127 for the exponent, interpreting
196
- the significand bits as a fractional number with the radix point after
197
- the first bit, which will be hidden:
198
- MY_FP = BinaryFormat.new(
199
- :fields=>[:significand,22,:exponent,9,:sign,1],
200
- :bias=>127, :bias_mode=>:normalized_significand,
201
- :hidden_bit=>true)
202
- Now we can encode values in this format, decode values, convet to other
203
- formats, query it's range, etc:
204
-
205
- puts MY_FP.from_fmt('0.1').to_bits_text(16) -> 1ee66666
206
- puts MY_FP.max_value.to_fmt(Nio::Fmt.prec(3)) -> 7.88E115
207
-
208
- You can look at float-formats/formats.rb to see how the built-in formats
209
- are defined.
210
-
211
- =License
212
-
213
- This code is free to use under the terms of the GNU GENERAL PUBLIC LICENSE.
214
-
215
- =Contact
216
-
217
- Nio has been developed by Javier Goizueta (mailto:javier@goizueta.info).
218
-
219
- You can contact me through Rubyforge:http://rubyforge.org/sendmessage.php?touser=25432
220
-
221
- =References
222
-
223
-
224
- [<i>Floating Point Representations.</i> C.B. Silio.]
225
- http://www.ece.umd.edu/class/enpm607.S2000/fltngpt.pdf
226
- Description of formats used in UNIVAC 1100, CDC 6600/7600, PDP-11, IEEE754, IBM360/370
227
-
228
- [<i>Floating-Point Formats.</i> John Savard.]
229
- http://www.quadibloc.com/comp/cp0201.htm
230
- Description of formats used in VAX and PDF-11
231
-
232
-
233
- ===IEEE754 binary formats
234
- [<i>IEEE-754 References.</i> Christopher Vickery.]
235
- http://babbage.cs.qc.edu/courses/cs341/IEEE-754references.html
236
-
237
- [<i>What Every Computer Scientist Should Know About Floating-Point Arithmetic.</i> David Goldberg.]
238
- http://docs.sun.com/source/806-3568/ncg_goldberg.html
239
-
240
-
241
- ===DPD/IEEE754r decimal formats
242
- [<i>Decimal Arithmetic Encoding. Strawman 4d.</i> Mike Cowlishaw.]
243
- http://www2.hursley.ibm.com/decimal/decbits.pdf
244
-
245
- [<i>A Summary of Densely Packed Decimal encoding.</i> Mike Cowlishaw.]
246
- http://www2.hursley.ibm.com/decimal/DPDecimal.html
247
-
248
- [<i>Packed Decimal Encoding IEEE-754-r.</i> J.H.M. Bonten.]
249
- http://home.hetnet.nl/mr_1/81/jhm.bonten/computers/bitsandbytes/wordsizes/ibmpde.htm
250
-
251
- [<i>DRAFT Standard for Floating-Point Arithmetic P754.</i> IEEE.]
252
- http://www.validlab.com/754R/drafts/archive/2007-10-05.pdf
253
-
254
-
255
-
256
- ===HP 10 digits calculators
257
-
258
- [<i>HP CPU and Programming</i>. David G.Hicks.]
259
- http://www.hpmuseum.org/techcpu.htm Description of calculator CPUs from the Museum of HP Calculators.
260
- [<i>HP 35 ROM step by step.</i> Jacques Laporte]
261
- http://www.jacques-laporte.org/HP35%20ROM.htm
262
- Description of HP35 registers.
263
- [<i>Scientific Pocket Calculator Extends Range of Built-In Functions.</i> Eric A. Evett, Paul J. McClellan, Joseph P. Tanzini.]
264
- Hewlett Packard Journal 1983-05 pgs 27-28. Describes format used in HP-15C.
265
-
266
-
267
- ===HP 12 digits calculators
268
- [<i>Software Internal Design Specification Volume I For the HP-71</i>. Hewlett Packard.]
269
- Available from http://www.hpmuseum.org/cd/cddesc.htm
270
- [<i>RPL PROGRAMMING GUIDE</i>]
271
- Excerpted from <i>RPL: A Mathematical Control Language</i>. by W. C. Wickes.
272
- Available at http://www.hpcalc.org/details.php?id=1743
273
-
274
- ===HP-3000
275
- [<i>A Pocket Calculator for Computer Science Professionals.</i> Eric A. Evett.]
276
- Hewlett Packard Journal 1983-05 pg 37. Describes format used in HP-3000
277
-
278
- ===IBM
279
- [<i>IBM Floating Point Architecture.</i> Wikipedia.]
280
- http://en.wikipedia.org/wiki/IBM_Floating_Point_Architecture
281
- [<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.]
282
- http://www.research.ibm.com/journal/rd/483/gerwig.html
283
-
284
- ===MBF
285
- [<i>Microsoft Knowledbase Article 35826</i>]
286
- http://support.microsoft.com/?scid=kb%3Ben-us%3B35826&x=17&y=12
287
- [<i>Microsoft MBF2IEEE library</i>]
288
- http://download.microsoft.com/download/vb30/install/1/win98/en-us/mbf2ieee.exe
289
-
290
- ===Borland
291
- [<i>An Overview of Floating Point Numbers.</i> Borland Developer Support Staff]
292
-
293
- [<i>Pascal Floating-Point Page.<i> J R Stockton.]
294
- http://www.merlyn.demon.co.uk/pas-real.htm
295
-
296
- ===8-bit micros
297
- This is the MS Basic format (BASIC09 for TRS-80 Color Computer, Dragon),
298
- also used in the Sinclair Spectrum.
299
-
300
- [<i>Numbers are followed by information not in listings</i>]
301
- Sinclair User October 1983 http://www.sincuser.f9.co.uk/019/helplne.htm
302
-
303
- [<i>Sinclair ZX Spectrum / Basic Programming.</i>. Steven Vickers.]
304
- Chapter 24. http://www.worldofspectrum.org/ZXBasicManual/zxmanchap24.html
305
-
306
-
307
-
308
- ===Apple II
309
- [<i>Floating Point Routines for the 6502</i> Roy Rankin and Steve Wozniak.]
310
- Dr. Dobb's Journal, August 1976, pages 17-19.
311
-
312
- ===C51
313
- [<i>Advanced Development System</i> Franklin Software, Inc.]
314
- http://www.fsinc.com/reference/html/com9anm.htm
315
-
316
- ===CDC6600
317
- [<i>CONTROL DATA 6400/6500/6600 COMPUTER SYSTEMS Reference Manual</i>]
318
- Manuals available at http://bitsavers.org/
319
-
320
-
321
- ===Cray
322
- [<i>CRAY-1 COMPUTER SYSTEM Hardware Reference Manual</i>]
323
- See pg 3-20 from 2240004 or pg 4-30 from HR-0808 or pg 4-21 from HP-0032.
324
- Manuals available at http://bitsavers.org/
325
-
326
- ===Wang 2200
327
- [<i>Internal Floating Point Representation</i>] http://www.wang2200.org/fp_format.html
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