ID:270823
 
How do you check if a variable is evenly divisible my a number?
#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.
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))

proc/divisable(a, b)
return !(a % b)


~~> Unknown Person