Floating-Point Types

You might remember the different types of numbers from grade school. Natural numbers correspond to unsigned integral values, but where are the real numbers (not to mention fractional numbers)? In the world of computer science, the answer is twofold, with a significant difference with regard to precision. As humans, we’ve embraced the decimal system, likely directly related to the number of fingers we’re born with. Computers work with binary representation, though. This poses a problem with real numbers, some of which can’t be represented exactly in binary. Let’s give it a try:

0.30  <  1/2        = 0.50.30  >  1/4        = 0.250.30  <  1/4 + 1/8  = 0.3750.30  <  1/4 + 1/16 = 0.31250.30  >  1/4 + 1/32 = 0.28125

We can continue ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.