0

When I execute the JavaScript code 8/(3-8/3), the result is displayed as 23.99999999999999, but I want to fix it to 24.

In order not to round numbers when they are not repeating decimals or when their repetend is not 9 (e.g. do not round 21.835 to 22; do not round 2.979797979797 to 3), how should I solve this problem?

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Banana Code
  • 759
  • 1
  • 12
  • 28
  • 1
    The result isn't "displayed as" 23.99999999999999, it _is_ 23.99999999999999, because you're using decimal fractions with IEEE floating point numbers. Which are binary, not decimal. There are numbers that IEEE floats _cannot_ represent, so you're seeing the nearest value that an IEEE floating point number _can_ represent. The best fix is to rearrange your math. – Mike 'Pomax' Kamermans Jan 27 '23 at 02:07

1 Answers1

0

There doesn't seem to be a clear way to resolve this issue without using some sort of rounding function, but there are alternative ways to write this specific equation, according to MathsIsFun

E.g.

8/(1/3)

Brendan
  • 4,565
  • 1
  • 24
  • 39