>0.1 + 0.2
<0.30000000000000004
(more info from actually smart people)
Number.prototype.add = function (float) {
var places = Math.max(...[this, float].map(x => x = x.toString().split(".")[1].length));
return ((this * Math.pow(10, places)) + (float * Math.pow(10, places))) / Math.pow(10, places);
}
my code simply multiplies both floats by the largest decimal place, and then adds them
since that's just normal addition, it works perfectly
then you just need to divide the sum by the largest decimal place and you've got your floating point number without bad
>0.1.add(0.2)
<0.3
tt also works with subtraction (technically)
>0.4.add(-0.1)
<0.3
unfortunately javascript doesn't allow operator overloading, and even if it did i'm pretty sure i'd still not be allowed to interfere with the Number
class (then again, it's javascript, and that's exactly how it works in adobe's extendscript)
so we're stuck with a dumb method. amazing. love it
(yes, i'm aware this only fixes float addition, but i'm too stupid to do multiplication and division)
(and yes, i'm also aware there are a bunch of cases that this won't work on. just let me have my moment.)