In IEEE Floats and Python, I chose to use a tiny "binary6" type because it's easy to show a table of values that helps clarify things.

Someone suggested that you could just as easily write a table of binary64 values, ellipsizing large chunks of it, and it would also be useful for clarifying things. And I think he's right.

The Table

    sign  exponent significand    binary scientific              decimal
       0 000...000 0000...0000    0.0...0000e-1022 = 0           0.0
       0 000...000 0000...0001    0.0...0001e-1022 = 1.0e-1074   5e-324
       0 000...000 0000...0010    0.0...0010e-1022 = 1.0e-1073   1e-323
       0 000...000 0000...0011    0.0...0011e-1022 = 1.1e-1073   1.5e-323
       0 000...000 0000...0100    0.0...0100e-1022 = 1.0e-1072   2e-323
       0 000...000 0000...0101    0.0...0101e-1022 = 1.01e-1072  2.5e-323
       0 000...000 0000...0110    0.0...0110e-1022 = 1.10e-1072  3e-323
       0 000...000 0000...0111    0.0...0111e-1022 = 1.11e-1072  3.5e-323
       0 000...000 0000...1100    0.0...1100e-1022 = 1.0e-1071   4e-323
       0 000...000 0000...1101    0.0...1101e-1022 = 1.01e-1071  4.4e-323
       0 000...000 0000...1110    0.0...1110e-1022 = 1.10e-1072  5e-323
       0 000...000 0000...1111    0.0...1111e-1022 = 1.11e-1072  5.4e-323
       ...
       0 000...000 1111...1110    0.1...1110e-1022 = 1.11e-1023  2.2250738585072004e-308
       0 000...000 1111...1111    0.1...1111e-1022 = 1.11e-1023  2.225073858507201e-308
       0 000...001 0000...0000    1.0...0000e-1022 = 1.0e-1022   2.2250738585072014e-308
       0 000...001 0000...0001    1.0...0001e-1022               2.225073858507202e-308
       ...
       0 000...001 1111...1111    1.1...1111e-1022               4.4501477170144023e-308
       0 000...010 0000...0000    1.0...0000e-1021               4.450147717014403e-308
       ...
       0 011...110 1111...1111    1.1...111e-1                   0.9999999999999999
       0 011...111 0000...0000    1.0...000e0                    1.0
       0 011...111 0000...0001    1.0...001e0                    1.0000000000000002
       ...
       0 111...101 1111...1111    1.1...111e+1022                8.988465674311579e+307
       0 111...110 0000...0000    1.0...000e+1023                8.98846567431158e+307
       0 111...110 0000...0001    1.0...001e+1023                8.988465674311582e+307
       ...
       0 111...110 1111...1110    1.1...110e+1023                1.7976931348623155e+308
       0 111...110 1111...1111    1.1...111e+1023                1.7976931348623157e+308
       0 111...111 0000...0000    inf                            inf
       0 111...111 0000...0001    snan1                          snan1
       0 111...111 0000...0010    snan10                         snan2
       ...
       0 111...111 0111...1111    snan1...1                      snan2251799813685247
       0 111...111 1000...0000    qnan0                          qnan0
       0 111...111 1000...0001    qnan1                          qnan1
       0 111...111 1000...0010    qnan10                         qnan2
       0 111...111 1111...1111    qnan1...1                      qnan2251799813685247
       1 000...000 0000...0000    -0.0...0000e-1022 = -0         -0.0
       0 000...000 0000...0001    -0.0...0001e-1022 = -1.0e-1074 -5e-324
       ...
       1 111...111 0000...0000    -inf                           -inf
       1 111...111 0000...0001    -snan1                         -snan1
       ...
       1 111...111 1000...0000    -qnan0                         -qnan0
       1 111...111 1111...1111    -qnan1...1                     -qnan2251799813685247

Notes

For finite numbers, I've displayed the decimal representation the same way Python 3.5 does (the shorted decimal fraction that rounds to the same binary fraction). For NaN values, Python displays them all as just "nan". Some C libraries will display something like "QNaN(1)" or "qnan1" or the like, so I've chosen to do that for clarity.

