Does anyone have a good fmod() function written in Javascript ?
Many thanks in advance,
Aaron 14 14320
"Randy Webb" <Hi************@aol.comwrote in message
news:44******************************@comcast.com. ..
Aaron Gray said the following on 7/21/2006 6:24 PM:
>Does anyone have a good fmod() function written in Javascript ?
That depends on what you want fmod() to do.
A floating point modulo function.
Aaron
Aaron Gray wrote:
Does anyone have a good fmod() function written in Javascript ?
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" <an********@gmail.comwrites:
....
>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?
/L

Lasse Reichstein Nielsen  lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Lasse Reichstein Nielsen wrote:
Ah, I can do that:
function fmod(a,b) {
return a % b;
}
Oh would you look at that. For some reason unbeknownst to me, I've
always thought javascript modulus only worked with whole numbers. :)
"Lasse Reichstein Nielsen" <lr*@hotpop.comwrote in message
news:bq**********@hotpop.com...
"Aaron Gray" <an********@gmail.comwrites:
...
>>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 :)
Many thanks,
Aaron
>A floating point modulo function.
>
Ah, I can do that:
function fmod(a,b) {
return a % b;
}
Its got some horrible rounding errors :
1295999.1 % 1296000 = 1295999.1
1296000.1 % 1296000 = 0.10000000009313226
1296001.1 % 1296000 = 1.1000000000931322
Same errors as :
function mods3600( x)
{
return x  1.296e6 * Math.floor(x/1.296e6);
}
Although that only works for positive numbers.
Aaron
Lasse Reichstein Nielsen wrote:
"Aaron Gray" <an********@gmail.comwrites:
...
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?
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:
function mod(num,modulus) {
return ((num<0) ? Math.abs(modulus) : 0) + (num % modulus); }
mod(num,modulus) above implements the classic notion of mod, returning
NaN if modulus is 0. mod(...) satisfies the equation
num=k*modulus+mod(num,modulus) for some integer k
where mod(num,modulus) is non negative and less than modulus. Note
that this differs from fmod(num,modulus), which will be negative iff
num<0. mod(...) will be non negative regardless of whether either or
both arguments are negative. There is no requirement that either a or
b be an integer.
Csaba Gabor from Vienna
web.dev wrote:
Aaron Gray wrote:
Does anyone have a good fmod() function written in Javascript ?
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);
}
Given that JavaScript has a modulus operator, an fmod function isn't
needed. For the sake of it, the above is overly complex  consider:
function fmod(a, b){
while ( b <= a) {a = b}
return a;
}
Which is shorter and likely faster, but is still very slow where
dividend is large and divisor small. It will also vary from the result
given by a%b quite often if either is a float because of the
compounding error from the use of IEEE 64 bit numbers (that has been
covered in great depth before). Consider:
function fmod(a, b){
var x = Math.floor(a/b);
return a  b*x;
}
Which, while still not spot on, is fast and should be close enough if %
is not available and intelligent rounding is used.

Rob
"Aaron Gray" <an********@gmail.comwrites:
Its got some horrible rounding errors :
1295999.1 % 1296000 = 1295999.1
1296000.1 % 1296000 = 0.10000000009313226
The rounding error is not intrinsic to modulus computation, but comes
from the fact that 1296000.1 cannot be represented exactly as a double
precission floating point number.
Try 1296000.1  1296000, which also gives 0.10000000009313226, because
1296000.10000000009313226
is the closest representable number to 1296000.1.
The precission was lost the moment you wrote 1296000.1, it has nothing
to do with the operation, which is actually precise in this case.
/L

Lasse Reichstein Nielsen  lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aaron Gray wrote:
>>A floating point modulo function.
Ah, I can do that: function fmod(a,b) { return a % b; }
Its got some horrible rounding errors :
1295999.1 % 1296000 = 1295999.1
1296000.1 % 1296000 = 0.10000000009313226
1296001.1 % 1296000 = 1.1000000000931322
Further to what Lasse said, you might like to try something that returns
a value based on the number of places used in the input:
function fmod(a, b){
var placesA = (''+a).replace(/^[^.]*(\.)?/,'').length;
var placesB = (''+b).replace(/^[^.]*(\.)?/,'').length;
var places = (placesA placesB)? placesA : placesB;
return (a%b).toFixed(places);
}
alert( fmod(1296001.1, 1296000)
+ '\n' + (1296001.1 % 1296000));
Notes:
toFixed was introduced in JavaScript verison 1.5 and while it is part of
the ECMAScript Ed 3 specification it may not be supported everywhere.
It may be necessary to test the input for suitable values (NaN, zero,
infinity, etc.  ECMAScript spec 11.5.3) and the size of places (it
should be from 0 to 20 inclusive  ECMAScript spec 15.7.4.5).

Rob
JRS: In article <4i************@individual.net>, dated Fri, 21 Jul 2006
23:24:58 remote, seen in news:comp.lang.javascript, Aaron Gray
<an********@gmail.composted :
>Does anyone have a good fmod() function written in Javascript ?
function fmod(X, Y) { return X%Y }
function fdiv(X, Y) { return (X/Y)0 }
Read the newsgroup FAQ.

© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/jsindex.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
In article <11**********************@i3g2000cwc.googlegroups. com>, Csaba
Gabor <da*****@gmail.comwrites
<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:
<snip>
This argument has appeared many times in other news groups. The fact is
that the "mathematician's" mod is defined for the Natural Numbers where
the numbers involved cannot be negative.
You can define mod, %, rem, whatever, for negative numbers but when you
do, you find that different application areas need different sign
conventions.
It's simply not true that there is one sign convention that is more
'correct' than all the others. It appears that most languages that are
specific about the sign follow the Fortran rules whether you like it or
not.
John

John Harris
JRS: In article <4i************@individual.net>, dated Sat, 22 Jul 2006
00:14:13 remote, seen in news:comp.lang.javascript, Aaron Gray
<an********@gmail.composted :
>"Lasse Reichstein Nielsen" <lr*@hotpop.comwrote in message news:bq**********@hotpop.com...
>"Aaron Gray" <an********@gmail.comwrites: ...
>>>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 :)
It's not an overloaded operator; it only has a single manifestation.
Barring future editions, Javascript has only one numeric type, Number,
an IEEE Double; and % has no application except between two Numbers.
One *can* write (new Date()>=0)%0.75 if one wants to generate 0
before 19700101 00:00:00.000 UTC and 0.25 thereafter; but the Boolean
is there forced to Number by the % operator. Indeed, one can use % with
other types; but the results are not useful.

© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/jsindex.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
2 posts
views
Thread by Zunbeltz Izaola 
last post: by

8 posts
views
Thread by seia0106 
last post: by

2 posts
views
Thread by Lonnie Princehouse 
last post: by

6 posts
views
Thread by stau 
last post: by

2 posts
views
Thread by Gintautas 
last post: by

17 posts
views
Thread by joseph.p.doyle 
last post: by

7 posts
views
Thread by bummerland 
last post: by

12 posts
views
Thread by bsabiston 
last post: by
          