help icon indicating copy to clipboard operation
help copied to clipboard

Error in sum!

Open JohnsonMauro opened this issue 9 years ago • 4 comments

  • Node.js Version: 7.10.0
  • OS: Windows (64-bit)
  • Scope (install, code, runtime, meta, other?):
  • Module (and version) (if relevant):

Good morning to the Devs (Brazilian time)!

Bem, gostaria apenas de registrar um erro que eu acidentalmente encontrei com uma soma feita nos terminais de acordo com a impressão anexada, eu testei a soma com vários números decimais, porém o erro acaba sendo ocasionando com os decimais .37 e .38! error sum 1 error sum

Thank you for the excellent work done to NodeJs!

JohnsonMauro avatar May 04 '17 07:05 JohnsonMauro

Hello JohnsonMauro, Nothing fundamentally wrong with Node.js; what you have observed is typical with any floating-point arithmetic (and a multitude of other programming languages). Your calculations may be expressed as [float] + [int], but are in practice is converted into [float] + [float] to maintain the precision of your operand.... Except that also creates the scope for imprecision at the very extreme (least significant) bits of what can be represented and accurately maintained in the mantissa, hence you observe discrepancies in the 37th/38th decimal digits.

shellberg avatar May 04 '17 12:05 shellberg

@shellberg Ahhh, okay, thanks for letting me know!

JohnsonMauro avatar May 04 '17 14:05 JohnsonMauro

expressed as [float] + [int], but are […] converted into [float] + [float]

Since this thread is a prominent reference for future learners: Do you mean the conversion for parsing it as a Number type, or do we nowadays have actual integers in standard JS? (not asm.js)

People wondering about the precision: JS Numbers are 64 bit, so if you're from C++, read "[double]" instead of "[float]". ;-)

While we're at it, have a look at Number.MAX_SAFE_INTEGER because 1e99 === (1e99 + 1).

mk-pmb avatar May 05 '17 23:05 mk-pmb

Hi all, Since there's an agreement that this is "Floating Point Arithmetic" issue and is not node specific, I'm going to close this issue. @JohnsonMauro Feel free to reopen if you find anything that is a direct node issue.

refack avatar May 06 '17 14:05 refack