[issue4114] struct returns incorrect 4 byte float

STINNER Victor report at bugs.python.org
Mon Oct 13 23:21:02 CEST 2008


STINNER Victor <victor.stinner at haypocalc.com> added the comment:

The problem is not from Python but from your FPU during the conversion 
from 64 bits float to 32 float (made by struct.pack). Example in C:

#include <stdio.h>
int main()
{
    float f;
    double d;
    d = 1.8183;
    printf("d=%.20f\n", d);
    f = (float)d;
    d = (double)f;
    printf("f=%.20f\n", f);
    printf("d=%.20f\n", d);
    return 0;
}

Result:
d=1.81830000000000002736   # ok
f=1.81830000877380371094   # 64->32: loose precision
d=1.81830000877380371094   # 32->64: no change

----------
resolution:  -> invalid
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4114>
_______________________________________


More information about the Python-bugs-list mailing list