Skip to main content
GameDev.net gamedev.net
🔒 Locked

float vs double in physics simulation

Started by orbano Jan 21, 2004 at 2:47 PM 6 replies 3.1k views
Original Post
orbano
orbano
My question is simple: Which of the mentioned floating point types should be used for physical calculations (for storing/calculating body sates) thank you "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
vesoljc
vesoljc
should is a funny word

flaots are precise up to 6,7 digits, while doubles can get 15 digits...

try it out...
if going for super realistic, go for doubles
Abnormal behaviour of abnormal brain makes me normal...www.zootfly.com
Sneftel
Sneftel
In the vast majority of cases, floats will have enough precision for video game stuff. There won''t be an appreciable difference in "realism".

If you''re concerned, just use typedefs, and switch ''em back and forth. I have something like this in my engine:

typedef float Scalar;

Scalar is used by all my math code. If I were to change it to typedef double Scalar, everything would switch over to using doubles.


"Sneftel is correct, if rather vulgar." --Flarelocke
MrRowl
MrRowl
quote:
Original post by Sneftel
In the vast majority of cases, floats will have enough precision for video game stuff. There won''t be an appreciable difference in "realism".



I agree - in fact even if you think you''ll ultimately use doubles then testing your code with floats will make potential accuracy problems show up much easier. Last thing you want is a bug that only shows up once every few hours (i.e. frequently enough to be a problem, but infrequently enough to be a pain to reproduce).

I also use typedef so that I can switch between double/float easily.

I find that using floats my (physics) code runs faster, though I expect this is mainly due to better cache usage since structures etc take up less space.

Incidently, I just got a book "Collision Detection in Interactive 3D environments" (I think - left it at work ;-) which apparantly has a scalar class that keeps track of numerical errors. Kind of a neat idea for debugging, maybe.
orbano
orbano
i think i choose floats. if the majority is satisfied with it, why should i use something else? creating a geenral floating point type is a good idea, until you plan to use SSE. Maybe i can handle this problem somehow, but its far more complicated. the main problem is that if i use float, i can use the __m128 packed floating point type in union with a float[4] array. any ideas how to solve this problem? (i dont want to write templates )
oh and thanx for the answers

"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
SpaceDude
SpaceDude
You don''t need to limit yourself to only using floats or only using doubles... you can mix and match... use floats where precision isn''t critical, and use doubles when it is...

just as long as you don''t do stupid things like doing all the calculations using floats then convert to double for storage
grhodes_at_work
grhodes_at_work
I concur with most here. Float should be sufficient. There are cases where doubles buy you precision. The main thing to be concerned with is implementing a numerically stable system (search forum archives for this topic). If the numerical technique you chose is conditionally stable or unstable, doubles won''t make it better. If your system is stable, then floats vs. doubles is just going to be about precision and for games that just isn''t usually critical. (Yeah, collision detection likes precision, but a robust technique should be able to deal well enough with just floats.)

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
grhodes_at_work
grhodes_at_work
I will add that if you have a large range of length scales, e.g., objects ranging from nanometers to ten thousand-ish meters (roughly the height of Mount Everest) then you will want doubles. And in that case doubles might not even be enough. This has to do with lost precision during, especially, subtraction. But, there are clever, hierarchical ways to represent multiple length scales without always needing higher precision floating point.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

Topic Locked

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

Sign in to reply to this topic.