float-formats 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2762f17e4a4d466ccfd26ecfa5e4bfc5f72a4de
4
+ data.tar.gz: 725f2276a8d2352a480738f6b80347afdee5429e
5
+ SHA512:
6
+ metadata.gz: 934167b2462593a9dc91a848b01eabdee00043f63fb92aefa4d265c635aa07efd2b67e53b2d1fc5b7f5efb7365ca079f1d93731acd5d01cadf9de4b5c4973b35
7
+ data.tar.gz: 811b80dd4a9fb97cf88913a0f480087e71dc4d4ff6bb9a4b6ee032cf5bf56219495fe7249b185caad9af54e4cf1241cfa41ba369364678ba7072643892d36501
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.2.1 2015-03-31
2
+
3
+ * Compatibility issues with Ruby 1.9/2
4
+
1
5
  == 0.2.0 2009-08-06
2
6
 
3
7
  * New redefined interface
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2007-2015 Javier Goizueta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,331 @@
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
+ To install the gem manually:
15
+
16
+ gem install float-formats
17
+
18
+ You can find the code in GitHub:
19
+
20
+ * http://github.com/jgoizueta/float-formats/
21
+
22
+ #Predefined formats
23
+
24
+ A number of common formats are defined as constants in the Flt module:
25
+
26
+ ##IEEE 754-2008
27
+
28
+ **Binary** floating point representations in little endian order:
29
+
30
+ * IEEE_binary16 (half precision),
31
+ * IEEE_binary32 (single precision),
32
+ * IEEE_binary64 (double precision),
33
+ * IEEE_binary80 (extended), IEEE_binary128 (quadruple precision) and
34
+ as little endian: IEEE_binary16_BE, etc.
35
+
36
+ **Decimal** formats (using DPD):
37
+
38
+ * IEEE_decimal32, IEEE_decimal64 and IEEE_decimal128.
39
+
40
+ **Interchange binary & decimal** formats:
41
+
42
+ * IEEE_binary256, IEEE_binary512, IEEE_binary1024, IEEE_decimal192, IEEE_decimal256.
43
+
44
+ Others can be defined with IEEE.interchange_binary and IEEE.interchange_decimal
45
+ (see the IEEE module).
46
+
47
+ ##Legacy
48
+
49
+ Formats of historical interest, some of which are found
50
+ in file formats still in use.
51
+
52
+ **Mainframe/supercomputer** formats:
53
+ Univac 1100 (UNIVAC_SINGLE, UNIVAC_DOUBLE),
54
+ IBM 360 etc. (IBM32, IBM64 and IBM128),
55
+ CDC 6600/7600: (CDC_SINGLE, CDC_DOUBLE),
56
+ Cray-1: (CRAY).
57
+
58
+ **Minis**: PDP11 and Vaxes: (PDP11_F, PDP11_D, VAX_F, VAX_D, VAX_G and VAX_H),
59
+ HP3000: (XS256, XS256_DOUBLE),
60
+ Wang 2200: (WANG2200).
61
+
62
+ **Microcomputers** (software implementations):
63
+ Apple II: (APPLE),
64
+ Microsoft Basic, Spectrum, etc.: (XS128),
65
+ Microsoft Quickbasic: (MBF_SINGLE, MBF_DOUBLE),
66
+ Borland Pascal: (BORLAND48).
67
+
68
+ **Embedded systems**:
69
+ Formats used in the Intel 8051 by the C51 compiler:
70
+ (C51_BCD_FLOAT, C51_BCD_DOUBLE and C51_BCD_LONG_DOUBLE).
71
+
72
+ ##Calculators
73
+
74
+ Formats used in HP RPL calculators: (RPL, RPL_X),
75
+ HP-71B formats (HP71B, HP71B_X)
76
+ and classic HP 10 digit calculators: (HP_CLASSIC).
77
+
78
+ #Using the pre-defined formats
79
+
80
+ require 'rubygems'
81
+ require 'float-formats'
82
+ include Flt
83
+
84
+ The properties of the floating point formats can be queried (which can be
85
+ used for tables or reports comparing different formats):
86
+
87
+ Size in bits of the representations:
88
+
89
+ puts IEEE_binary32.total_bits # -> 32
90
+
91
+ Numeric radix:
92
+
93
+ puts IEEE_binary32.radix # -> 2
94
+
95
+ Digits of precision (radix-based)
96
+
97
+ puts IEEE_binary32.significand_digits # -> 24
98
+
99
+ Minimum and maximum values of the radix-based exponent:
100
+
101
+ puts IEEE_binary32.radix_min_exp # -> -126
102
+ puts IEEE_binary32.radix_max_exp # -> 127
103
+
104
+ Decimal precision
105
+
106
+ puts IEEE_binary32.decimal_digits_stored # -> 6
107
+ puts IEEE_binary32.decimal_digits_necessary # -> 9
108
+
109
+ Minimum and maximum decimal exponents:
110
+
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
+
119
+ * Using three integers:
120
+ the sign (+1 for +, -1 for -), the significand (coefficient or mantissa)
121
+ and the exponent.
122
+ * From a text numeral (with an optional Nio format specifier)
123
+ * From a number : converts a numerical value
124
+ to a floating point representation.
125
+
126
+ Examples:
127
+
128
+ File.open('binary_file.dat','wb'){|f| f.write IEEE_binary80('0.1').to_bytes}
129
+ puts IEEE_binary80('0.1').to_hex(true) # -> CD CC CC CC CC CC CC CC FB 3F
130
+ puts IEEE_binary80(0.1).to_hex(true) # -> CD CC CC CC CC CC CC CC FB 3F
131
+ puts IEEE_binary80(+1,123,-2).to_hex(true) # -> 00 00 00 00 00 00 00 F6 03 40
132
+ puts IEEE_decimal32('1.234').to_hex(true) # -> 22 20 05 34
133
+
134
+ A floating-point encoded value can be converted to useful formats with the to_ and similar methods:
135
+
136
+ * `split` (split as integral sign, significand, exponent)
137
+ * `to_text`
138
+ * `to(num_class)`
139
+
140
+ Examples:
141
+
142
+ v = IEEE_binary80.from_bytes(File.read('binary_file.dat'))
143
+ puts v.to(Rational) # -> 1/10
144
+ puts v.split.inspect # -> [1, 14757395258967641293, -67]
145
+ puts v.to_text # -> 0.1
146
+ puts v.to(Float) # -> 0.1
147
+ puts v.to_hex # -> CDCCCCCCCCCCCCCCFB3F
148
+ puts v.to_bits # -> 00111111111110111100110011001100110011001100110011001100110011001100110011001101
149
+ puts v.to_bits_text(16) # -> 3ffbcccccccccccccccd
150
+
151
+ ##Special values:
152
+
153
+ Let's show the decimal expression of some interesting values using
154
+ 3 significative digits:
155
+
156
+ fmt = Nio::Fmt.mode(:gen,3)
157
+ puts IEEE_SINGLE.min_value.to_text(fmt) # -> 2E-45
158
+ puts IEEE_SINGLE.min_normalized_value.to_text(fmt) # -> 1.18E-38
159
+ puts IEEE_SINGLE.max_value.to_text(fmt) # -> 3.4E38
160
+ puts IEEE_SINGLE.epsilon.to_text(fmt) # -> 1.19E-7
161
+
162
+ ##Convert between formats
163
+
164
+ v = IEEE_EXTENDED.from_text('1.1')
165
+ v = v.convert_to(IEEE_SINGLE)
166
+ v = v.convert_to(IEEE_DEC64)
167
+
168
+ #Tools for the native floating point format
169
+
170
+ This is an optional module to perform conversions and manipulate the native Float format.
171
+
172
+ require 'float-formats/native'
173
+ include Flt
174
+
175
+ puts float_shortest_dec(0.1) # -> 0.1
176
+ puts float_significant_dec(0.1) # -> 0.10000000000000001
177
+ puts float_dec(0.1) # -> 0.1000000000000000055511151231257827021181583404541015625
178
+ puts float_bin(0.1) # -> 1.100110011001100110011001100110011001100110011001101E-4
179
+ puts hex_from_float(0.1) # -> 0x1999999999999ap-56
180
+
181
+ puts float_significant_dec(Float::MIN_D) # -> 5E-324
182
+ puts float_significant_dec(Float::MAX_D) # -> 2.2250738585072009E-308
183
+ puts float_significant_dec(Float::MIN_N) # -> 2.2250738585072014E-308
184
+
185
+ Together with flt/sugar (from Flt) can be use to explore or work with Floats:
186
+
187
+ require 'flt/sugar'
188
+
189
+ puts 1.0.next_plus-1 == Float::EPSILON # -> true
190
+ puts float_shortest_dec(1.0.next_plus) # -> 1.0000000000000002
191
+ puts float_dec(1.0.next_minus) # -> 0.99999999999999988897769753748434595763683319091796875
192
+ puts float_dec(1.0.next_plus) # -> 1.0000000000000002220446049250313080847263336181640625
193
+ puts float_bin(1.0.next_plus) # -> 1.0000000000000000000000000000000000000000000000000001E0
194
+ puts float_bin(1.0.next_minus) # -> 1.1111111111111111111111111111111111111111111111111111E-1
195
+
196
+ puts float_significant_dec(Float::MIN_D.next_plus) # -> 1.0E-323
197
+ puts float_significant_dec(Float::MAX_D.next_minus) # -> 2.2250738585072004E-308
198
+
199
+ #Defining new formats
200
+
201
+ New formats are defined using one of the classes defined in float-formats/classes.rb
202
+ and passing the necessary parameters in a hash to the constructor.
203
+
204
+ For example, here we define a binary floating point 32-bits format with
205
+ 22 bits for the significand, 9 for the exponent and 1 for the sign
206
+ (these fields are allocated from least to most significant bits).
207
+ We'll use excess notation with bias 127 for the exponent, interpreting
208
+ the significand bits as a fractional number with the radix point after
209
+ the first bit, which will be hidden:
210
+
211
+ Flt.define(
212
+ :MY_FP, BinaryFormat,
213
+ :fields=>[:significand,22,:exponent,9,:sign,1],
214
+ :bias=>127, :bias_mode=>:scientific_significand,
215
+ :hidden_bit=>true
216
+ )
217
+
218
+ Now we can encode values in this format, decode values, convet to other
219
+ formats, query it's range, etc:
220
+
221
+ puts MY_FP('0.1').to_bits_text(16) # -> 1ee66666
222
+ puts MY_FP.max_value.to_text(Nio::Fmt.prec(3)) # -> 7.88E115
223
+
224
+ You can look at float-formats/formats.rb to see how the built-in formats
225
+ are defined.
226
+
227
+ #License
228
+
229
+ This code is free to use under the terms of the MIT license.
230
+
231
+ #References
232
+
233
+ [*Floating Point Representations.* C.B. Silio.](http://www.ece.umd.edu/class/enpm607.S2000/fltngpt.pdf)
234
+ Description of formats used in UNIVAC 1100, CDC 6600/7600, PDP-11, IEEE754, IBM360/370
235
+
236
+ [*Floating-Point Formats.* John Savard.](http://www.quadibloc.com/comp/cp0201.htm)
237
+ Description of formats used in VAX and PDF-11
238
+
239
+ ###IEEE754 binary formats
240
+
241
+ [*IEEE-754 References.* Christopher Vickery.](http://babbage.cs.qc.edu/courses/cs341/IEEE-754references.html)
242
+
243
+ [*What Every Computer Scientist Should Know About Floating-Point Arithmetic.* David Goldberg.](http://docs.sun.com/source/806-3568/ncg_goldberg.html)
244
+
245
+ ###DPD/IEEE754r decimal formats
246
+
247
+ [*Decimal Arithmetic Encoding. Strawman 4d.* Mike Cowlishaw.](http://www2.hursley.ibm.com/decimal/decbits.pdf)
248
+
249
+ [*A Summary of Densely Packed Decimal encoding.* Mike Cowlishaw.](http://www2.hursley.ibm.com/decimal/DPDecimal.html)
250
+
251
+ [*Packed Decimal Encoding IEEE-754-r.* J.H.M. Bonten.](http://home.hetnet.nl/mr_1/81/jhm.bonten/computers/bitsandbytes/wordsizes/ibmpde.htm)
252
+
253
+ [*DRAFT Standard for Floating-Point Arithmetic P754.* IEEE.](http://www.validlab.com/754R/drafts/archive/2007-10-05.pdf)
254
+
255
+ ###HP 10 digits calculators
256
+
257
+ [*HP CPU and Programming*. David G.Hicks.](http://www.hpmuseum.org/techcpu.htm)
258
+ Description of calculator CPUs from the Museum of HP Calculators.
259
+
260
+ [*HP 35 ROM step by step.* Jacques Laporte](http://www.jacques-laporte.org/HP35%20ROM.htm)
261
+ Description of HP35 registers.
262
+
263
+ *Scientific Pocket Calculator Extends Range of Built-In Functions.* 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
+ ###HP 12 digits calculators
267
+
268
+ *Software Internal Design Specification Volume I For the HP-71*. Hewlett Packard.
269
+ Available from http://www.hpmuseum.org/cd/cddesc.htm
270
+
271
+ *RPL PROGRAMMING GUIDE*
272
+ Excerpted from *RPL: A Mathematical Control Language*. by W. C. Wickes.
273
+ Available at http://www.hpcalc.org/details.php?id=1743
274
+
275
+ ###HP-3000
276
+
277
+ *A Pocket Calculator for Computer Science Professionals.* Eric A. Evett.
278
+ Hewlett Packard Journal 1983-05 pg 37. Describes format used in HP-3000
279
+
280
+ ###IBM
281
+
282
+ [*IBM Floating Point Architecture.* Wikipedia.](http://en.wikipedia.org/wiki/IBM_Floating_Point_Architecture)
283
+
284
+ [*The IBM eServer z990 floating-point unit*. G. Gerwig, H. Wetter, E. M. Schwarz, J. Haess, C. A. Krygowski, B. M. Fleischer and M. Kroener.](http://www.research.ibm.com/journal/rd/483/gerwig.html)
285
+
286
+ ###MBF
287
+
288
+ [*Microsoft Knowledbase Article 35826*](http://support.microsoft.com/?scid=kb%3Ben-us%3B35826&x=17&y=12)
289
+
290
+ [*Microsoft MBF2IEEE library*](http://download.microsoft.com/download/vb30/install/1/win98/en-us/mbf2ieee.exe)
291
+
292
+ ###Borland
293
+
294
+ *An Overview of Floating Point Numbers.* Borland Developer Support Staff
295
+
296
+ [*Pascal Floating-Point Page.* J R Stockton.](http://www.merlyn.demon.co.uk/pas-real.htm)
297
+
298
+ ###8-bit micros
299
+
300
+ This is the MS Basic format (BASIC09 for TRS-80 Color Computer, Dragon),
301
+ also used in the Sinclair Spectrum.
302
+
303
+ *Numbers are followed by information not in listings*
304
+ Sinclair User October 1983 http://www.sincuser.f9.co.uk/019/helplne.htm
305
+
306
+ *Sinclair ZX Spectrum / Basic Programming.*. Steven Vickers.
307
+ Chapter 24. http://www.worldofspectrum.org/ZXBasicManual/zxmanchap24.html
308
+
309
+ ###Apple II
310
+
311
+ *Floating Point Routines for the 6502* Roy Rankin and Steve Wozniak.
312
+ Dr. Dobb's Journal, August 1976, pages 17-19.
313
+
314
+ ###C51
315
+
316
+ [*Advanced Development System* Franklin Software, Inc.](http://www.fsinc.com/reference/html/com9anm.htm)
317
+
318
+ ###CDC6600
319
+
320
+ *CONTROL DATA 6400/6500/6600 COMPUTER SYSTEMS Reference Manual*
321
+ Manuals available at http://bitsavers.org/
322
+
323
+ ###Cray
324
+
325
+ *CRAY-1 COMPUTER SYSTEM Hardware Reference Manual*
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
+
331
+ [*Internal Floating Point Representation*](http://www.wang2200.org/fp_format.html)
data/Rakefile CHANGED
@@ -1,38 +1,19 @@
1
- # Look in the tasks/setup.rb file for the various options that can be
2
- # configured in this Rakefile. The .rake files in the tasks directory
3
- # are where the options are used.
1
+ require "bundler/gem_tasks"
4
2
 
5
- begin
6
- require 'bones'
7
- Bones.setup
8
- rescue LoadError
9
- load 'tasks/setup.rb'
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
10
8
  end
11
9
 
12
- ensure_in_path 'lib'
13
- #require 'float-formats'
14
- require 'float-formats/version'
10
+ require 'rdoc/task'
11
+ Rake::RDocTask.new do |rdoc|
12
+ version = Flt::Frmts::VERSION
15
13
 
16
- task :default => 'spec:run'
17
-
18
- depend_on 'flt', '1.0.0'
19
- depend_on 'nio', '0.2.4'
20
-
21
- PROJ.name = 'float-formats'
22
- PROJ.description = "Floating-Point Formats"
23
- PROJ.authors = 'Javier Goizueta'
24
- PROJ.email = 'javier@goizueta.info'
25
- PROJ.version = Flt::FORMATS_VERSION::STRING
26
- PROJ.rubyforge.name = 'float-formats'
27
- PROJ.url = "http://#{PROJ.rubyforge.name}.rubyforge.org"
28
- PROJ.rdoc.opts = [
29
- "--main", "README.txt",
30
- '--title', 'Float-Formats Documentation',
31
- "--opname", "index.html",
32
- "--line-numbers",
33
- "--inline-source"
34
- ]
35
- depend_on 'nio', '>=0.2.0'
36
- depend_on 'flt', '>=1.0.0'
37
-
38
- # EOF
14
+ rdoc.rdoc_dir = 'rdoc'
15
+ rdoc.title = "Float-Formats #{version}"
16
+ rdoc.main = "README.md"
17
+ rdoc.rdoc_files.include('README*')
18
+ rdoc.rdoc_files.include('lib/**/*.rb')
19
+ end