Results 1 to 3 of 3

Thread: Doubles/Floats

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    18

    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

  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    18

    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

  3. #3
    Senior Member
    Join Date
    Jul 2009
    Location
    Northern Europe
    Posts
    311

    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.

Similar Threads

  1. Replies: 3
    Last Post: 06-26-2011, 07:52 PM
  2. Rounding floats
    By Siassei in forum OpenCL
    Replies: 1
    Last Post: 05-13-2011, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •