What does 'mod' mean?

george7378

DON'T PANIC
Addon Developer
Donator
Joined
Jun 26, 2009
Messages
1,045
Reaction score
0
Points
36
Hi everyone,

I was reading this page:

[ame="http://en.wikipedia.org/wiki/Sunrise_equation"]Sunrise equation - Wikipedia, the free encyclopedia[/ame]

...and I have noticed that there is a 'mod' function in some of the equations further down the page - what does this mean, and how would I use it? I'm not really familiar with what it does. An example would be great!

Thanks!
 
[ame="http://en.wikipedia.org/wiki/Modulo_operation"]Modulo operation - Wikipedia, the free encyclopedia[/ame]
 
Seems kinda self explanatory from the operations. You have an angle that goes from 0 to (not including) 360. If the calculation returns more, you need to take care of it somehow...
 
Ah - so it tells you the remainder when you divide all the stuff on the left by 360? Also, does anyone know how I would use mod in C++? The Wiki page says that the symbol is % - would it be as simple as just putting [stuff on left]%360? Thanks!
 
[stuff on left]%360?
That one. But remember about precedence of operators, and use parenthesis when needed.

---------- Post added ----------

I forgot that your modulo isn't exactly a remainder but [ame=http://en.wikipedia.org/wiki/Modular_arithmetic]arithmetic modulo[/ame], so for negative results of remainder you need to add to it 360.
 
Thanks for the update - is the arithmetic modulo any different in C++ (i.e. do I have to account for negative remainders myself)?
 
OK - so something like this:

remainder = [stuff on left] % 360;

if (remainder < 0)
answer = remainder + 360;

else answer = remainder;

Sorry - I just want to make sure it's crystal clear, since I haven't met it before.
 
Yes, something like that.
 
Great - thankyou for your help! I'll use that to calculate the mean anomaly and ecliptic longitude.

---------- Post added at 14:11 ---------- Previous post was at 10:15 ----------

...also, can anyone tell me what the 'round(n*)' function does on the same page? Does it round to the nearest whole number, or does it truncate it or round it up? Thanks.
 
Have a look at this: http://pubs.opengroup.org/onlinepubs/9699919799/


NAME
round, roundf, roundl - round to the nearest integer value in a floating-point format
SYNOPSIS
#include <math.h>
double round(double x);
float roundf(float x);
long double roundl(long double x);
DESCRIPTION
[CX] The functionality described on this reference page is aligned with the ISO C standard. Any conflict
between the requirements described here and the ISO C standard is unintentional. This volume of
IEEE Std 1003.1-2001 defers to the ISO C standard.
These functions shall round their argument to the nearest integer value in floating-point format, rounding
halfway cases away from zero, regardless of the current rounding direction.
An application wishing to check for error situations should set errno to zero and call
feclearexcept(FE_ALL_EXCEPT) before calling these functions. On return, if errno is non-zero or
fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) is non-zero, an
error has occurred.
RETURN VALUE
Upon successful completion, these functions shall return the rounded integer value.
[MX] If x is NaN, a NaN shall be returned.
If x is ±0 or ±Inf, x shall be returned.
[XSI] If the correct value would cause overflow, a range error shall occur and round(), roundf(), and
roundl() shall return the value of the macro ±HUGE_VAL, ±HUGE_VALF, and ±HUGE_VALL (with the
same sign as x), respectively.
ERRORS
These functions may fail if:
Range Error
[XSI] The result overflows.
If the integer expression (math_errhandling & MATH_ERRNO) is non-zero, then errno shall be set
to [ERANGE]. If the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero,
then the overflow floating-point exception shall be raised.

EXAMPLES
None.
APPLICATION USAGE
On error, the expressions (math_errhandling & MATH_ERRNO) and (math_errhandling &
MATH_ERREXCEPT) are independent of each other, but at least one of them must be non-zero.
 
Last edited:
Mod is very useful if you want to trap a value between a defined range...

Personally I use it mostly like this:

Code:
aNumber += anotherNumber % highLimit //keeps aNumber between 0 and (highlimit - 1)

With it you can cycle through onwards, and whenever the value reaches limit, it will fall back to zero again.
This is especially useful for positioning things on a grid.

You can also use it to check if a number is odd or even:

Code:
if (number % 2 == 0) it's even!

Or just check if a number is a multiple of another:

Code:
if (number % 4 == 0) it's a multiple of 4!

Hope this helps

BTW, there should be such a thing as a sqrt mod operator... so you can check if the sqrt of a number is a round number or not... this way we could check to see if a 2D grid of values is squarely divided or not, without having to go:
Code:
if (sqrt(arrayLength) - round(sqrt(arrayLength)) == 0)

Cheers
 
Last edited:
OK - so something like this:

remainder = [stuff on left] % 360;

if (remainder < 0)
answer = remainder + 360;

else answer = remainder;

Sorry - I just want to make sure it's crystal clear, since I haven't met it before.
Tidying it up a bit:
Code:
remainder = [stuff on left] % 360;
if(remainder < 0)
  remainder += 360;

return remainder;
or even more concise:
Code:
remainder = [stuff on left] % 360;
return remainder >= 0 ? remainder : remainder + 360;
 
Note that the modulo operator (%) can only be applied to integer arguments, returning an integer result. For floating point reminders of floating point arguments, you should use the fmod function instead, e.g.
Code:
theta = fmod(theta, 2*pi);
 
wow.........something i can finally relate to learning from a classroom......first time :D
 
Back
Top