Android Question Divide by zero not an error?

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
It's still early for me, and I may have insufficient levels of caffeine in my body at this point, but this is making my head hurt. I'm trying to test some logging I added to the Starter service's Application_Error block. Being the clever person I am, I thought I'd just add a quick statement into an activity (not inside of any try/catch block) that would surely throw a runtime error:
B4X:
    Dim piVal3 As Int 
    piVal3 = 1 / 0
    Log("value is: " & piVal3)
I start the program up (release build, obfuscated), and this is what I see on the log output:
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (actinitialize) Create, isFirst = true **
** Activity (actinitialize) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (actinitialize) Pause, UserClosed = true **
** Activity (actlogin) Create, isFirst = true **
** Activity (actlogin) Resume **
Mqtt Connected
** Activity (actlogin) Pause, UserClosed = true **
** Activity (actmainmenu) Create, isFirst = true **
** Activity (actmainmenu) Resume **
value is: 2147483647
Should I apply for a Nobel prize in mathematics for solving "i", or am I just being exceptionally dense this morning?
 

agraham

Expert
Licensed User
Longtime User
Erel would have to give you his reason why but division operations in B4X cast the divisor to a double value which forces the expression to be evaluated as double which does not raise an exception. In your case the result is positive infinity which you see by changing the Dim to a double. When cast back to an int according to the arcane rules of Java infinity results in a max positive int value. If the result would be negative the value is negative infinity which would cast back to negative max int value.
 
Upvote 0

amorosik

Expert
Licensed User
Erel would have to give you his reason why but division operations in B4X cast the divisor to a double value which forces the expression to be evaluated as double which does not raise an exception. In your case the result is positive infinity which you see by changing the Dim to a double. When cast back to an int according to the arcane rules of Java infinity results in a max positive int value. If the result would be negative the value is negative infinity which would cast back to negative max int value.

The reason you described is clear
But regardless of the data type, if the divisor is zero, the division should return an error
If not, then there is an anomaly in the 'division function' of B4a compiler
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Depends on what you mean by 'error'. The IEE 754 standard for floating point arithemetic has specific values of infinites and NotANumbers but there is no requirement to trap or raise exceptions when they are found so Java doesn't.

The IEEE Standard for Floating-Point Arithmetic
Ok but B4X is not obliged to follow recommendations. We all, I think, are used to get an error message due to division by zero, using other languages (mainly VB*).
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Ok but B4X is not obliged to follow recommendations.
B4X does whatever Erel intended it to do but it has to follow the Java implementation which behaves as I have indicated. To do otherwise would need additional code compiled to check each and every arithmetic operation which would totally destroy performance,
 
Upvote 0
Top