# Divide by 0 Error

• ### Question

• I have been coding a maths machine for a while and I came across a problem where it divides by zero; but it shouldn't.

Sub Equation_Worker
MathsRoundedNames[2] = "(Floored)"
MathsRoundedNames[3] = "(Ceilinged)"
MathsAnswer[1] = Controls.GetTextBoxText(MathTextBox[1]) - Controls.GetTextBoxText(MathTextBox[3])
MathsAnswer[2] = Controls.GetTextBoxText(MathTextBox[4]) - Controls.GetTextBoxText(MathTextBox[2])
If MathsAnswer[1] < 0 Then
Math.Abs(MathsAnswer[1])
MathsAnswer[2] = MathsAnswer[2] * -1
EndIf
If MathsAnswer[1] <> 0 Then
MathsAnswer[3] = MathsAnswer[2] / MathsAnswer[1]
Else
MathsAnswer[3] = 0
EndIf
If UsersRounding[User] = 2 Then
MathsAnswer2[1] = Math.Floor(MathsAnswer[3])
ElseIf UsersRounding[User] = 3 then
MathsAnswer2[1] = Math.Ceiling(MathsAnswer[3])
Else
MathsAnswer2[1] = MathsAnswer[3]
EndIf
if MathsAnswer2[1] <> MathsAnswer[3] Then
MathsRounded[1] = UsersRounding[User]
MathsAnswer[1] = MathsAnswer2[1]
Else
MathsRounded[1] = 1
MathsAnswer[1] = MathsAnswer[3]
EndIf
Sound.PlayChime()
TextWindow.WriteLine("a = " + MathsAnswer[1] + " " + MathsRoundedNames[MathsRounded[1]])
EndSub

(This is not the whole thing)

It should not be able to have that error but it does.

FYI, the textboxes go:

[1]         [2]            [3]          [4]

(a) (+/- number) = (a) (+/- Number)

Please Help - I don't know why it doesn't work

Monday, October 19, 2015 3:31 PM

### Answers

• I think maybe the statement:

If MathsAnswer[1] <> 0 Then

is not formatted correctly.

what if you formatted it as:

If MathsAnswer[1] < 0 Then

If MathsAnswer[1] > 0 Then

so that the statements are clearly defined and there is no confusion in the interpreter's part.

Let's try that and see how it goes. =)

Monday, October 19, 2015 8:05 PM
• I can't see how you code segment can divide by 0.

I suggest checking all other divisions in you code, perhaps by putting some TextWindow.WriteLine debugging in.

If you still can't find the problem then:

Simplify the code and simplify again while still seeing the error and finally if still an issue post full code and instructions i.e. what data entry and button presses etc to create error - by this stage (after simplfying code) is should be quick and easy to reproduce and therefore for us to help better - but I expect you will have found it by then...

Monday, October 19, 2015 9:07 PM
• OK, perhaps if MathsAnswer[1] is undefined it could happen - see this test:

```a = ""
If (a <> 0) Then
b = 1/a
Else
b = 0
EndIf
TextWindow.WriteLine(b) ```

Undefined is not 0, but when processed in a math statement will be evaluated to 0 and hence division by 0.

Or even if the divisor is not a number

a = "d" etc

If this is the issue then this check should work (the second test checks that a is a number)

If (a <> 0 And a+0 = a) Then

And a shorter test might be (this woks because a*1 is forced to be evaluated to a number as part of the test).

If (a*1 <> 0) Then

Monday, October 19, 2015 9:12 PM

### All replies

• I think maybe the statement:

If MathsAnswer[1] <> 0 Then

is not formatted correctly.

what if you formatted it as:

If MathsAnswer[1] < 0 Then

If MathsAnswer[1] > 0 Then

so that the statements are clearly defined and there is no confusion in the interpreter's part.

Let's try that and see how it goes. =)

Monday, October 19, 2015 8:05 PM
• I can't see how you code segment can divide by 0.

I suggest checking all other divisions in you code, perhaps by putting some TextWindow.WriteLine debugging in.

If you still can't find the problem then:

Simplify the code and simplify again while still seeing the error and finally if still an issue post full code and instructions i.e. what data entry and button presses etc to create error - by this stage (after simplfying code) is should be quick and easy to reproduce and therefore for us to help better - but I expect you will have found it by then...

Monday, October 19, 2015 9:07 PM
• OK, perhaps if MathsAnswer[1] is undefined it could happen - see this test:

```a = ""
If (a <> 0) Then
b = 1/a
Else
b = 0
EndIf
TextWindow.WriteLine(b) ```

Undefined is not 0, but when processed in a math statement will be evaluated to 0 and hence division by 0.

Or even if the divisor is not a number

a = "d" etc

If this is the issue then this check should work (the second test checks that a is a number)

If (a <> 0 And a+0 = a) Then

And a shorter test might be (this woks because a*1 is forced to be evaluated to a number as part of the test).

If (a*1 <> 0) Then

Monday, October 19, 2015 9:12 PM