Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Division by Zero error

Posted on 2003-12-05
10
Medium Priority
?
481 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
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

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

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

670 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