0

I have some unexpected answers when I do calculations with Javascript. Example:

 var a = 1.12064;
 var b = 1.12065;
 var c = 1.13498;
 var d = 1.13497;
 var num = b-a;
 var answer =c-d;
 console.log(num); 
 console.log(answer);

The expected result for both is 0.00001. However, the num returns 0.0000099. Any suggestion to have a result as in a normal calculator? Thanks in advance.

Antonis
  • 195
  • 1
  • 3
  • 13
  • Sure, JavaScript uses Float64 by default, it has precision of 64 binary digits. 0.00001 can't be represented exactly using final number of binary digits, yet alone 64 binary digits. – FlatAssembler Jun 05 '20 at 13:07
  • Devices and software designed to work like traditional calculators use a decimal representation for numbers. JavaScript (in company with many other languages) does not; they're *binary* fraction representations. – Pointy Jun 05 '20 at 13:07

0 Answers0