Question

When to use a Float

Years ago I learned the hard way about precision problems with floats so I quit using them. However, I still run into code using floats and it make me cringe because I know some of the calculations will be inaccurate.

So, when is it appropriate to use a float?

EDIT: As info, I don't think that I've come across a program where the accuracy of a number isn't important. But I would be interested in hearing examples.

 45  30769  45
1 Jan 1970

Solution

 60

Short answer: You only have to use a float when you know exactly what you're doing and why.

Long answer: floats (as opposed to doubles) aren't really used anymore outside 3D APIs as far as I know. Floats and doubles have the same performance characteristics on modern CPUs, doubles are somewhat bigger and that's all. If in doubt, just use double.

Oh yes, and use decimal for financial calculations, of course.

2009-01-02

Solution

 13

All floating point calculations are inaccurature in a general case, floats just more so than doubles. If you want more information have a read of What Every Computer Scientist Should Know About Floating-Point Arithmetic

As for when to use floats - they are often used when precision is less important than saving memory. For example simple particle simulations in video games.

2009-01-02