Skip to main content
Inspiring
November 30, 2010
Answered

Divide by zero problem

  • November 30, 2010
  • 1 reply
  • 2303 views

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

    This topic has been closed for replies.
    Correct answer TheRealAgentK

    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

    1 reply

    doug777Author
    Inspiring
    November 30, 2010

    Apologies it only happens if the divisor is less than 1

    e.g. 0 MOD 0.25


    TheRealAgentKCorrect answer
    Participating Frequently
    November 30, 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