# Doubles/Floats

• 01-06-2010, 08:26 AM
toneburst
Doubles/Floats
Quick query.

in the following code;

Code :

```float4 VertexInterp(isolevel, p1, p2, valp1, valp2) double isolevel, valp1, valp2; float4 p1, p2; { float mu; float4 p = 0;   if (fabs(isolevel-valp1) < 0.00001) return(p1); if (fabs(isolevel-valp2) < 0.00001) return(p2); if (fabs(valp1-valp2) < 0.00001) return(p1); mu = isolevel - valp1) / (valp2 - valp1; p = mix(p1,p2,mu); return(p); } }```

is it good to explicitly convert the result of the calculation
Code :

`isolevel - valp1) / (valp2 - valp1`
(where valp1 and valp2 are doubles) into a float, or can it be left as it is?

I presume I'd cast the result as a float like so
Code :

`mu = (float)(isolevel - valp1) / (valp2 - valp1);`
but am wondering if it's necessary or desirable to do so.

a|x
http://machinesdontcare.wordpress.com
• 01-06-2010, 08:37 AM
toneburst
Re: Doubles/Floats
Sorry, just realised I made some typos in the code above.

Code :

`mu = isolevel - valp1) / (valp2 - valp1;`

should of course have been

Code :

`mu = (isolevel - valp1) / (valp2 - valp1);`

and

Code :

`mu = (float)(isolevel - valp1) / (valp2 - valp1);`

should have been

Code :

`mu = (float)((isolevel - valp1) / (valp2 - valp1));`

(I think).

a|x
• 01-11-2010, 06:26 AM
dbs2
Re: Doubles/Floats
My guess is yes.
The compiler is extremely unlikely to do any conversion before the write to mu unless you set some flag because it can lose precision. So you will get actual double math with the code the way it is.

A quick search suggests that the intel latency difference between a single precision and a double precision divide is between 15 and 7 cycles depending on the radix of the divider.