Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it appropriate to use floating precision data types?

Tags:

math

It's clear that one shouldn't use floating precision when working with, say, monetary amounts since the variation in precision leads to inaccuracies when doing calculations with that amount.

That said, what are use cases when that is acceptable? And, what are the general principles one should have in mind when deciding?

like image 441
Edward Q. Bridges Avatar asked Apr 06 '09 21:04

Edward Q. Bridges


People also ask

When would you use a floating-point data type?

Floating-point data types are mainly used in mathematics and science to simplify the calculations with scientific notation. Storing numbers with major differences in magnitude is their advantage in databases too, because zeros trailing or following the decimal sign does not consume memory as it does for decimal format.

What is the precision of float data type?

The data type float has 24 bits of precision. This is equivalent to only about 7 decimal places. (The rest of the 32 bits are used for the sign and size of the number.) The number of places of precision for float is the same no matter what the size of the number.

Why do we use floating-point numbers?

It provides the same relative accuracy at all magnitudes (limited by the length of the significand) It allows calculations across magnitudes: multiplying a very large and a very small number preserves the accuracy of both in the result.


1 Answers

Floating point numbers should be used for what they were designed for: computations where what you want is a fixed precision, and you only care that your answer is accurate to within a certain tolerance. If you need an exact answer in all cases, you're best using something else.

Here are three domains where you might use floating point:

  1. Scientific Simulations
    Science apps require a lot of number crunching, and often use sophisticated numerical methods to solve systems of differential equations. You're typically talking double-precision floating point here.

  2. Games
    Think of games as a simulation where it's ok to cheat. If the physics is "good enough" to seem real then it's ok for games, and you can make up in user experience what you're missing in terms of accuracy. Games usually use single-precision floating point.

  3. Stats
    Like science apps, statistical methods need a lot of floating point. A lot of the numerical methods are the same; the application domain is just different. You find a lot of statistics and monte carlo simulations in financial applications and in any field where you're analyzing a lot of survey data.

Floating point isn't trivial, and for most business applications you really don't need to know all these subtleties. You're fine just knowing that you can't represent some decimal numbers exactly in floating point, and that you should be sure to use some decimal type for prices and things like that.

If you really want to get into the details and understand all the tradeoffs and pitfalls, check out the classic What Every Programmer Should Know About Floating Point, or pick up a book on Numerical Analysis or Applied Numerical Linear Algebra if you're really adventurous.

like image 142
Todd Gamblin Avatar answered Oct 12 '22 02:10

Todd Gamblin