-19

I came across this question: How to deal with floating point number precision in JavaScript? which is flagged as a duplicate of Is floating point math broken?.

The problem is that the first question is asking specifically about JavaScript solutions, while the supposed duplicate is language-agnostic, and more about the general theory of floating point math (it appears that back in 2009 the question referred to JavaScript, but it no longer does). As such, I don't agree that they are duplicates. If I want to find a quality JavaScript solution to the issue, I'd like to read answers specifically about JavaScript.

I voted to reopen the question, but it now says:

The community reviewed whether to reopen this question 14 days ago and left it closed:
Original close reason(s) were not resolved

Can someone shed light on why this is the case?

If there's a way to improve the question, I'm open to doing so, however I think the question is quite reasonable as is.

I remain stuck with knowing where to find or contribute a thorough answer that:

  1. Suggests an explanation for why floating point errors occur
  2. Explains common theoretical approaches for dealing with the problem
  3. Also includes some short JavaScript code examples
  4. Warns about potential pitfalls of the built-in JavaScript functions (e.g. that they return strings, that precision may be lost) and suggests potential workarounds

The canonical thread does #1 and #2, but not #3 or #4. Is this kind of answer not welcome anywhere on Stack Overflow? Or does it have to be part of a much narrower and more focused question? In my opinion it would be laborious to have to hunt through multiple questions to gather information on various approaches, when a clear summary would get readers started.

It appears that there is a similar question related to floating point arithmetic in Java that has become somewhat of a canonical, see How to avoid floating point precision errors with floats or doubles in Java?

Is there really no value in having a JavaScript canonical question about floating point precision that covers all 4 (while linking to the main canonical for the general theory and background)?

4
  • 3
    A JavaScript-specific canonical with JavaScript-specific solutions does sound potentially reasonable. I haven't really dug into whether that one is suitable (and I'm not a JavaScript expert by any stretch of the imagination, so I leave that to those who are).
    – Ryan M Mod
    Nov 12 at 5:34
  • 4
    So you want to say that 62 answers aren't enough and that question needs more?
    – Tom
    Nov 13 at 0:32
  • 2
    Just to note: closure isn't handled with the amount of precision you probably think it should be. Questions are only reopened when it makes sense to reopen them; "the original close reason is incorrect" is not enough of an argument. The question has dozens of answers and is from 2009, you'll need a strong argument to reopen something like that. Keeping it closed prevents more unneeded answers from being piled on.
    – Gimby
    Nov 14 at 10:55
  • @Gimby I liked your pun about precision. Nice. 😉 But not really sure where your theory that it's "not enough of an argument" comes from. If a question can be improved to address the close reason, then it should be eligible for reopening, right? If the issue in this case is more about limiting further answers, it could be Protected rather than Closed. When it's closed, it's saying "this question is not welcome on SO, go look elsewhere". Currently I'm blocked from providing a more thorough, canonical answer to the JS-related question. 🙁
    – Simon East
    Nov 19 at 3:39

1 Answer 1

24

The problem is that the first question is asking specifically about JavaScript solutions, while the supposed duplicate is language-agnostic, and more about the general theory of floating point math (it appears that back in 2009 the question referred to Javascript, but it no longer does).

The canonical duplicate (which is generally considered to be among the most important and useful questions on the entire site) was deliberately broadened in scope vs. the original question asked about JavaScript, exactly because it is the same problem with the same solutions regardless of the implementation language.

The problem isn't caused by anything inherent to JavaScript; it's a consequence of the IEEE 754 standard and the fact that computers want to be able to do arithmetic quickly.

The fundamental shape of the solution is the same regardless: use tolerances when comparing for (near-)equality, accept minor inaccuracies and design algorithms to tolerate them; use display formatting to show fewer decimal places where appropriate; use a decimal (or fixed-point, or rational, or...) library if the application demands it (and accept that representing real numbers exactly is still utterly impossible). None of that changes no matter what the implementation language is.

We consider questions to be duplicates when they are about substantively the same thing and answering one answers the other. Asking specifically for JavaScript solutions doesn't make it a JavaScript question, because the JavaScript solutions are the language-agnostic solutions. Unless you count questions of the form "what library can I use to implement exact decimals in JavaScript?", but this would be explicitly off-topic. A question about how to do it manually would either need more focus (as it's asking to implement an entire program feature), or else be something related to the theory (which is adequately covered by the canonical).

There are 62 answers at the closed question; in my view, exactly zero of them tell the reader anything that could not be inferred from the canonical. The 83 answers at the canonical are already massively redundant. We should be trying to point people to fewer, more focused answers that directly explain what is going on - not the opposite.

If you think that answers on the canonical that discuss approaches to workarounds are not prominent enough, upvote them.

8
  • 1
    So which question can people go to to get a thorough understanding of the differences and pitfalls of .toFixed() and .toPrecision() (which both return strings, which when added together without conversion will be incorrectly concatenated) and how to correctly apply them, with code examples. These kinds of language-specific answers wouldn’t belong in the canonical thread.
    – Simon East
    Nov 13 at 10:31
  • @SimonEast It should be specifically one or more questions about how to correctly apply them - for example, a question seeking to fix the incorrect concatenation result, or a question where someone attempted to use .toFixed where .toPrecision would have been appropriate instead, or where someone tried to use one of these methods and it didn't resolve the numerical issue due to one of the pitfalls you mention. Nov 13 at 21:00
  • 1
    But it would not be what the title of this question implies (that's too broad) and it would not be "how do I get the right answer for a specific arithmetic question" (both the approach and the underlying problem are language-agnostic). It could be "how do I implement X technique in language Y for working around floating-point imprecision", e.g. "what support is available for accurate decimal representations in JavaScript?" or "how do I format a float in Python to 2 decimal places [without actually modifying the value]?" - which exists. Nov 13 at 21:04
  • 1
    Re "The 83 answers": 50 of them have been deleted. Though 33 is still a lot. Nov 14 at 4:38
  • Thanks again for your post and comments @KarlKnechtel. I added a bit more context to my original question, and also discovered a similar floating point question that relates to Java, but for some reason that one is welcome (and has become somewhat of a canonical answer), but the JavaScript one (with far more upvotes) is closed. Aren't they very similar?
    – Simon East
    Nov 19 at 4:03
  • 1
    @SimonEast we need close voters and reviewers of those close votes to get questions curated. Not enough users use all their close votes / reviews each day and each day more questions get asked then we have voters for. Things slip through the cracks. I've gone ahead and casted a first duplicate vote on that question you linked. let's see if the reviewers agree with that.
    – rene
    Nov 19 at 7:33
  • @rene Oh no. My intention wasn’t to get both questions closed. I was trying to point out that both actually had language-specific relevance and answers with helpful code examples that would not be relevant in the generic canonical. I really don’t understand this heavy-handedness. Answers about the general theory are not the same as those about a specific language, even though there is definitely some crossover. The code examples and related discussion are very useful and I’m saddened to see them getting suppressed in SO. 🙁
    – Simon East
    Nov 22 at 21:58
  • That Java question as presented was certainly a duplicate, but I can imagine one where someone explicitly asked what techniques are available in Java to work around the problem. I would consider that valid and separate, and be rather annoyed at anyone who wanted to try to close it as a library recommendation - especially since the standard library includes useful approaches. Nov 23 at 3:03

You must log in to answer this question.

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