Floating Point representation

Searching for a neat data type that can represent either a double or an error (similar to a boost::optional but where the name suggests the possibility of error and the type could hold an error string. I came across this StackOverflow page with an excellent answer by Jon Skeet:

Decimal numbers can be represented exactly, if you have enough space – just not by floating binary point numbers. If you use a floating decimal point type (e.g. System.Decimal in .NET) then plenty of values which can’t be represented exactly in binary floating point can be exactly represented.

Let’s look at it another way – in base 10 which you’re likely to be comfortable with, you can’t express 1/3 exactly. It’s 0.3333333… (recurring). The reason you can’t represent 0.1 as a binary floating point number is for exactly the same reason. You can represent 3, and 9, and 27 exactly – but not 1/3, 1/9 or 1/27.

The problem is that 3 is a prime number which isn’t a factor of 10. That’s not an issue when you want to multiply a number by 3: you can always multiply by an integer without running into problems. But when you divide by a number which is prime and isn’t a factor of your base, you can run into trouble (and will do so if you try to divide 1 by that number).

Although 0.1 is usually used as the simplest example of an exact decimal number which can’t be represented exactly in binary floating point, arguably 0.2 is a simpler example as it’s 1/5 – and 5 is the prime that causes problems between decimal and binary.

Leave a comment

Filed under Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.