0
\$\begingroup\$

In this challenge: rounding numbers.

Most answers use floating point numbers, which means that many potential input values are impossible due to the limited precision.

For example, the inputs .02675 and 4. In this challenge, that would mean rounding .02675 to 4 decimal places, which should result in .0268.
However, 0.02675 can't be represented exactly in the common floating point formats, and it's actually stored as .02674999..., which rounds to .0267.

Are these answers are invalid because they can't accept all the potential input values?

\$\endgroup\$
1
  • \$\begingroup\$ Possible duplicate, though a strong consensus wasn't reached there. \$\endgroup\$ Jun 7, 2018 at 17:56

1 Answer 1

2
\$\begingroup\$

Allow the default behavior

Floating point inaccuracies is a limitation we have to work with/around. That's just how computers work. If a language/program circumvents this and treats the numbers as accurate decimal numbers, then that's fine. If not, then that's fine too.


If a language/program can handle the numbers as accurate decimal numbers then allow this. That means that it's OK to return 0.15 as 0.2 if only one decimal number is required.

If a language/program can't handle the numbers as accurate decimal numbers, then allow for the default behavior. That means that it's OK to return 0.15 as 0.1 if only one decimal is required, since it's floating point value is 0.14999...

In the specific challenge you linked to, both 0.0268 and 0.0267 would be OK. However, rounding 0.25 to 0.2 would not be OK, since 0.25 can be accurately represented using floats.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .