Link to home
Start Free TrialLog in
Avatar of Brian Coughter
Brian CoughterFlag for United States of America

asked on

Division by Zero error

When I set this line:

<cfset CM_aVisits = NumberFormat(CM_Visits.Count / CM_uVisits.Count,'999,999,999,999.9')>

I get a division by zero error if the count is zero.  So then I set this:

<cfoutput>#CM_Avg()#</cfoutput>

<cffunction name="CM_Avg">
<cfif CM_Visits.Count NEQ 0>
      <cfset CM_aVisits = 0>
      <cfelse>
      <cfset CM_aVisits = NumberFormat(CM_Visits.Count / CM_uVisits.Count,'999,999,999,999.9')>
</cfif>
<cfoutput>#CM_Visits#</cfoutput>
</cffunction>

However, I still get the error from the 5th line in the function.  Is there another way to write this so that CF doesn't throw an error?
Avatar of shooksm
shooksm

You are using your numerator for your comparison.  A divide by zero error is based on the denominator being zero.  Update your function to this:

<cffunction name="CM_Avg">
<cfif CM_uVisits.Count NEQ 0>   <!--- This line should contain your denominator --->
     <cfset CM_aVisits = 0>
     <cfelse>
     <cfset CM_aVisits = NumberFormat(CM_Visits.Count / CM_uVisits.Count,'999,999,999,999.9')>
</cfif>
<cfoutput>#CM_Visits#</cfoutput>
</cffunction>
Avatar of Brian Coughter

ASKER

Okay.  I made the change but CF still throws an error on that line in the IF statement.
<cfset CM_aVisits = NumberFormat(CM_Visits.Count / CM_uVisits.Count,'999,999,999,999.9')>

It means the value of CM_uVisits.Count is 0
you can't divide CM_Visits.Count by 0

I would do a <cftry> around the value that could be 0

<cffunction name="CM_Avg">
<cfif CM_uVisits.Count NEQ 0>   <!--- This line should contain your denominator --->
     <cfset CM_aVisits = 0>
     <cfelse>
     <cftry>
     <cfset CM_aVisits = NumberFormat(CM_Visits.Count / CM_uVisits.Count,'999,999,999,999.9')>
     <cfcatch><cfset CM_aVisits = 0><!--- or whatever value is appropiate ---></cfcatch>
     <cftry>
</cfif>
<cfoutput>#CM_Visits#</cfoutput>
</cffunction>
Just caught another error in your if.  You should evaluate it on the value equalling 0 as you want it to run the first set statemant if the CM_uVisits.Count variable is 0:

<cffunction name="CM_Avg">
<cfif CM_uVisits.Count EQ 0>   <!--- This line should contain your denominator and should check to see if it is equal to 0--->
     <cfset CM_aVisits = 0>
     <cfelse>
     <cfset CM_aVisits = NumberFormat(CM_Visits.Count / CM_uVisits.Count,'999,999,999,999.9')>
</cfif>
<cfoutput>#CM_Visits#</cfoutput>
</cffunction>
In addition to shooksm's observation above (EQ instead of NEQ) you were outputting CM_Visist, should be CM_aVisits.  This should work fine...

    <cfif CM_uVisits.Count EQ 0>  
        <cfset CM_aVisits = 0>
    <cfelse>
        <cfset CM_aVisits = NumberFormat(CM_Visits.Count / CM_uVisits.Count,'999,999,999,999.9')>
    </cfif>
    <cfoutput>#CM_aVisits#</cfoutput>
could u paste what the cf error was, otherwise i feel danrosenthal has pointed out the proper stuff.

if it doesn't work just paste the error that cf is showing... that would be morehelpfull

i hope CM_Visits.Count is a structure??

Regards
Hart
ASKER CERTIFIED SOLUTION
Avatar of hart
hart
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you all for your help.  

Hart: Special thanks!!! You nailed it!!!

PE_CEF_DEV: I like your thinking on the <cfreturn> !!!
cool :-)

Regards
Hart