Tuesday, 15 May 2018

javascript - float number math problem addition and subtraction

I know JavaScript has a notorious problem with math that can cause math problems to return wrong with wonky decimal numbers. But how does one do a simple addition and subtraction in JS and not have a wonky result? And the numbers for the addition and subtraction are not fixed numbers and relay on variables. For example...




addition



var a = 3.94
var b = 0.5
var r = a + b
console.log(r) //4.4399999999999995


subtract




var a = 4.22
var b = 0.5
var r = a - b
console.log(r) //3.7199999999999998


I am aware of the math floating point issue in JS but I am looking for a simple solution to not have this issue occur when doing some addition and subtraction.



Is floating point math broken?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...