double - How do I convert a Python float to a hexadecimal string in python 2.5? Nonworking solution attached -
What I really need to do is export a floating point number C with no exact loss.
I did this in Python:
import math import structure x = math.sqrt (2) print struct.unpack ('ii', struct.pack ('d ', X)) # print (1719614413, 1073127582) and try this in CI: #include # Include & lt; Stdio.h & gt; Int main (zero) {unsigned long long x [2] = {1719614413, 1073127582}; LX long; Double XF; LX = (X [0] <<32 <32 X [1]; Xf = (double) lx; Printf ("% lf \ n", xf); Return 0; }
but found in CI:
7385687666638364672.000000 and not sqrt (2).
What am I missing?
Thank you.
Python code appears to be working. The problem is in C code: You have the long filled right
, but then you change the integer values directly to the floating point, instead of re-defining the byte < Code> double . If you throw some points or address it, then it works:
jkugelman $ cat float.c #include & lt; Stdio.h & gt; Int main (zero) {unsigned long x [2] = {1719614413, 1073127582}; Double D = * (double *) x; Printf ("% f \ n", d); Return 0; } Jkugelman $ gcc -o float float.c jkugelman $ ./float 1.414214
Also note that double
(and float
) Is % f
, not % lf
. % lf
is for long double
.
Comments
Post a Comment