Division by Zero: B+ just crashes (even in debug)

Archives Forums/BlitzPlus Bug Reports/Division by Zero: B+ just crashes (even in debug)

Mimi(Posted 2015) [#1]
Why does Blitz+ just crash (even in debug mode) in case of a division by zero. Can this error not be caught? Shouldn't there be an error message in debug mode and the program stop before the division?

Sample Code:
a = 0
b = 3 / a ; --> crashes without error message


indietom(Posted 2016) [#2]
Why are you dividing by zero? It's mathematically impossible.


Mimi(Posted 2016) [#3]
Of course not on purpose like in the above example, but because my code is quite complex and in the specific situation I may not aware that the denominator is zero.


Guy Fawkes(Posted 2016) [#4]
Please stop complaining & make a function for it.




LineOf7s(Posted 2016) [#5]
What an extraordinary function.


Zethrax(Posted 2016) [#6]
Wierd. Blitz3D and BlitzMax both pop an error dialog, but BlitzPlus just results in a "This program has stopped working" OS message in debug mode.

Seems like a bug in the debugger.

-

For general use you should obviously sanitize any "division by' values that don't have a known constant range (user input, etc).


RemiD(Posted 2016) [#7]
@LineOf7s>>Notice how the indentation makes sense and add clarity to the code...


Floyd(Posted 2016) [#8]
As long as we are discussing a six month old topic, keep in mind that BlitzPlus is no longer a commercial product. Nothing "official" is going to happen.

A glance at github shows everything still two years old, so nothing is happening with the code, at least not publicly. I suppose somebody could have a private fork of BlitzPlus.

If you want something like "divide by zero not detected" to be fixed you probably have to do it yourself.


Guy Fawkes(Posted 2016) [#9]
Ok, you know what? You can SHOVE it Remi! We ALL have our own styles of coding! I don't question yours, don't question mine, ass!


RemiD(Posted 2016) [#10]

We ALL have our own styles of coding!


@GF>>I agree with you, but in your code, the indentation of the 3 lines in the middle is weird and, to me, senseless (they could all start at the same row)
(but nothing important, disregard my previous post...)


xlsior(Posted 2016) [#11]
FWIW: The function listed above has no solution for when A# and B# are equal


Guy Fawkes(Posted 2016) [#12]
Good. at least you understand that. I retract my last message, @RemiD.


RGR(Posted 2016) [#13]
Just for fun ;-)
Well GF
Don't get crazy again ... your Function has so many mistakes it's awful

First: all your results are integers - so why do you use num_A# and num_B# ?
You give advice "Please stop complaining & make a function for it." And then provide a Function you have never tried ... otherwise you would have noticed how wrong the results are!

num_A can be 0 because you can devide 0 by any number not 0 (result is always 0) that makes If ( num_A# = 0 Or num_B# = 0 ) Then Return in your function wrong

the rest is complete false thinking. you cannot just swap a and b and expect to have a right result.

num_A / num_B is not the same as num_B / num_A
it has nothing to do which of the numbers is bigger or smaller ...
You just want to divide

Line 4 and Line 5 will never be reached - the function (in case it returns anything) is left after Line 2 or Line 3 anyway ...

Finally: You forgot what happens if both numbers are the same - your function provides 0 but it must be 1.0

Graphics 640, 580, 0, 2

Print "5.0 / 0.0 Not allowed"
Print "changed GF.function to give it a little bit of sense"
Print "GF_function now: "+check_Num(5.0, 0.0)
Print "Should return 4.65661e-010 as error code catching division by 0"
Print "GF_function original: "+GF_check_Num(5.0, 0.0)
Print "CheckZero_Div: "+CheckZero_Div(5.0, 0.0)
Print "------------"

Print "0.0 / 5.0"
Print 0.0 / 5.0
Print "GF_function: "+check_Num(0.0, 5.0)
Print "Should be 0.0 - shows Infinity because GF swaps number positions "
Print "GF_function original: "+GF_check_Num(0.0, 5.0)
Print "CheckZero_Div: "+CheckZero_Div(0.0, 5.0)
Print "------------"

Print "2.0 / 5.0"
Print 2.0 / 5.0
Print "GF_function: "+check_Num(2.0, 5.0)
Print "Should be 0.4"
Print "GF_function original: "+GF_check_Num(2.0, 5.0)
Print "CheckZero_Div: "+CheckZero_Div(2.0, 5.0)
Print "------------"

Print "5.0 / 2.0"
Print 5.0 / 2.0
Print "GF_function: "+check_Num(5.0, 2.0)
Print "Should be 2.5"
Print "GF_function original: "+GF_check_Num(5.0, 2.0)
Print "CheckZero_Div: "+CheckZero_Div(5.0, 2.0)
Print "------------"

Print "5.0 / 5.0"
Print 5.0 / 5.0
Print "GF_function: "+check_Num(5.0, 5.0)
Print "Should be 1.0"
Print "GF_function original: "+GF_check_Num(5.0, 5.0)
Print "CheckZero_Div: "+CheckZero_Div(5.0, 5.0)
Print "------------"

WaitKey()
End

; GuyFawkes Function - corrected missing # in Function name  
Function Check_Num# ( num_A#, num_B# )
    If ( num_B# = 0 ) Then Print "Line 1 - returns 4.65661e-010 as error code ": Return 1.0 / 9999999999999999999999999999999 ; just to show anything
    If ( num_A# < num_B# ) Then Print "Line 2 - " : Return ( num_B# / num_A# )
    If ( num_B# < num_A# ) Then Print "Line 3 - " : Return ( num_A# / num_B# )
    If ( num_A# > num_B# ) Then Print "Line 4 - " : Return ( num_A# / num_B# )
    If ( num_B# > num_A# ) Then Print "Line 5 - " : Return ( num_B# / num_A# )
End Function


Function GF_Check_Num ( num_A#, num_B# )

    If ( num_A# = 0 Or num_B# = 0 ) Then Return

        If ( num_A# < num_B# ) Then Return ( num_B# / num_A# )

            If ( num_B# < num_A# ) Then Return ( num_A# / num_B# )

        If ( num_A# > num_B# ) Then Return ( num_A# / num_B# )

    If ( num_B# > num_A# ) Then Return ( num_B# / num_A# )

End Function

Function CheckZero_Div#(a#,b#)
	If (b# = 0.0)
		Return 999999.0*10^32 ; return a very high number
	Else
		Return (a# / b#)
	EndIf
End Function


@Mimi
You always check Variables in your code for 0.0 before you make divisions and your variable can be 0
Or in some cases catch infinity with a very high number

Function CheckZero_Div#(a#,b#)
	If (b# = 0.0)
		Return 999999.0*10^32 ; return a very high number
	Else
		Return (a# / b#)
	EndIf
End Function



RemiD(Posted 2016) [#14]
@Me>>Repeat to self 10 times : "i have full control of my mind/body, and i will not troll"

I totally resisted the urge. ah ! :P


RGR(Posted 2016) [#15]
@RemiD : let it all out, do not resist any urge ;-)


_PJ_(Posted 2016) [#16]
Function Divide#(a#,b#)
If (b) Then Return a/b
 RunTimeError("Divide By Zero "+str(a))
End Function