0

I am using lodash version 4.17.15 Here when I am using its _.sum method, with certain values, I'm getting a bit odd result Here are some examples

_.sum([5.32,1.25,1.3])

this is giving correct result #7.87

but when I'm doing this

_.sum([6.32,1.25,1.3])

the result is #8.870000000000001

I unable to find any reason or any supporting document on how this sum method actually works and why its giving such a drastically different result where apparently I just gave it 3 numbers to do a simple summation.

I was doing a live test here

Julian
  • 4,176
  • 19
  • 40
Souvik Das
  • 41
  • 1
  • 7

1 Answers1

1

This is due to javascript's precision. If you add those 3 numbers together in the console you will get the same result, so it is not an issue with lodash.

You can check this question for more info on the subject

Luís Alves
  • 162
  • 8
  • Wow..had no clue about this precision issue with java script. who can think adding such simple fractional numbers can give two different result based on just numbers themselves being different. Thanks a lot for pointing me to that post, very interesting read. – Souvik Das Apr 21 '22 at 13:27