A
Aaron Gray
Does anyone have a good fmod() function written in Javascript ?
Many thanks in advance,
Aaron
Many thanks in advance,
Aaron
Does anyone have a good fmod() function written in Javascript ?
Randy Webb said:Aaron Gray said the following on 7/21/2006 6:24 PM:
That depends on what you want fmod() to do.
Aaron said:Does anyone have a good fmod() function written in Javascript ?
Aaron Gray said:....[which is]...Aaron Gray said the following on 7/21/2006 6:24 PM:
A floating point modulo function.
Lasse said:Ah, I can do that:
function fmod(a,b) {
return a % b;
}
Lasse Reichstein Nielsen said:Aaron Gray said:...[which is]...Aaron Gray said the following on 7/21/2006 6:24 PM:
Does anyone have a good fmod() function written in Javascript ?
A floating point modulo function.
Ah, I can do that:
function fmod(a,b) {
return a % b;
}
Or, do you need something else?
Ah, I can do that:
function fmod(a,b) {
return a % b;
}
Lasse said:Aaron Gray said:...[which is]...Aaron Gray said the following on 7/21/2006 6:24 PM:
Does anyone have a good fmod() function written in Javascript ?
A floating point modulo function.
Ah, I can do that:
function fmod(a,b) {
return a % b;
}
Or, do you need something else?
web.dev said:I'm assuming you're looking for a javascript port of the fmod function
in php.
Using the php example as a basis:
$x = 5.7;
$y = 1.3;
$r = fmod( $x, $y);
//$r equals 0.5, because 4 * 1.3 + 0.5 = 5.7
I threw together this inefficient method, plus I did not take care of
the intricacies behind javascript's floating point precision.
function fmod(dividend, divisor)
{
var multiplier = 0;
while(divisor * multiplier < dividend)
{
++multiplier;
}
--multiplier;
return dividend - (divisor * multiplier);
}
Aaron Gray said:Its got some horrible rounding errors :-
1295999.1 % 1296000 = 1295999.1
1296000.1 % 1296000 = 0.10000000009313226
Aaron said:Its got some horrible rounding errors :-
1295999.1 % 1296000 = 1295999.1
1296000.1 % 1296000 = 0.10000000009313226
1296001.1 % 1296000 = 1.1000000000931322
Does anyone have a good fmod() function written in Javascript ?
<snip>Yes. I've ever hated fmod. A mod should always be non negative.
That's what they were when I was a kid, and that's what they should be
now, darnit. The mathematician's mod:
Lasse Reichstein Nielsen said:Aaron Gray said:Aaron Gray said the following on 7/21/2006 6:24 PM:
Does anyone have a good fmod() function written in Javascript ? ...[which is]...
A floating point modulo function.
Ah, I can do that:
function fmod(a,b) {
return a % b;
}
Or, do you need something else?
No, thats the one, nice and integeral being in Javascript as an overloaded
operator, great![]()
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.