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
Solved

Division by Zero error

Posted on 2003-12-05
10
475 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
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.

 
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
website does not load without www 12 74
Redundant SQL Servers Without Clustering 7 119
Change local server setting in php 6 94
spamming  on Hosted svrs? 6 98
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
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 …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 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