Bug? [Solved 'ish]Division by Zero

Daestrum

Expert
Licensed User
Longtime User
I may be missing something, but it appears dividing by zero no longer generates an Exception.

B4X:
Dim a As Int = 99
Dim b As Int = 0
Dim c As Int = a/b

Log(c)       '<== 2147483647 should raise Exception or at least warn of division by zero

It makes it virtually impossible to track down a bug where a value is not set and left at 0 and used in a divide.
 

OliverA

Expert
Licensed User
Longtime User
It looks like this ties tangentially to this thread: https://www.b4x.com/android/forum/t...on-instead-of-floating-point-division.158469/

Under the hood, B4X (at least B4J and B4A that I know of), does a float division and then casts the result to an Int. Floating division by 0 does not throw an exception in Java. It results as the largest or smallest possible value for the given type (depending on the positive or negative result of the division). As the link below shows, that's the proper implementation of the IEEE 754 spec (IEEE Standard for Floating-Point Arithmetic)

Link:

Update: B4i also casts the denominator as a double
B4X:
 //BA.debugLineNum = 393223;BA.debugLine="log (c / 0)";
[self->___c LogImpl:@"4393223" :[self.bi NumberToString:@(_c/(double)0)] :0];
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
I may be missing something, but it appears dividing by zero no longer generates an Exception
BTW, I've just re-installed B4J version 8.50 (that's the oldest one I have a copy of), it it produces the same result. For some reason, I thought I had B4J bomb out on division by 0, since I would use that to, on purpose, create a program fault. I guess I'm remembering wrong.
 

OliverA

Expert
Licensed User
Longtime User
I guess I'm remembering wrong.
I am remembering wrong. I just looked at that project and noticed that division by 0 did NOT work (it did not bomb the program), so I resorted to defining a list and adding an element BEFORE initializing the list. I guess I just never gave it a second thought that division by 0 did not bomb.
 

Daestrum

Expert
Licensed User
Longtime User
Change c from Int to Double for a clue as to what's happening.
I know floats/doubles use POSITIVE/NEGATIVE_INFINITY on division by 0, but as far as I was aware Integers didn't. (Although they do have MAX_VALUE)
B4X:
#if java
public static Integer test1(){
    return 1/0;
}
#End If
Causes
Caused by: java.lang.ArithmeticException: / by zero

But if b4? is casting them to float etc , that explains why there is no error.
 
Last edited:

emexes

Expert
Licensed User
I know floats/doubles use POSITIVE/NEGATIVE_INFINITY on division by 0, but as far as I was aware Integers didn't.

I was a bit surprised too when I discovered that B4J followed Java via a scenic route when dividing integers. Was no worries though, because with production code I never rarely do a division without checking for zero first anyway. 😇

But I admit that I may have had some longer-than-necessary debugging sessions with private code. 🤣
 
Top