ID:270823
Sep 17 2006, 2:47 pm
|
|
How do you check if a variable is evenly divisible my a number?
|
In response to Audeuro
|
|
Audeuro wrote:
> #define DIVISIBLE(a,b) (!(a%b)) Baah, wrong! That'll fail miserablly if you want to use DIVISABLE(1 + 2, 9), since macros replace whatever you put in during compile time. You either should make it its own proc, or put parenthesis around a and b. #define DIVISABLE(a,b) !((a)%(b)) ~~> Unknown Person |
#define DIVISIBLE(a,b) (!(a%b))
% is the modulus operator. a%b returns the remainder of a/b, so if it's 0, then a is divisible by b. If it is non-zero, then a is NOT divisible by b.