Bug? or Wish? Divide by zero

wonder

Expert
Licensed User
Longtime User
main-qimg-8b45cc0b27ee2a6ee5ce44489041c0fc
 

cimperia

Active Member
Licensed User
Longtime User
Unlike many (all?) languages, B4A returns 2147483647 if you divide a number by zero, instead of raise an exception.

Are you using floats or doubles? In that case, Java does not raise an arithmetic exception. However if you're using integers, then an exception should be raised.

If you're using non integers then you can check the result against these Java Values instead of hard-coding them.

B4X:
Double.POSITIVE_INFINITY
Double.NEGATIVE_INFINITY

IEEE 754 defines 1.0 / 0.0 as Infinity and -1.0 / 0.0 as -Infinity and 0.0 / 0.0 as NaN.
 

LucaMs

Expert
Licensed User
Longtime User
Thank you, but I just needed... an error :) (#3).

Unfortunately, of course, I can not make errors.

(not more than 100 per day).

:D



[probably I used:
Dim A As Int
Log(A / 0)

or directly

Log(1 / 0)]
 
Last edited:

cimperia

Active Member
Licensed User
Longtime User
OK, I understand where you're coming from. Unfortunately Java has implemented binary floating-point arithmetic to be compatible with IEEE 754 standards and will not raise division-by-zero exceptions when explicit or implicit non-integer values are used.

As Java does not allow operator overloading, it's not possible, as far as I know, to modify this behaviour.

C++ would allow you to overload the division operator and thus you could create your own class in which dividing by zero would generate an exception, for example.
 
Top