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

It's been more than a decade since Typical Programmer Greg Jorgensen taught the word about Abject-Oriented Programming.

Much of what he said still applies, but other things have changed. Languages in the Abject-Oriented space have been borrowing ideas from another paradigm entirely—and then everyone realized that languages like Python, Ruby, and JavaScript had been doing it for years and just hadn't noticed (because these languages do not require you to declare what you're doing, or even to know what you're doing). Meanwhile, new hybrid languages borrow freely from both paradigms.

This other paradigm—which is actually older, but was largely constrained to university basements until recent years—is called Functional Addiction.
5

I haven't posted anything new in a couple years (partly because I attempted to move to a different blogging platform where I could write everything in markdown instead of HTML but got frustrated—which I may attempt again), but I've had a few private comments and emails on some of the old posts, so I decided to do some followups.

A couple years ago, I wrote a blog post on greenlets, threads, and processes.
6

Looking before you leap

Python is a duck-typed language, and one where you usually trust EAFP ("Easier to Ask Forgiveness than Permission") over LBYL ("Look Before You Leap"). In Java or C#, you need "interfaces" all over the place; you can't pass something to a function unless it's an instance of a type that implements that interface; in Python, as long as your object has the methods and other attributes that the function needs, no matter what type it is, everything is good.
1

Background

Currently, CPython’s internal bytecode format stores instructions with no args as 1 byte, instructions with small args as 3 bytes, and instructions with large args as 6 bytes (actually, a 3-byte EXTENDED_ARG followed by a 3-byte real instruction). While bytecode is implementation-specific, many other implementations (PyPy, MicroPython, …) use CPython’s bytecode format, or variations on it.

Python exposes as much of this as possible to user code.
6

If you want to skip all the tl;dr and cut to the chase, jump to Concrete Proposal.

Why can’t we write list.len()? Dunder methods C++ Python Locals What raises on failure? Method objects What about set and delete? Data members Namespaces Bytecode details Lookup overrides Introspection C API Concrete proposal CPython Analysis

Why can’t we write list.len()?

Python is an OO language. To reverse a list, you call lst.reverse(); to search a list for an element, you call lst.index().
8

Many people, when they first discover the heapq module, have two questions:

Why does it define a bunch of functions instead of a container type? Why don't those functions take a key or reverse parameter, like all the other sorting-related stuff in Python? Why not a type?

At the abstract level, it's often easier to think of heaps as an algorithm rather than a data structure.
1

Currently, in CPython, if you want to process bytecode, either in C or in Python, it’s pretty complicated.

The built-in peephole optimizer has to do extra work fixing up jump targets and the line-number table, and just punts on many cases because they’re too hard to deal with. PEP 511 proposes a mechanism for registering third-party (or possibly stdlib) optimizers, and they’ll all have to do the same kind of work.
3

One common "advanced question" on places like StackOverflow and python-list is "how do I dynamically create a function/method/class/whatever"? The standard answer is: first, some caveats about why you probably don't want to do that, and then an explanation of the various ways to do it when you really do need to.

But really, creating functions, methods, classes, etc. in Python is always already dynamic.

Some cases of "I need a dynamic function" are just "Yeah? And you've already got one".
1

A few years ago, Cesare di Mauro created a project called WPython, a fork of CPython 2.6.4 that “brings many optimizations and refactorings”. The starting point of the project was replacing the bytecode with “wordcode”. However, there were a number of other changes on top of it.

I believe it’s possible that replacing the bytecode with wordcode would be useful on its own.
1

Many languages have a for-each loop. In some, like Python, it’s the only kind of for loop:

for i in range(10): print(i) In most languages, the loop variable is only in scope within the code controlled by the for loop,[1] except in languages that don’t have granular scopes at all, like Python.[2]

So, is that i a variable that gets updated each time through the loop or is it a new constant that gets defined each time through the loop?

Almost every language treats it as a reused variable.
4
Blog Archive
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.