-1

My question is simple.

1/3 = 0.33333..... (recurring) 1/3 + 1/3 + 1/3 = 1 (in pure mathematics)

Actually

1/3 + 1/3 + 1/3 = 0.99999.... (recurring)

My questions are

1]. How programmers eliminate this error ?

2]. Is this error elimination named something ?

3]. 1/3 + 1/3 + 1/3 = 0.9999.... is correct. 1/3 + 1/3 + 1/+3 = 1 is logic less. How a logic less operation is carried away in programming ?

This question is asked in mathematics community in math.stackexchange , if you are a mathematician and programmer please try to answer that question also.

https://math.stackexchange.com/questions/4391241/why-fractions-like-1-3-1-9-is-recurring-and-how-to-stop-this-recurring

  • Each programming language and each architecture represent floats differently. You can not generalize this to programming in general. The short version is computers can not represent infinite data like this so you need to make a decision on how much precision you want to support. At come point they will round it – sinanspd Feb 25 '22 at 22:43
  • 1
    See also [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken/28679423) – GrapefruitIsAwesome Feb 25 '22 at 23:36
  • Despite the comments and answers you **can** eliminate those errors completely by using rational arithmetic. Many languages have support for rational arithmetic, either built-in or through 3rd party packages, e.g. [python `fractions`](https://docs.python.org/3/library/fractions.html). – President James K. Polk Feb 26 '22 at 11:26

1 Answers1

2

How programmers eliminate this error ?

You can't eliminate floating-point imprecision completely, but you can mitigate it in a number of ways:

  • Round to a reasonable accuracy (# of decimal places)
  • Don't compare floating-point values directly; always compare the absolute difference to a very small tolerance
  • Make sure you are limiting the number of decimals you display to a reasonable amount
  • Use floating-point values only when this imprecision is irrelevant (like when measuring non-discrete things like temperature or distance). Use decimal or equivalent types when decimal representation is critical (like when calculating monetary amounts)
D Stanley
  • 149,601
  • 11
  • 178
  • 240