Skip to main content
gamedev.net
🔒 Locked

double/float and speed

Started by Tree Penguin Jan 22, 2004 at 6:31 AM 4 replies 550+ views
Original Post
Tree Penguin
Tree Penguin
Has anyone got articles on the speed difference between float and double and what you can use which one best for. Tnx!
orbano
orbano
if you need high precision (in a game probably you don''t) use doubles. otherwise use floats. calculations with doubles take more CPU (FPU) cycles. its slower.


"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
Enigma
Enigma
functions:
void floatFunc()
{
float x = 1;
for (int i = 0; i < 200000000; i++)
{
x *= ((x + 1.0f) / 2.0f);
}
std::cout << x << ''\n'';
}

void doubleFunc()
{
double x = 1;
for (int i = 0; i < 200000000; i++)
{
x *= ((x + 1.0) / 2.0);
}
std::cout << x << ''\n'';
}


Output from gprof (profiler):
  %   cumulative   self              self     total           
time seconds seconds calls s/call s/call name
49.83 3.01 3.01 1 3.01 3.01 floatFunc()
49.83 6.02 3.01 1 3.01 3.01 doubleFunc()


So there is no difference in execution time for floats vs doubles. Doubles may be slower to fetch/transfer though, due to their size.

Enigma
Tree Penguin
Tree Penguin
Enigma, thanks, that was very helpfull !
variant
variant
If you are looking at doing precise physics or calculations, I would use doubles. I''m not sure if this is correct but I think that floats can have a variation in their decimal values at like the 8th decimal number (.#######1). I made a gravity simulation and used floats to store position and acceleration etc., and a system the should have been stable would eventually become unstable becasue of the tiny variations in the float. I used doubles and the problem went away.
Tree Penguin
Tree Penguin
That''s correct, i first used code to draw a quad using many tiny triangles, but the last row of vertices wasn''t drawn. I finally found out (when i decided to place all data in a text file to) that the value, when it should have been something like 16.0 it was 16.000001, so the >=16.0f test failed. I use floats now for all calculations except drawing planes in pieces (is that tesselating or is tesselating something else, sorry if it comes to names of tecniques i am a n00b ).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.