Trying to divide the smallest hexadecimal number

You can run into problems relying on the averages of floating point numbers. This is something I think about often after reading Sinan Ünür’s How you average numbers matters. I thought about it again when I read Honza Brabec’s Mean of two floating point numbers can be dangerous. Despite the hyperbole of “can be dangerous” (see “Considered Harmful” Essays Considered Harmful), it certainly can do something that you don’t intend.

Honza wanted to add two floating point numbers that differed by the smallest unit possible. If you try to average that, what do you get? There’s no representable number between the two. For languages that rely on the iron to determine number ranges (unlike Perl 6 Rats, by the way), you will always have this problem. You’ve sacrificed infinite accuracy for speed.

The hexadecimal floating point format Perl added in v5.22 makes it easy to try this in Perl. Last summer I showcased using Inline::C to look at the hexadecimal format.

That 0x1p-52 is the smallest number we can get to in 64-bit numbers. One bit is the sign, the exponent takes 11 bits, and the rest are left for the mantissa. That’s one bit set in the 52 bits available in the mantissa, and it’s the least bit.

use v5.22;

# smallest representable interval in IEEE 754
my $quantum = 0x1p-52;

my $d1 = 0x1p0;
my $d2 = $d1 + $quantum;
my $d3 = ( $d2 + $d1 ) / 2;

say "q:  ", sprintf "%a", $quantum;
say "d1: ", sprintf "%a", $d1;
say "d2: ", sprintf "%a", $d2;
say "d3: ", sprintf "%a", $d3;

if( $d3 == $d1 ) {
	say "d1 and d3 is the same!";
elsif( $d3 == $d2 ) {
	say "d2 and d3 is the same!";

When I run that I see that the average is the same as the $d1:

q:  0x1p-52
d1: 0x1p+0
d2: 0x1.0000000000001p+0
d3: 0x1p+0
d1 and d3 is the same!

There’s no representable number between $d1 and $d2. The value of $quantum is indivisible (hence the name). The result of the division has to be something, and it ends up being the lower number again.

In Honza’s case, that the number didn’t change put him into an infinite loop. Dangerous? He doesn’t say what he was doing. Maybe he’s working on machines that deliver radiation where a software error can kill people. If you are working on something that is important, you should explore the bounds of your functions (perhaps with tests, as we describe in the book).

Using Inline::C to look at a number’s double representation

Since Perl v5.22 added hexadecimal floating-point numbers, I investigated how floating point numbers are actually represented. These are specified in IEEE 754 and are something you’ve probably taken for granted. I can use Inline::C to play with these. » Read more…

Pre-compiled patterns retain their settings if they are interpolated

The qr// operator precompiles a regular expression and returns a reference to that compiled pattern. You can use that reference with the binding operator to perform a match, interpolate the reference into the match or substitution operators, or combine the reference with other references or literal patterns to make a larger pattern. We cover this in Chapter 9, “Regular Expression References”, but don’t emphasize what happens with regex flags when you combine patterns. » Read more…

Second printing available

Intermediate Perl 2nd edition’s second printing is now available. This contains fixes for almost all of the reported errata, but it otherwise the same content.

If you’ve bought your ebook through O’Reilly, you should have it available in your O’Reilly account. Look at your O’Reilly products list. I have the Alpaca under mine and I can immediately download the format I want or send them to Dropbox.

The Alpaca gets a second printing

Most tech books have trouble selling even a couple thousand copies, which makes it tough for a publisher to figure out how many to print at first. Print too many and they’ll just sit there, or worse. The publishing industry is a bitch because the book shops can returned unsold inventory and get money back. On every royalty statement, I have a “reserve withheld” and a “reserve returned”. The publisher reserves some of my royalty in case the book stores return books. After a certain period, they return that. But, they then withhold more. That’s just the way it is. » Read more…

Clarifying local::lib and cpan in Chapter 2

local::lib is highlighted in Intermediate Perl when I go through the CPAN tools in Chapter 2. Each of the tools has a slightly different set of features and I try to steal the good one. I added a local::lib to cpan so you can add the local::lib defaults for a one-shot installation process. This steals a feature from cpanm which has a --local-lib option: » Read more…

Answers for O’Reilly PR

The O’Reilly public relations people asked me to answer some questions about the new Intermediate Perl so they can prepare materials for reviewers and the press. As a reader of this website, however, you get the answers before they do, and you get my full answers, which might show up as edited excerpts in O’Reilly’s materials. » Read more…

Get 50% off Perl ebooks, including Intermediate Perl

You can buy Intermediate Perl now, directly from O’Reilly in ebook form. For the next week, you can buy it for 50% off using discount code WKPER5.

50% off

Every ebook from O’Reilly is DRM free and come in PDF, ePub, and Mobi formats. If you’ve authorized O’Reilly’s Dropbox app, once you’ll purchase the books they’ll sync to your Dropbox account (in ~/Dropbox/Apps/O’Reilly Media). You can read them on any device anywhere you are.

The print version is still making its way out of the printers and to distributors, but we expect it to show up in the next three weeks.

Intermediate Perl is available for pre-order

You can now buy Intermediate Perl. I’ve just submitted the final changes and the final publishing bits should finish this week, sending the result to the printers very soon. The book should ship before the end of August.

If I haven’t listed your favorite bookseller, send a link.

Syntax coloring in Intermediate Perl PDF

O’Reilly wants to try an experiment with the Intermediate Perl PDF. We’re not limited by the physical process of putting ink on paper (and it’s a bit expensive to have more than one color of ink). I’m just going to show you the images and let you tell me what you think.

Page 25

Page 60

Page 104

Page 190

Page 210