double/float precision question

Mensanator mensanator at aol.com
Wed Apr 1 13:52:10 EDT 2009


On Apr 1, 12:13 pm, TP <Tribulati... at Paralleles.invalid> wrote:
> Hi everybody,
>
> Try the following python statements:
>
> >>> "%.40f" % 0.2222222222222222222222222222222
>
> '0.2222222222222222098864108374982606619596'>>> float( 0.2222222222222222222222222222222)
>
> 0.22222222222222221
>
> It seems the first result is the same than the following C program:
> ################
> #include <stdio.h>
>
> int main(void)
> {
>     double a = 0.2222222222222222222222222222222;
>
>     printf( "%.40f\n", a );
>     return 0;}
>
> #################
>
> My problem is the following:
> * the precision "40" (for example) is given by the user, not by the
> programmer.
> * I want to use the string conversion facility with specifier "e", that
> yields number is scientific format; so I cannot apply float() on the result
> of "%.40e" % 0.2222222222222222222222222222222, I would lost the scientific
> format.
>
> Is there any means to obtain the full C double in Python, or should I limit
> the precision given by the user (and used in "%.*e") to the one of a Python
> float?

You can get arbitrary precision floats from the gmpy module.

>
> Thanks in advance
>
> Julien
>
> --
> python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
> 9&1+,\'Z4(55l4('])"
>
> "When a distinguished but elderly scientist states that something is
> possible, he is almost certainly right. When he states that something is
> impossible, he is very probably wrong." (first law of AC Clarke)




More information about the Python-list mailing list