Page 1 of 1

Dividing by Zero

Posted: Sat Jan 28, 2012 7:42 pm
by KennyR
Question, hope it not a stupid one... I have a problem with my script for a program that has 3 check boxes, a field for the user to enter a number and a button to cycle through the amount of times the user entered. I wrote a "switch" to trigger actions until the count reaches "0", but my problem starts when the script attempts to calculate a percentage and list them in the appropriate fields. If on of the check boxes is selected it can calculate that percentage fine, but the script gives an error for the other two stating "execution error at line 50 (Operators /: divide by zero), char 15". What should I do here to fix this problem? I will attach the script below...

here is the problem area:
put (vTotal/vCorr)*100 into fld "correctNum"
put (vTotal/vCorrCue)*100 into fld "withCueNum"
put (vTotal/vincorrect)*100 into fld "incorrectNum"

Code: Select all

on mouseUp
   
   global vTotal
   global vCorr
   global vCorrCue
   global vIncorrect
   global vCustom
   put fld "customField" into vCustom
   
   switch vCustom > 0
   
   case the hilite of button "correct"
      add 1 to vCorr
      
      set the hilite of button "correct" to false
     set the hilite of button "correctCue" to false
     set the hilite of button "incorrect" to false
     break
     
  case the hilite of button "correctCue"
     add 1 to vCorrCue
     set the hilite of button "correct" to false
     set the hilite of button "correctCue" to false
     set the hilite of button "incorrect" to false
     break
     
  case the hilite of button "incorrect"
     add 1 to vIncorrect
     set the hilite of button "correct" to false
     set the hilite of button "correctCue" to false
     set the hilite of button "incorrect" to false
     break
     
  default
     
end switch

 if vCustom > 0 then
         add 1 to vTotal
         put vCustom -1 into vCustom
         put vCustom into fld "customField"
         set the label of me to "Next"
         
         put (vTotal/vCorr)*100 into fld "correctNum"
         put (vTotal/vCorrCue)*100 into fld "withCueNum"
         put (vTotal/vincorrect)*100 into fld "incorrectNum"
         
      else
         set the label of me to "Start"
         end if

end mouseUp

Re: Dividing by Zero

Posted: Sat Jan 28, 2012 9:04 pm
by KennyR
think I have it figured out....I had to write a bunch of "if else" statements ...it looks ugly but it works...

Re: Dividing by Zero

Posted: Sun Jan 29, 2012 11:11 am
by kevin11
I'm not sure about this !

Shouldn't vTotal be on the bottom ? That is :

put (vCorr/vTotal)*100 into fld "correctNum"
put (vCorrCue/vTotal)*100 into fld "withCueNum"
put (vincorrect/vTotal)*100 into fld "incorrectNum"

unless I'm missing something ?

Re: Dividing by Zero

Posted: Sun Jan 29, 2012 2:19 pm
by spencer
This would be easier to see and think about if it were commented so we knew what vCustom is, and what vCorrCue is.

Re: Dividing by Zero

Posted: Mon Jan 30, 2012 2:38 pm
by kevin11
spencer wrote:This would be easier to see and think about if it were commented so we knew what vCustom is, and what vCorrCue is.
Yes, but my point is that I think his maths is wrong. To get a percentage you should use :

(correct_answers / total_attempts) * 100

and not

(total_attempts / correct_answers) * 100

He is using, I think, the second method and getting a divide by zero when correct_answers = 0

Re: Dividing by Zero

Posted: Wed Feb 01, 2012 7:42 pm
by spencer
Of course. Easy to see when the variable names are chosen so that it is obvious what they stand for.