floating point in 2.0
Edward Jason Riedy
ejr at cs.berkeley.edu
Thu Jun 7 19:57:01 EDT 2001
And Tim Peters writes:
-
- Note that this is platform-dependent, and even across platforms
- that conform fully to IEEE-754: they're allowed to display only
- zeroes after the 17th significant decimal digit.
I've forgotten the exact language, but we're going to strongly
recommend using Steele and White's algorithm by default:
http://www.acm.org/pubs/citations/proceedings/pldi/93542/p112-steele/
Given 0.1 converted to binary, this will print 0.1. It's a
complicated algorithm, but worth it. And it's not a lie.
It returns the shortest decimal string which will reproduce
the given binary number after a decimal->binary conversion.
Of course, you may worry about 0.1 + 0.1 + ... != 1.0. We're
also going to be giving a good deal of advice (and some
requirements) on handling arithmetic. I'm working on a paper
with greater detail, but try the following C program for a
taste of some intended advice:
========
#include <stdio.h>
/*
Accumulating in more precision than the data `deserves'
AND rounding the output gives the right answer for more
cases.
*/
int
main (void)
{
int i;
float x = 0.1;
double accum = 0.0;
float accumf = 0.0;
float y;
for (i = 0; i < 100; ++i) {
accum += x; /* accumulate in double... */
accumf += x;
}
y = (float) accum; /* round back to float... */
printf ("%.36g\n%.36g\n%.36g\n", accumf, accum, y);
}
========
Next meeting for 754r is on the 20th somewhere around San Jose.
There's a mostly inactive mailing list about it; people honestly
interested in participating can mail me for more info.
Jason
--
More information about the Python-list
mailing list