Sign in to follow this  
Sync Views

double or float

Recommended Posts

When should I use floats rather than doubles? Currently I'm useing doubles for all my decimal stuff as the ropunding errors seem to be smaller but should I use floats for collision testing etc because there faster to work with?

Share this post


Link to post
Share on other sites
Quote:
Original post by Sync Views
When should I use floats rather than doubles? Currently I'm useing doubles for all my decimal stuff as the ropunding errors seem to be smaller but should I use floats for collision testing etc because there faster to work with?


I can think of 3 good reasons off the top of my head to use floats over doubles:
  • Your hardware doesn't support doubles.
    This is usually only a problem on embedded systems, and most of them don't have floats either.

  • You are interfacing with an API that uses floats
    OpenGL is a great example of this, GPUs don't have double support in general.

  • You are strapped for memory or bandwidth limited, and you don't need the precision anyway.
    Your buffers will be half the size, and thus much faster to copy and much friendlier to the cache if they are floats rather than doubles.

In all other cases, you might as well use doubles. As an aside, double and float maths should be exactly the same speed on modern CPUs.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sync Views
When should I use floats rather than doubles?


When you have a lot of them, and you really care about saving space.

Quote:
Currently I'm useing doubles for all my decimal stuff as the ropunding errors seem to be smaller but should I use floats for collision testing etc because there faster to work with?


They aren't necessarily any faster to work with (for the computer; obviously it makes no real difference as a programmer anyway).

Share this post


Link to post
Share on other sites
k. I thought computers could use floats alot faster than doubles. Kind of like if I'm doing say "49.95*1.2005" to 2 decimal places I'll stop at "59.964" and just round it from there rather than working the entire thing out (59.964975). I just didn't know how much faster. If theres no noticable diffrence I won't worry about moving my collision stuff (serval 1000 sums a second) from doubles to floats.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sync Views
k. I thought computers could use floats alot faster than doubles. Kind of like if I'm doing say "49.95*1.2005" to 2 decimal places I'll stop at "59.964" and just round it from there rather than working the entire thing out (59.964975). I just didn't know how much faster. If theres no noticable diffrence I won't worry about moving my collision stuff (serval 1000 sums a second) from doubles to floats.


As an example, floats are 32-bit, doubles are 64-bit, and all recent Intel CPUs do all floating point calculations with 80-bits. So all you accomplish (instruction wise) by using floats is requiring the CPU to do additional rounding.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sync Views
Currently I'm useing doubles for all my decimal stuff as the ropunding errors seem to be smaller

What decimal stuff? Monetary values or something similar? float and double shouldn't be used for that at all.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this