# 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 = "(Floored)"
MathsRoundedNames = "(Ceilinged)"
EndIf
Else
EndIf
If UsersRounding[User] = 2 Then
ElseIf UsersRounding[User] = 3 then
Else
EndIf
MathsRounded = UsersRounding[User]
Else
MathsRounded = 1
EndIf
Sound.PlayChime()
TextWindow.WriteLine("a = " + MathsAnswer + " " + MathsRoundedNames[MathsRounded])
EndSub

(This is not the whole thing)

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

FYI, the textboxes go:

                               

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

Monday, October 19, 2015 3:31 PM

• I think maybe the statement:

is not formatted correctly.

what if you formatted it as:

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 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:

is not formatted correctly.

what if you formatted it as:

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