QamoosTech
Programming FundamentalsLanguage basicsIntermediate

Floating-Point Number

PronunciationFLOH-ting point NUM-ber

Definition

A floating-point number stores a decimal value as a binary approximation, so some fractions, such as 0.1, cannot be represented exactly.

Where you hear it

In bug reports about rounding, discussions of money calculations, and interviews asking why 0.1 + 0.2 is not 0.3.

Examples

  • Never compare two floats with equality; check that they are close enough instead.
  • We store prices as whole piasters to avoid floating-point errors.
  • In floating-point arithmetic, 0.1 + 0.2 gives 0.30000000000000004.

Common mistake

Using floats for money. Tiny errors add up; use integers of the smallest unit or a decimal type.

Don't confuse with

A decimal type, which stores base-10 digits exactly and is slower but safe for money.

Say it at work

  • In a meeting or chatThat total is off by a fraction because of float rounding; let's switch to integers.
  • In an email or ticketUse a tolerance when comparing floats in the test.