Solved

Division by Zero error

Posted on 2003-12-05
10
473 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Script 12 157
Internal DNS Zone Issue 13 74
.htaccess rewrite rule to 301 redirect canonical versions of homepage to root 4 100
AWS New EC2 Instance and EBS Storage 2 78
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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