double/float precision question

T

TP

Hi everybody,

Try the following python statements:
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?

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)
 
M

Mensanator

Hi everybody,

Try the following python statements:


'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)
 
D

Dave Angel

It's not at all clear what you really want. You say you want to "use"
the %e format, but then imply you're then going to turn it back into a
float. Since I don't know what the end goal is, I'll just comment
generally.

All Python floating point is equivalent to the 'double' type of the C
implementation. That may vary by C compiler, or by processor, but what
you see is typical of an 8 byte double type. I don't really remember my
values, but it's around 16 digits of precision. Anything displayed
beyond those digits is noise, and shouldn't be considered reproducible.
In other words, those two results are the same, the rounding is just
different.

To put it simply, in Python, float() on a floating point number does
not reduce the precision in the least.

As for having the user specify the number of digits he/she wants, that
just means you have to construct the format string in a variable.
Something like:
digits = 14 (this you'd have gotten from a user)
format = "%." + str(digits) + "e"
result = format % number #this does the formatting
 
S

Steven D'Aprano

Hi everybody,

Try the following python statements:

0.22222222222222221

Remove the leading quote from the first one, and you'll see that the two
numbers look pretty similar:

0.2222222222222222098864108374982606619596
0.22222222222222221

By the way, calling float(0.2222...2) is redundant, because 0.222...2 is
already a float. Calling float again just wastes CPU cycles, because the
same object is returned again.
True


We can see that floats have more precision than they display by default:
2.2204460492503131e-16

Notice that doing this reveals more significant digits than were apparent
from just printing x.

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.

No, this is confused. The float you create is the exact same object
whether you use scientific format or not.
0.0123

*All* floats contain mantissa and an exponent, but in binary, not decimal:
0.0123


Is there any means to obtain the full C double in Python

Floats in Python *are* C doubles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top