[Python-Dev] stupid floating point question...

M.-A. Lemburg mal@lemburg.com
Thu, 28 Sep 2000 00:08:42 +0200


Fredrik Lundh wrote:
> 
> > Try again?  I have no idea what you're asking.  Obviously, str(i) won't
> > look anything like str(1./6) for any integer i, so *that's* not what you're
> > asking.
> 
> consider this code:
> 
>         PyObject* myfunc1(void) {
>             return PyFloat_FromDouble((double) A / B);
>         }
> 
> (where A and B are constants (#defines, or spelled out))
> 
> and this code:
> 
>         PyObject* myfunc2(int a, int b) {
>             return PyFloat_FromDouble((double) a / b);
>         }
> 
> if I call the latter with a=A and b=B, and pass the resulting
> Python float through "str", will I get the same result on all
> ANSI-compatible platforms?
> 
> (in the first case, the compiler will most likely do the casting
> and the division for me, while in the latter case, it's done at
> runtime)

Casts have a higher precedence than e.g. /, so (double)a/b will
be compiled as ((double)a)/b.

If you'd rather play safe, just add the extra parenthesis.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/