Convert single precision to double precision
Author Message
Convert single precision to double precision

How can I simply convert a float(single precision) into a double(double
precision).
I know I can load a float in the FPU, but if the precision of the FPU is
double, the value isn't correct.
(0.300000(single precision) isn't 0.300000 in double precision, but
something like 0.300001292)

My processor isn't capable of SSE2 instructions.
(SSE2 has instructions for converting from single to double and back again)

Fri, 04 Nov 2005 00:57:50 GMT
Convert single precision to double precision
fld dword ptr [flt]
fstp qword ptr [dbl]

Converts from float to double.. However it doesn't solve the precision
problem.

So you could do like this:

fld dword ptr [flt]
fimul dword ptr [mult];
frndint
fidiv dword ptr [mult]
fstp qword ptr [dbl]

"mult" is a large int value (10,000,000 or something). This thing works nice
for your 0.3000 example, and is at least exact for most low values (under 10
or something, didn't really test but when I tested with 333.313 or whatever
it was off a bit..)
It's a hack but it could work if you really need to have exactly
0.3000000000 :)

/Nico

| How can I simply convert a float(single precision) into a double(double
| precision).
| I know I can load a float in the FPU, but if the precision of the FPU is
| double, the value isn't correct.
| (0.300000(single precision) isn't 0.300000 in double precision, but
| something like 0.300001292)
|
| My processor isn't capable of SSE2 instructions.
| (SSE2 has instructions for converting from single to double and back
again)
|
|
|
|
|

Fri, 04 Nov 2005 03:46:27 GMT
Convert single precision to double precision

Quote:

> How can I simply convert a float(single precision) into a double(double
> precision).
> I know I can load a float in the FPU, but if the precision of the FPU is
> double, the value isn't correct.
> (0.300000(single precision) isn't 0.300000 in double precision, but
> something like 0.300001292)
> My processor isn't capable of SSE2 instructions.
> (SSE2 has instructions for converting from single to double and back again)

The problem is that 0.3 can not be represented exactly in binary
floating point. See the chapter on floating-point math in my tutorial
for the details.

http://www.drpaulcarter.com/pcasm/

--
Paul Carter

Fri, 04 Nov 2005 06:01:29 GMT
Convert single precision to double precision
A very interesting document indeed!

Quote:

> > How can I simply convert a float(single precision) into a double(double
> > precision).
> > I know I can load a float in the FPU, but if the precision of the FPU is
> > double, the value isn't correct.
> > (0.300000(single precision) isn't 0.300000 in double precision, but
> > something like 0.300001292)

> > My processor isn't capable of SSE2 instructions.
> > (SSE2 has instructions for converting from single to double and back
again)

> The problem is that 0.3 can not be represented exactly in binary
> floating point. See the chapter on floating-point math in my tutorial
> for the details.

> http://www.drpaulcarter.com/pcasm/

> --
> Paul Carter

Fri, 04 Nov 2005 14:13:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages