Javascript fmod() wanted

A

Aaron Gray

Does anyone have a good fmod() function written in Javascript ?

Many thanks in advance,

Aaron
 
R

Randy Webb

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

Aaron Gray

Randy Webb said:
Aaron Gray said the following on 7/21/2006 6:24 PM:

That depends on what you want fmod() to do.

A floating point modulo function.

Aaron
 
W

web.dev

Aaron said:
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);
}
 
L

Lasse Reichstein Nielsen

Aaron Gray said:
Aaron Gray said the following on 7/21/2006 6:24 PM:
....[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
 
W

web.dev

Lasse said:
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. :)
 
A

Aaron Gray

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 :)

Many thanks,

Aaron
 
A

Aaron Gray

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
 
C

Csaba Gabor

Lasse 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?

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
 
R

RobG

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);
}

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.
 
L

Lasse Reichstein Nielsen

Aaron Gray said:
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
 
R

RobG

Aaron said:
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).
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 21 Jul 2006
23:24:58 remote, seen in Aaron Gray
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.
 
J

John G Harris

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 22 Jul 2006
00:14:13 remote, seen in Aaron Gray
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 :)


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 1970-01-01 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.
 

Ask a Question

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.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top