Solved

Division by Zero error

Posted on 2003-12-05
10
479 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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
 
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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

630 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