[ProgSoc] 10000001

arp arp at progsoc.uts.edu.au
Sun Oct 4 11:03:47 EST 2009


On Sun, Oct 04, 2009 at 01:35:14AM +1000, John Elliot wrote:
> Is it hard to believe I've spent my career as a programmer in ignorance 
> of the fact that the smallest positive integer that can't be represented 
> by a 32-bit floating point number is 10,000,001?
> 
> Figured I'd share.

Ok. Hopefully I got this right. I already made more mistakes
writing this than I care to admit (sunday morning brain resume
failure). Perl's pretty typeless so I did some stuff to hopefully
force the issue (and some wibblies).

#!/usr/bin/perl

use warnings;
use strict;

my $i = 0;
my $s = 0.0;

while (1) {
        $i++;
        $s += 1.0;

        my $si = sprintf("%d", $i);
        my $ss = sprintf("%f", $s);

        if ( "$si.000000" ne "$ss" or $i != $s ) {
                print "si = $si, ss = $ss\n";
                exit;
        }
        
        print "Checpoint: $i\n" unless ($i % 1_000_000);
}

$ perl moo.pl 
Checpoint: 1000000
Checpoint: 2000000
Checpoint: 3000000
Checpoint: 4000000
Checpoint: 5000000
Checpoint: 6000000
Checpoint: 7000000
Checpoint: 8000000
Checpoint: 9000000
Checpoint: 10000000
Checpoint: 11000000
Checpoint: 12000000
Checpoint: 13000000
Checpoint: 14000000
Checpoint: 15000000
...

arp



More information about the Progsoc mailing list