Skip to content

Instantly share code, notes, and snippets.

@wteuber
Last active February 6, 2024 14:47
Show Gist options
  • Save wteuber/6241786 to your computer and use it in GitHub Desktop.
Save wteuber/6241786 to your computer and use it in GitHub Desktop.
fmod for Javascript, will work with any ECMA-262 implementation.If you need a precision higher than 8, please use another implementaion of fmod.
/*
fmod for Javascript, will work with any ECMA-262 implementation.
If you need a precision higher than 8, please use another implementation of fmod.
1.05 % 0.05
=> 0.04999999999999999
Math.fmod(1.05, 0.05)
=> 0
Math.fmod(1.05, 0.019)
=> 0.005
Math.fmod(1.00000012, 0.00000005)
=> 0.00000002
fmod will most likely fail, if (Math.log(a)/Math.log(10) - Math.log(b)/Math.log(10)) > 8
Try to keep the difference in order of magnitudes as small as possible!:
Math.fmod(1000000.40, 0.07)
=> 0.07
Math.fmod(10000000.40, 0.07)
=> 0.039999999
*/
/* TODO: fix for a=4.55 b=0.05 !!*/
Math.fmod = function (a,b) { return Number((a - (Math.floor(a / b) * b)).toPrecision(8)); };
@wteuber
Copy link
Author

wteuber commented Feb 6, 2024

Great, thanks for sharing 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment