 | |  |
02-15-2015, 09:36 PM | #1 | Human being with feelings Join Date: Feb 2008 Location: Mesa, AZ Posts: 2,057 | 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. | |  |
02-16-2015, 01:09 AM | #2 | Human being with feelings Join Date: Oct 2013 Location: Seattle, WA Posts: 877 |
How is that different from using the % operator? | |  |
02-16-2015, 04:44 AM | #3 | Human being with feelings Join Date: Jul 2008 Location: The Netherlands Posts: 3,682 |
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. | |  |
02-16-2015, 06:00 AM | #4 | Human being with feelings Join Date: Oct 2007 Location: Lincoln, UK Posts: 7,981 |
I define my own INT and MOD functions in EEL -INT is floor for >0 and ceil for <=0, and then MOD uses INT.
> | |  |
02-16-2015, 07:53 AM | #5 | Human being with feelings Join Date: Mar 2008 Location: Unwired (probably in the proximity of Amsterdam) Posts: 4,868 |
Quote: Originally Posted by planetnine  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ʎ | |  |
02-16-2015, 08:49 AM | #6 | Human being with feelings Join Date: Oct 2007 Location: Lincoln, UK Posts: 7,981 |
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. | |  |
02-16-2015, 10:41 AM | #7 | Human being with feelings Join Date: Mar 2008 Location: Unwired (probably in the proximity of Amsterdam) Posts: 4,868 |
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ʎ | |  |
02-16-2015, 01:16 PM | #8 | Human being with feelings Join Date: Oct 2007 Location: Lincoln, UK Posts: 7,981 |
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. > | |  |
02-17-2015, 09:40 AM | #9 | Human being with feelings Join Date: Oct 2013 Location: Seattle, WA Posts: 877 |
Are we saying that JS converts floats to integers before doing a modulus? | |  |
02-17-2015, 09:50 AM | #10 | Human being with feelings Join Date: Jul 2008 Location: The Netherlands Posts: 3,682 |
Quote: Originally Posted by SaulT  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. | |  |
Thread Tools | | Display Modes | Linear Mode |
Posting Rules | You may not post new threads You may not post replies You may not post attachments You may not edit your posts
HTML code is Off
|
| |
All times are GMT -7. The time now is 12:43 AM. |