Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Divide by zero problem

Contributor ,
Nov 29, 2010 Nov 29, 2010

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

2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Nov 29, 2010 Nov 29, 2010

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

...
Translate
Contributor ,
Nov 29, 2010 Nov 29, 2010

Apologies it only happens if the divisor is less than 1

e.g. 0 MOD 0.25


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 29, 2010 Nov 29, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 29, 2010 Nov 29, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 30, 2010 Nov 30, 2010
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources