472,980 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,980 software developers and data experts.

Javascript fmod() wanted

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

Many thanks in advance,

Aaron
Jul 21 '06 #1
14 16326
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 21 '06 #2
"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
Jul 21 '06 #3

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

Jul 21 '06 #4
"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.'
Jul 21 '06 #5

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

Jul 21 '06 #6
"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
Jul 21 '06 #7
>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
Jul 22 '06 #8
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

Jul 22 '06 #9

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

Jul 22 '06 #10
"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.'
Jul 22 '06 #11
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
Jul 22 '06 #12
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/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 22 '06 #13
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
Jul 22 '06 #14
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 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.

--
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/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 22 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zunbeltz Izaola | last post by:
Hi, I have a problem with % operation. It returns a incorrect value in this case: >>> -2.77555756156e-17%1 1.0 where the returned value should be -2.77555756156e-17.
8
by: seia0106 | last post by:
Hello, I have a C++ program , which has following two lines of code z=atan2(x,y); z=(float)(fmod(2*pi+z, 2*pi); The comments written by the other programmer says that second line is used to...
2
by: Lonnie Princehouse | last post by:
I've been trying to debug this for two days now, and it's a longshot but I'm hoping that someone here might recognize a solution. I've got a C extension which calls a function in a C library,...
6
by: stau | last post by:
Hi! I'm reading a C book, and it says that fmod() returns the remainder of the exact division of it's arguments. Well, in a exact division, the remainder shall always be 0 (zero), so this don't...
2
by: Gintautas | last post by:
I'm trying to play a part of wav file. FSOUND_Sample_Load (0,"T01.wav",FSOUND_NORMAL, 0,0); plays all file FSOUND_Sample_Load (0,"T01.wav",FSOUND_NORMAL, 0,90000); plays file until 90000 sample...
17
by: joseph.p.doyle | last post by:
This code, compiled with visual studio .NET 2003, double a = 95.022, b = 0.01; printf ("%lf - floor(%lf / %lf) * %lf = %.17lf\n", a, a, b, b, a - floor(a / b) * b); a = 95.021, b = 0.01;...
7
by: bummerland | last post by:
Hi, I have a problem with the function fmod. Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0? tia bummerland
12
by: bsabiston | last post by:
Hi, I'm trying to get the fractional part of a floating point number. I've tried fmod() and modf(), and while both do work, they also occasionally return 1.0 for the fractional part of the number...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.