Home:ALL Converter>Correcting floating math broken by multiplying, is it ok?

Correcting floating math broken by multiplying, is it ok?

Ask Time:2014-05-08T00:16:43         Author:Prosto Trader

Json Formatter

I have multiple calculations which have troubles because of floating math problem in js. I'm trying to fix it by multiplying all parts of equation by 1000 and then dividing the result by 1000.

var a = 16924.83,
    b = 16835.61;

var c = a-b; //outputs 89.22000000000116
var d = (1000*a-1000*b)/1000;  //outputs 89.22 and seems to be working correct

Is it ok to do correction of floating math problem this way?

I did read Is floating point math broken?

Author:Prosto Trader,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/23523065/correcting-floating-math-broken-by-multiplying-is-it-ok
yy