Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do-s and Don't-s for floating point arithmetic?

What are some good do-s and don't-s for floating point arithmetic (IEEE754 in case there's confusion) to ensure good numerical stability and high accuracy in your results?

I know a few like don't subtract quantities of similar magnitude, but I'm curious what other good rules are out there.

like image 369
gct Avatar asked Jun 23 '10 14:06

gct


People also ask

What are the rules for arithmetic operations on floating-point numbers?

Arithmetic operations on floating point numbers consist of addition, subtraction, multiplication and division. The operations are done with algorithms similar to those used on sign magnitude integers (because of the similarity of representation) — example, only add numbers of the same sign.

Why is floating point arithmetic not exact?

Floating-point decimal values generally do not have an exact binary representation. This is a side effect of how the CPU represents floating point data. For this reason, you may experience some loss of precision, and some floating-point operations may produce unexpected results.

Why is 0.1 not represented as a float?

The number 0.1 in floating-point The finite representation of 1/10 is 0.0 0011 ‾ 0.0\overline{0011} 0.00011, but it can't be represented in floating-point because we can't deal with bars in floating-point. We can represent it only in fixed digits/bits using any data type.

What is floating-point representation and arithmetic?

In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base.


1 Answers

First, enter with the notion that floating point numbers do NOT necessarily follow the same rules as real numbers... once you have accepted this, you will understand most of the pitfalls.

Here's some rules/tips that I've always followed:

  • NEVER compare a floating point number to zero or anything else for that matter (IE don't do: if (myFloat == 0)
  • Associative property does not hold for floating point... meaning (a + b) + c != a + (b + c)
  • Remember that there is always rounding
  • Floating point numbers do not necessarily have a unique inverse
  • No closure with floating point numbers... never assume that the result of a floating point operation results in a valid floating point number.
  • Distributive property does not hold
  • Try to avoid using floating point comparisons at all... as round off error can cause unexpected results
like image 169
Polaris878 Avatar answered Oct 13 '22 21:10

Polaris878