factorial of negative one (-1)

Hrvoje Niksic hniksic at xemacs.org
Mon Nov 1 10:42:06 CET 2010


Chris Rebert <clp2 at rebertia.com> writes:

> (2) The underlying double-precision floating-point number only has ~16
> decimal digits of precision, so it's pointless to print out "further"
> digits.

A digression which has nothing to do with Raj's desire for "better
accuracy"...

Printing out further digits (without quotes) is not pointless if you
want to find out the exact representation of your number in python's
floating point, for educational purposes or otherwise.  Python has a
little-known but very instructive method for determining the makeup of a
float:

>>> 1.1 .as_integer_ratio()
(2476979795053773, 2251799813685248)

1.1 is represented with the closest fraction with a power-of-two
denominator, 2476979795053773/2251799813685248.  As is the case with all
Python floats, this fraction has an exact decimal representation,
1.100000000000000088817841970012523233890533447265625.  It is not that
unreasonable to request that the whole number be printed, and python
will happily oblige:

>>> "%.100g" % 1.1
'1.100000000000000088817841970012523233890533447265625'

The digits after the first cluster of zeros are not garbage, at least
not in the sense of what you get reading uninitialized memory and such;
they're mathematically precise decimal digits of the number that "1.1"
has turned into during conversion to float.



More information about the Python-list mailing list