4

Should 331.449999 be 331.4 or 331.5?

I can see a issue with a programming framework I am using. I think I am getting erroneous results in some cases and wanted to make sure I am using the right math results, before I raise a bug for it.

In the framework 331.45 -> 331.5

Akamad007
  • 149
  • 3
  • It's $331.4$. Don't double round (i.e., round the second $4$ and then use that value to round again). But you should be aware of what rounding mode is being used in the project (the typical rounding is nearest, but there are other options). – Michael Burr Feb 12 '18 at 21:51
  • 1
    Is the $9$ meant to be repeated so that your number is a representation of $331.45$? In any case, what rule do you favor for rounding $331.45$? – lulu Feb 12 '18 at 21:51
  • 331.45 -> 331.5 – Akamad007 Feb 12 '18 at 21:53
  • 1
    To be clear: many systems use a system in which both $331.35$ and $331.45$ would round to $331.4$. In some ways this is less biased then a rule which always rounds up if the first omitted digit is a $5$. – lulu Feb 12 '18 at 21:53
  • Ask your system to do the rounding. Personally, I would say it should be $331.4$ but people disagree on the point. – lulu Feb 12 '18 at 21:54
  • 1
    It is highly unlikely this is a bug, but rather you don't know what rounding rules they are using (there are a lot of different rules). – law-of-fives Feb 12 '18 at 21:55
  • Just a note $0.050001=|331.5 - 331.449999| > |331.4 - 331.449999|=0.049999$, so $331.4$ is closest ... – rtybase Feb 12 '18 at 22:41

2 Answers2

4

The number $331.449999$ cannot be represented as a $32$-bit floating point, but is stored as $331.45001220703125$, which gets rounded to $331.5$.

3

There is no such thing as a “correct” way to round. There are many different ways to round, and to check if the rounding is being done correctly you’d have to specify what kind of rounding you’re asking about.

Rounding to the nearest tenth (a common default way to round) would round that number down. However, there are many mathematical and non-mathematical reasons why the computer might not be doing that.

Notably, your number can’t be represented in 32 bits and the closest 32 bit number is big enough to get rounded the other way. So even if it was trying to round to the nearest tenth, it might not achieve that.