I’m working in a motion class for my babylonjs project and I have some issues interpolating arrays of numbers.
I believe this is caused for the javascript floating point precision issues: [Link]
Is there some recommended library to deal with this problem?
I’ve seen some of them (big.js, bignumber.js, decimal.js, bigdecimal.js…) but I’m not sure which one could be the best regarding performance.
It sounds like you’ve already made up your mind about your issues being related to “flops”. Just for curiosity’s sake then, could you describe the problem you’re seeing with vector interpolation? Is there some code or a PG you have that shows this?
It’s tough to recommend anything specific to your needs without having a better understanding of the problem you’re trying to solve
Agree with @jelster on this one, JS is actually fortunate enough (or way too expensive depending on how you look into it ) to be all based on double so 64 bit precisions which is more than plenty in the majority of cases for interpolations.
Would be great to know a tiny bit more about your use case.
@jelster@sebavan sorry I didn’t explain well. I’m not using a BabylonJs vector, I’m using a custom vector (an array of numbers). I had Issues with some very small calculations, but I found a mistake in the algorithm and it is fixed now Using custom vector allows me to use more than 3 or 4 values, then I can add scale, rotation, colors, AI params, or whatever.
This is the algorithm (there was a wrong reduce there):
const increaseVector = (from: number[], to: number[], speed: number, completed?: () => void): number[] => {
const complete = () => {
if (completed) {
completed();
}
return to;
};
const director = [];
for (let i = 0; i < from.length; i++) {
director.push(to[i] - from[i]);
}
let lengthPow2 = 0;
director.forEach((value) => (lengthPow2 += Math.pow(value, 2)));
if (lengthPow2 < MIN_DISTANCE) {
return complete();
}
const length = Math.sqrt(lengthPow2);
const directorNormalized = director.map((value) => value / length);
let result = [];
for (let i = 0; i < from.length; i++) {
result.push(increaseValue(from[i], to[i], Math.abs(directorNormalized[i]) * speed));
}
return result;
};
My question wasn’t about JS nor BabylonJs itself, my question is which one of the Big Decimals libraries should I use beside BabylonJs in case I need it.