Solved

Division by Zero error

Posted on 2003-12-05
10
469 Views
Last Modified: 2013-12-24
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?
0
Comment
Question by:Brian Coughter
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 9

Expert Comment

by:shooksm
ID: 9884887
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>
0
 

Author Comment

by:Brian Coughter
ID: 9884936
Okay.  I made the change but CF still throws an error on that line in the IF statement.
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 9884960
<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>
0
 
LVL 9

Expert Comment

by:shooksm
ID: 9884976
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>
0
 
LVL 15

Expert Comment

by:danrosenthal
ID: 9886108
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>
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 11

Expert Comment

by:hart
ID: 9886919
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
0
 
LVL 11

Accepted Solution

by:
hart earned 400 total points
ID: 9887000
try this

<cffunction name="CM_Avg">
   <CFARGUMENT NAME="VisitsCount" REQUIRED="true">
   <CFARGUMENT NAME="UVisitsCount" REQUIRED="true">
   
   <CFIF UVisitsCount NEQ 0>
       <CFSET AVisitsCount = NumberFormat(VisitsCount / UVisitsCount ,'999,999,999,999.9')>
   <CFELSE>
       <CFSET AVisitsCount = 0>
   </CFIF>
    <cfoutput>#AVisitsCount#</cfoutput>
</cffunction>

and call it as <cfoutput>#CM_Avg('#CM_Visits.Count #','#CM_uVisits.Count#')#</cfoutput>

Regards
Hart
0
 
LVL 6

Assisted Solution

by:PE_CF_DEV
PE_CF_DEV earned 100 total points
ID: 9897217
Yes hart has made an important change. He has required 2 parameters. You shouold not be accessing variables that are not defined in your function, its just bad programing practice. But I would use the <cfreturn> instead of outputing it in your function. The main reason for this is to increase reasuablility. Sometimes you may want to do other stuff with the value before outputing it.


<cffunction name="CM_Avg">
   <CFARGUMENT NAME="VisitsCount" REQUIRED="true">
   <CFARGUMENT NAME="UVisitsCount" REQUIRED="true">
   
   <CFIF UVisitsCount NEQ 0>
       <CFSET AVisitsCount = NumberFormat(VisitsCount / UVisitsCount ,'999,999,999,999.9')>
   <CFELSE>
       <CFSET AVisitsCount = 0>
   </CFIF>
    <cfreturn AVisitsCount>
</cffunction>
output:

<cfoutput>#CM_Avg(CM_Visits.Count ,CM_uVisits.Count)#</cfoutput>
0
 

Author Comment

by:Brian Coughter
ID: 9904310
Thank you all for your help.  

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

PE_CEF_DEV: I like your thinking on the <cfreturn> !!!
0
 
LVL 11

Expert Comment

by:hart
ID: 9910083
cool :-)

Regards
Hart
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now