How do different programming languages ​​handle division by 0?

Perhaps this is the wrong question asked here, but I'm curious. I know that many languages ​​will simply explode and fail when they are asked to divide by 0, but are there any programming languages ​​that can reasonably handle this impossible amount - and if so, what do they do? Do they continue to process, process 350/0 as 350, or stop execution, or what?

+3
source share
9 answers

From Wikipedia :

IEEE, , 1, 1.5 .. , ( , ), . .

0

Java Double.POSITIVE_INFINITY Double.NEGATIVE_INFINITY ( ), IEEE. - undefined, ArithmeticException, " ".

+4
+2

Java Double.POSITIVE_INFINITY Double.NEGATIVE_INFINITY.

+1

, - 350, 350/0. , Java , . C/++ ( , , ).

0

Delphi ( 0 const), , .

C ++.

PHP :

: < file.php > X

, PHP, - :

$i = 123 / 0;

$i . $i === NULL, isset ($ i) true, is_string ($ i) false.

0

Python (at least version 2, I don't have 3) throws a ZeroDivisionError that can be caught.

num = 42
try:
    for divisor in (1,0):
        ans = num / divisor
        print ans
except ZeroDivisionError:
    print "Trying to divide by 0!"

produces:

42
Trying to divide by 0!
0
source

Floating-point numbers according to IEEE define NaN constants, etc. Any continuous operation including the thst value will remain unchanged until the end. Integers or integers differ when exceptions are thrown ... In java ...

0
source

Source: https://habr.com/ru/post/1735632/


All Articles