In the process of fixing the rounding error floats have, I noticed something weird.
2.05 * 100
doesn’t give you back 205. It returns 204.99999999999997
instead of 205
.
In the process of fixing the rounding error floats have, I noticed something weird.
2.05 * 100
doesn’t give you back 205. It returns 204.99999999999997
instead of 205
.
And you need to round.
The calculation is still done in binary so decimal numbers will often be periodic
The not periodic are 1/2, 1/4, 1/8 etc, and sums of these
So, you will still have floating point error if you not account to the fact that this errors are present. So, round the value and it will be alright.
I’m already rounding it up by two places.
You will need to round the results of floating point arithmetic, not the intermediary steps.
This is what I’m working with. It won’t round the values using Math.round()
once it’s rounded up by two digits.
function fixRoundingError(input, action) {
if(Array.isArray(input)) {
for(let i = 0; i < input.length; i++) {
if(action === "multiply") {
input[i][1] *= 100;
Math.round(input[i][1]);
} else if (action === "divide") {
input[i][1] /= 100;
}
}
} else if (typeof input === "object") {
for(let property in input) {
if(action === "multiply") {
input[property] *= 100;
} else if(action === "divide") {
input[property] /= 100;
}
}
} else if(typeof input === "number"){
if(action === "multiply") {
input *= 100;
} else if(action === "divide") {
input /= 100;
}
}
return input;
}
console.log(fixRoundingError([["one", 2.05]], "multiply"));
You aren’t assigning the result of Math.round
to anything.
Math.round()
returns a new value, doesn’t change the value on which is applied
Oh yeaa. Bless you. Got it working now.
Glad I could help. Happy coding!
yup got it! thank you