As mentioned in the previous post, this is for IEEE 754-2008 binary64, even though your platform (assuming you're not reading this in some far-future era where C11 and Python 3.5 are distant memories but blogspot posts are still relevant) probably uses the older IEEE 754-1985 double type, which is a slightly looser version of the same thing. So if you're on some older platform, a few things may be different (e.g., Irix on MIPS has snan and qnan reversed).

Generating the table yourself

Using the floatextras module, you can generate the values yourself. If it's not quite as easy as maybe it could be, suggestions are welcome.

One trick that helps a bit is that the make_tuple function can take any sequence of digits, or an int, which will be interpreted as an unsigned 52-bit integer:
    >>> from_tuple((0, -1, 0))
    1.9999999999999998
Unfortunately, you still have to turn that into bits to print the significand column of the table. I guess you could recover them from the float with as_tuple, but that seems silly. (I used the bitstring library again.) Maybe a function for generating table rows given either a float or a tuple and a +/- range would be useful?
1

View comments

  1. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles python certification

    ReplyDelete
Hybrid Programming
Hybrid Programming
5
Greenlets vs. explicit coroutines
Greenlets vs. explicit coroutines
6
ABCs: What are they good for?
ABCs: What are they good for?
1
A standard assembly format for Python bytecode
A standard assembly format for Python bytecode
6
Unified call syntax
Unified call syntax
8
Why heapq isn't a type
Why heapq isn't a type
1
Unpacked Bytecode
Unpacked Bytecode
3
Everything is dynamic
Everything is dynamic
1
Wordcode
Wordcode
1
For-each loops should define a new variable
For-each loops should define a new variable
4
Views instead of iterators
Views instead of iterators
2
How lookup _could_ work
How lookup _could_ work
2
How lookup works
How lookup works
7
How functions work
How functions work
2
Why you can't have exact decimal math
Why you can't have exact decimal math
2
Can you customize method resolution order?
Can you customize method resolution order?
1
Prototype inheritance is inheritance
Prototype inheritance is inheritance
1
Pattern matching again
Pattern matching again
The best collections library design?
The best collections library design?
1
Leaks into the Enclosing Scope
Leaks into the Enclosing Scope
2
Iterable Terminology
Iterable Terminology
8
Creating a new sequence type is easy
Creating a new sequence type is easy
2
Going faster with NumPy
Going faster with NumPy
2
Why isn't asyncio too slow?
Why isn't asyncio too slow?
Hacking Python without hacking Python
Hacking Python without hacking Python
1
How to detect a valid integer literal
How to detect a valid integer literal
2
Operator sectioning for Python
Operator sectioning for Python
1
If you don't like exceptions, you don't like Python
If you don't like exceptions, you don't like Python
2
Spam, spam, spam, gouda, spam, and tulips
Spam, spam, spam, gouda, spam, and tulips
And now for something completely stupid…
And now for something completely stupid…
How not to overuse lambda
How not to overuse lambda
1
Why following idioms matters
Why following idioms matters
1
Cloning generators
Cloning generators
5
What belongs in the stdlib?
What belongs in the stdlib?
3
Augmented Assignments (a += b)
Augmented Assignments (a += b)
11
Statements and Expressions
Statements and Expressions
3
An Abbreviated Table of binary64 Values
An Abbreviated Table of binary64 Values
1
IEEE Floats and Python
IEEE Floats and Python
Subtyping and Ducks
Subtyping and Ducks
1
Greenlets, threads, and processes
Greenlets, threads, and processes
6
Why don't you want getters and setters?
Why don't you want getters and setters?
8
The (Updated) Truth About Unicode in Python
The (Updated) Truth About Unicode in Python
1
How do I make a recursive function iterative?
How do I make a recursive function iterative?
1
Sockets and multiprocessing
Sockets and multiprocessing
Micro-optimization and Python
Micro-optimization and Python
3
Why does my 100MB file take 1GB of memory?
Why does my 100MB file take 1GB of memory?
1
How to edit a file in-place
How to edit a file in-place
ADTs for Python
ADTs for Python
5
A pattern-matching case statement for Python
A pattern-matching case statement for Python
2
How strongly typed is Python?
How strongly typed is Python?
How do comprehensions work?
How do comprehensions work?
1
Reverse dictionary lookup and more, on beyond z
Reverse dictionary lookup and more, on beyond z
2
How to handle exceptions
How to handle exceptions
2
Three ways to read files
Three ways to read files
2
Lazy Python lists
Lazy Python lists
2
Lazy cons lists
Lazy cons lists
1
Lazy tuple unpacking
Lazy tuple unpacking
3
Getting atomic writes right
Getting atomic writes right
Suites, scopes, and lifetimes
Suites, scopes, and lifetimes
1
Swift-style map and filter views
Swift-style map and filter views
1
Inline (bytecode) assembly
Inline (bytecode) assembly
Why Python (or any decent language) doesn't need blocks
Why Python (or any decent language) doesn't need blocks
18
SortedContainers
SortedContainers
1
Fixing lambda
Fixing lambda
2
Arguments and parameters, under the covers
Arguments and parameters, under the covers
pip, extension modules, and distro packages
pip, extension modules, and distro packages
Python doesn't have encapsulation?
Python doesn't have encapsulation?
3
Grouping into runs of adjacent values
Grouping into runs of adjacent values
dbm: not just for Unix
dbm: not just for Unix
How to use your self
How to use your self
1
Tkinter validation
Tkinter validation
7
What's the deal with ttk.Frame.__init__(self, parent)
What's the deal with ttk.Frame.__init__(self, parent)
1
Does Python pass by value, or by reference?
Does Python pass by value, or by reference?
9
"if not exists" definitions
"if not exists" definitions
repr + eval = bad idea
repr + eval = bad idea
1
Solving callbacks for Python GUIs
Solving callbacks for Python GUIs
Why your GUI app freezes
Why your GUI app freezes
21
Using python.org binary installations with Xcode 5
Using python.org binary installations with Xcode 5
defaultdict vs. setdefault
defaultdict vs. setdefault
1
Lazy restartable iteration
Lazy restartable iteration
2
Arguments and parameters
Arguments and parameters
3
How grouper works
How grouper works
1
Comprehensions vs. map
Comprehensions vs. map
2
Basic thread pools
Basic thread pools
Sorted collections in the stdlib
Sorted collections in the stdlib
4
Mac environment variables
Mac environment variables
Syntactic takewhile?
Syntactic takewhile?
4
Can you optimize list(genexp)
Can you optimize list(genexp)
MISRA-C and Python
MISRA-C and Python
1
How to split your program in two
How to split your program in two
How methods work
How methods work
3
readlines considered silly
readlines considered silly
6
Comprehensions for dummies
Comprehensions for dummies
Sockets are byte streams, not message streams
Sockets are byte streams, not message streams
9
Why you don't want to dynamically create variables
Why you don't want to dynamically create variables
7
Why eval/exec is bad
Why eval/exec is bad
Iterator Pipelines
Iterator Pipelines
2
Why are non-mutating algorithms simpler to write in Python?
Why are non-mutating algorithms simpler to write in Python?
2
Sticking with Apple's Python 2.7
Sticking with Apple's Python 2.7
Blog Archive
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.