finding out the precision of floats
Arnaud Delobelle
arnodel at googlemail.com
Sun Feb 25 09:11:02 EST 2007
On Feb 25, 1:31 pm, "John Machin" <sjmac... at lexicon.net> wrote:
> On Feb 25, 11:06 pm, "Arnaud Delobelle" <arno... at googlemail.com>
> wrote:
[...]
> Evidently not; here's some documentation we both need(ed) to read:
>
> http://docs.python.org/tut/node16.html
Thanks for this link
> I'm very curious to know what the exceptions were in November 2000 and
> if they still exist. There is also the question of how much it matters
> to you. Presuming the representation is 64 bits, even taking 3 bits
> off the mantissa and donating them to the exponent leaves you with
> 15.05 decimal digits -- perhaps you could assume that you've got at
> least 15 decimal digits.
It matters to me because I prefer to avoid making assumptions in my
code!
Moreover the reason I got interested in this is because I am creating
a
Dec class (decimal numbers) and I would like that:
* given a float x, float(Dec(x)) == x for as many values of x as
possible
* given a decimal d, Dec(float(d)) == d for as many values of d as
possible.
(and I don't want the standard Decimal class :)
> While we're waiting for the gurus to answer, here's a routine that's
> slightly less dodgy than yours:
>
> | >>> for n in range(200):
> | ... if (1.0 + 1.0/2**n) == 1.0:
> | ... print n, "bits"
> | ... break
> | ...
> | 53 bits
Yes I have a similar routine:
|def fptest():
| "Gradually fill the mantissa of a float with 1s until running out
of bits"
| ix = 0
| fx = 0.0
| for i in range(200):
| fx = 2*fx+1
| ix = 2*ix+1
| if ix != fx:
| return i
...
>>> fptest()
53
I guess it would be OK to use this. It's just that this 'poking into
floats'
seems a bit strange to me ;)
Thanks for your help
--
Arnaud
More information about the Python-list
mailing list