Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't 0.9 recurring always equal 1

Tags:

c#

.net

math

Mathematically, 0.9 recurring can be shown to be equal to 1. This question however, is not about infinity, convergence, or the maths behind this.

The above assumption can be represented using doubles in C# with the following.

var oneOverNine = 1d / 9d;
var resultTimesNine = oneOverNine * 9d;

Using the code above, (resultTimesNine == 1d) evaluates to true.

When using decimals instead, the evaluation yields false, yet, my question is not about the disparate precision of double and decimal.

Since no type has infinite precision, how and why does double maintain such an equality where decimal does not? What is happening literally 'between the lines' of code above, with regards to the manner in which the oneOverNine variable is stored in memory?

like image 216
Tom Bowers Avatar asked Oct 17 '13 20:10

Tom Bowers


People also ask

Is .9 repeating the same as 1?

= 1 — the sequence of terminating decimals 0.9, 0.99, 0.999, 0.9999, and so on, converges to 1, so the repeating decimal 0.9999... representing the limit of that sequence, is said to be equal to 1. The same idea works for any rational number with a repeating infinite decimal expansion. 0.333...

Is there a number between .9 repeating and 1?

No, there is not a number between 1 and 0.9999 repeating. This is because they are equal.

How do you get 0.9 recurring?

We first let 0.9 (9 being repeated) be x . Since x is recurring in 1 decimal places, we multiply it by 10. Next, we subtract them. Lastly, we divide both sides by 9 to get x as a fraction.

Is 0.9 Repeating a rational number?

ANSWER : 0.9999 is non terminating recurring,so it is a RATIONAL NUMBER.


1 Answers

It depends on the rounding used to get the closest representable value to 1/9. It could go either way. You can investigate the issue of representability at Rob Kennedy's useful page: http://pages.cs.wisc.edu/~rkennedy/exact-float

But don't think that somehow double is able to achieve exactness. It isn't. If you try with 2/9, 3/9 etc. you will find cases where the rounding goes the other way. The bottom line is that 1/9 is not exactly representable in binary floating point. And so rounding happens and your calculations are subject to rounding errors.

like image 58
David Heffernan Avatar answered Sep 22 '22 14:09

David Heffernan