Old02-15-2015, 09:36 PM  #1
Human being with feelings
 
Argitoth's Avatar
 
Join Date: Feb 2008
Location: Mesa, AZ
Posts: 2,057
Default formula for modulo?

need floating point modulo, so need formula for jsfx rather than using built in one.

anyone know it? thanks!

got it

function mod(f,mod) ( f-floor(f/mod)*mod );
__________________
Soundemote - Home of the chaosfly and pretty oscilloscope.
MyReaperPlugin - Easy-to-use cross-platform C++ REAPER extension template

Last edited by Argitoth; 02-15-2015 at 11:20 PM.
Argitoth is offline   Reply With Quote
Old02-16-2015, 01:09 AM  #2
Human being with feelings
 
Join Date: Oct 2013
Location: Seattle, WA
Posts: 877
Default

How is that different from using the % operator?
SaulT is offline   Reply With Quote
Old02-16-2015, 04:44 AM  #3
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,682
Default

The % operator works with integer numbers, while Argitoth's mod() works with floating point numbers. And I think they return different signs when the modulus is negative.
Tale is offline   Reply With Quote
Old02-16-2015, 06:00 AM  #4
Human being with feelings
 
planetnine's Avatar
 
Join Date: Oct 2007
Location: Lincoln, UK
Posts: 7,981
Default

I define my own INT and MOD functions in EEL -INT is floor for >0 and ceil for <=0, and then MOD uses INT.



>
planetnine is offline   Reply With Quote
Old02-16-2015, 07:53 AM  #5
Human being with feelings
 
Banned's Avatar
 
Join Date: Mar 2008
Location: Unwired (probably in the proximity of Amsterdam)
Posts: 4,868
Default

Quote:
Originally Posted by planetnine View Post
I define my own INT and MOD functions in EEL -INT is floor for >0 and ceil for <=0, and then MOD uses INT.
That makes sense for 'better' rounding of a float value to an int, but for a floating point modulo function, one may also need the fractional part of the remainder after division by an integer, no?
__________________
˙lɐd 'ʎɐʍ ƃuoɹʍ ǝɥʇ ǝɔıʌǝp ʇɐɥʇ ƃuıploɥ ǝɹ,noʎ
Banned is offline   Reply With Quote
Old02-16-2015, 08:49 AM  #6
Human being with feelings
 
planetnine's Avatar
 
Join Date: Oct 2007
Location: Lincoln, UK
Posts: 7,981
Default

Well it's easy enough to make a REM function in addition to your MOD function, once you have a functioning INT


Code:
function int(val) local(res)
(
	val >=0 ? // calculates true integer of pos and neg values
	(
		res = floor(val);
	):(
		res = ceil(val);
	);
	res;
);


function mod(num, divisor)
(
	int(num/divisor);
);

function remdr(num, divisor)
(
	num - (mod(num) * divisor);
);

Edit: Hehe, I'm using modulo for the integer part and remainder for what is actually modulo. If you ignore the screwed-up terminology, it works



>

Last edited by planetnine; 02-16-2015 at 09:15 AM.
planetnine is offline   Reply With Quote
Old02-16-2015, 10:41 AM  #7
Human being with feelings
 
Banned's Avatar
 
Join Date: Mar 2008
Location: Unwired (probably in the proximity of Amsterdam)
Posts: 4,868
Default

Is it? I don't see why conversion to int is a prerequisite for a MOD function. After conversion to int, you have lost the fractional part. So I'd rather just have a MOD function that works with float input and output, and convert to int when needed afterwards.
__________________
˙lɐd 'ʎɐʍ ƃuoɹʍ ǝɥʇ ǝɔıʌǝp ʇɐɥʇ ƃuıploɥ ǝɹ,noʎ
Banned is offline   Reply With Quote
Old02-16-2015, 01:16 PM  #8
Human being with feelings
 
planetnine's Avatar
 
Join Date: Oct 2007
Location: Lincoln, UK
Posts: 7,981
Default

If you read my Edit, I've used the term mod erroneously for the integer division part, and "remainder" for the actual mod value.

I can't remember the mathematical term for the integer division bit

So, more like:
Code:
function int(val) local(res)
(
	val >=0 ? // calculates true integer of pos and neg values
	(
		res = floor(val);
	):(
		res = ceil(val);
	);
	res;
);


function int_div(num, divisor)
(
	int(num/divisor);
);

function mod(num, divisor)
(
	num - (intdiv(num, divisor) * divisor);
);
It can be written more compactly, but it depends if you need the intermediate stages' functions in your script.



>
planetnine is offline   Reply With Quote
Old02-17-2015, 09:40 AM  #9
Human being with feelings
 
Join Date: Oct 2013
Location: Seattle, WA
Posts: 877
Default

Are we saying that JS converts floats to integers before doing a modulus?
SaulT is offline   Reply With Quote
Old02-17-2015, 09:50 AM  #10
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,682
Default

Quote:
Originally Posted by SaulT View Post
Are we saying that JS converts floats to integers before doing a modulus?
Well, if you use the % operator then: Yeah, I guess. C/C++ also does this, the reason is probably that the % operator results in a DIV instruction.
Tale is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 12:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.