Dividing by Zero

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Dividing by Zero

Post by KennyR » Sat Jan 28, 2012 7:42 pm

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

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Dividing by Zero

Post by KennyR » Sat Jan 28, 2012 9:04 pm

think I have it figured out....I had to write a bunch of "if else" statements ...it looks ugly but it works...

kevin11
Posts: 101
Joined: Thu Aug 11, 2011 5:02 pm

Re: Dividing by Zero

Post by kevin11 » Sun Jan 29, 2012 11:11 am

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 ?

spencer
Posts: 71
Joined: Mon May 09, 2011 3:01 pm

Re: Dividing by Zero

Post by spencer » Sun Jan 29, 2012 2:19 pm

This would be easier to see and think about if it were commented so we knew what vCustom is, and what vCorrCue is.

kevin11
Posts: 101
Joined: Thu Aug 11, 2011 5:02 pm

Re: Dividing by Zero

Post by kevin11 » Mon Jan 30, 2012 2:38 pm

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

spencer
Posts: 71
Joined: Mon May 09, 2011 3:01 pm

Re: Dividing by Zero

Post by spencer » Wed Feb 01, 2012 7:42 pm

Of course. Easy to see when the variable names are chosen so that it is obvious what they stand for.

Post Reply