I'm working on a report as I type this. One of the fields is a long
milliseconds of duration that I got from somewhere else. This is going to be sent to Microsoft Excel and the duration units it uses is decimal days (1.25
= 1 day, 6 hours).
Sure, you can subdivide a range from the lowest possible value to the largest and have integer units stepping between them, but thats a really awkward unit to work with in most cases.
There are three times that come into play when working with computers:
- Time it takes to code
- Time it takes to run
- Time it takes to maintain
When working with integers rather than floating point, you're trading times 1 and 3 for time 2 to run faster. But here's the thing... I don't need it to run fast.
If there's a rounding error in the 10th decimal place in calculating a decimal time, thats fractions of a second that I don't care about (the report is HH:MM:SS
- not HH:MM:SS.000
). They don't get rounded into what I'm dealing with or presenting. Floating point is good enough.
Calculating the area of a circle for a graph or where pi finds its way into probabilities with only integers is not fun. Pi is not 3.
Its also faster for me to work with floating point in many places. 1.5
is 1.5
not (15 with a scale of 1). So I can write my code faster and the person maintaining it can read it faster and work with it.
If you need to use fixed point precision (money) or pure integer math for speed certain applications (simulating galaxies colliding and nuclear bombs), by all means, use them. But for most things, floating point is just fine if you really aren't dealing with those specialized situations.
That's just working with one end of the scale. The numbers that can be subdivided between 0 and 264 (or however many bits you are using). 264 is about 1019. But what if you need to work with 1020 or 10200? There are problems that work in this domain that aren't restricted to super computers. There are times when regular simulations and games use floats - often because of restrictions on the library, but they use them there too. Sometimes its just easier.
Related on gamedev.SE: About floating point precision and why do we still use it