Copy link to clipboard
Copied
Why does n%3 or n MOD 3 return 'Division by zero is not allowed.', if n == 0? (3 can be any number).
Using CF 9,0,1,274733
Doug
The modulo operator being used with real/double numeric values doesn't make much sense from a maths point of view in the first place as it's really meant to provide you the remainder after an integer division.
ColdFusion still lets you do this, but if the right hand side of the modulo operator is a real/dobule numeric value it will just ignore the fractional part. In your case it will do 3 MOD 0 -> therefore division by zero error.
This particular behaviour is actually documented in the CF documen
...Copy link to clipboard
Copied
Apologies it only happens if the divisor is less than 1
e.g. 0 MOD 0.25
Copy link to clipboard
Copied
The modulo operator being used with real/double numeric values doesn't make much sense from a maths point of view in the first place as it's really meant to provide you the remainder after an integer division.
ColdFusion still lets you do this, but if the right hand side of the modulo operator is a real/dobule numeric value it will just ignore the fractional part. In your case it will do 3 MOD 0 -> therefore division by zero error.
This particular behaviour is actually documented in the CF documentation as such.
Cheers
Kai
Copy link to clipboard
Copied
In case you are still asking why this occurs, when calculating the remainder using MOD, ColdFusion works only with integers. Any fractions are truncated.
So when you do:
2 mod 0.25
you are essentially doing
2 mod 0
Which is dividing by zero.
If you already knew this, then my apologies. I just thought the solution should be documented in case anyone else comes along with this issue.
Jason
Copy link to clipboard
Copied
Just to give the official (ie: documented) position on this:
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09d55-7ffc.html#WSc3ff6d0ea77859461172e0811cbec09d55-7ffb
MOD or %
Modulus: Return the remainder after a number is divided by a divisor. The result has the same sign as the divisor. The value to the right of the operator should be an integer; using a non-numeric value causes an error, and if you specify a real number, ColdFusion ignores the fractional part (for example, 11 MOD 4.7 is 3).
(my emphasis).
Also: http://en.wikipedia.org/wiki/Remainder
--
Adam
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more