Daestrum Expert Licensed User Longtime User Feb 9, 2024 #1 I may be missing something, but it appears dividing by zero no longer generates an Exception. Code: Copy 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.
I may be missing something, but it appears dividing by zero no longer generates an Exception. Code: Copy 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 Feb 9, 2024 #2 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: Java division by zero doesn't throw an ArithmeticException - why? Why doesn't this code throw an ArithmeticException? Take a look: public class NewClass { public static void main(String[] args) { // TODO code application logic here double tab... stackoverflow.com Update: B4i also casts the denominator as a double Code: Copy //BA.debugLineNum = 393223;BA.debugLine="log (c / 0)"; [self->___c LogImpl:@"4393223" :[self.bi NumberToString:@(_c/(double)0)] :0]; Last edited: Feb 9, 2024
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: Java division by zero doesn't throw an ArithmeticException - why? Why doesn't this code throw an ArithmeticException? Take a look: public class NewClass { public static void main(String[] args) { // TODO code application logic here double tab... stackoverflow.com Update: B4i also casts the denominator as a double Code: Copy //BA.debugLineNum = 393223;BA.debugLine="log (c / 0)"; [self->___c LogImpl:@"4393223" :[self.bi NumberToString:@(_c/(double)0)] :0];
E emexes Expert Licensed User Longtime User Feb 9, 2024 #3 Daestrum said: Code: Copy 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 Click to expand... Change c from Int to Double for a clue as to what's happening.
Daestrum said: Code: Copy 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 Click to expand... Change c from Int to Double for a clue as to what's happening.
OliverA Expert Licensed User Longtime User Feb 9, 2024 #4 Daestrum said: I may be missing something, but it appears dividing by zero no longer generates an Exception Click to expand... 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.
Daestrum said: I may be missing something, but it appears dividing by zero no longer generates an Exception Click to expand... 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 Feb 9, 2024 #5 OliverA said: I guess I'm remembering wrong. Click to expand... 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.
OliverA said: I guess I'm remembering wrong. Click to expand... 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 Feb 9, 2024 #6 emexes said: Change c from Int to Double for a clue as to what's happening. Click to expand... 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) Code: Copy #if java public static Integer test1(){ return 1/0; } #End If Causes Caused by: java.lang.ArithmeticException: / by zero Click to expand... But if b4? is casting them to float etc , that explains why there is no error. Last edited: Feb 9, 2024
emexes said: Change c from Int to Double for a clue as to what's happening. Click to expand... 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) Code: Copy #if java public static Integer test1(){ return 1/0; } #End If Causes Caused by: java.lang.ArithmeticException: / by zero Click to expand... But if b4? is casting them to float etc , that explains why there is no error.
E emexes Expert Licensed User Longtime User Feb 9, 2024 #7 Daestrum said: I know floats/doubles use POSITIVE/NEGATIVE_INFINITY on division by 0, but as far as I was aware Integers didn't. Click to expand... 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. ?
Daestrum said: I know floats/doubles use POSITIVE/NEGATIVE_INFINITY on division by 0, but as far as I was aware Integers didn't. Click to expand... 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. ?