0

I have an issue,

I have a number that I wish to divide by 49 as shown in the code below:

// in Javascript:
let referenceNumber= 3487039819743582477;
let division = referenceNumber /49;

//I am using https://github.com/dcodeIO/long.js

//Now when I do the following with the Long.js library:
let longValue = Long.fromValue(division); //Long.js gave me    71164077953950662 as the result

//Now, in Kotlin:
val longValue = division.toDouble().toLong(); //Kotlin gave me 71164077953950664 as result 

// As you can see, there is a difference of 2 between the two programming languages.

Which is the correct value here and How do I rectify this? I want both languages to give me exact value all the time after division and conversion to long

Thank you.

mekings
  • 361
  • 3
  • 17
  • Dupe of this?https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript – mplungjan Jan 16 '21 at 14:48

1 Answers1

0

The problem is that you are doing toDouble(), which adds imprecision.

println(3487039819743582477 / 49) prints 71164077953950662.

If you want to know about Double and imprecision, check out https://floating-point-gui.de/.

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74