5

Perhaps this is the wrong sort of question to ask here but I am curious. I know that many languages will simply explode and fail when asked to divide by 0, but are there any programming languages that can intelligently handle this impossible sum - and if so, what do they do? Do they keep processing, treating 350/0 as 350, or stop execution, or what?

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
Jack
  • 9,615
  • 18
  • 72
  • 112
  • 1
    Are you asking about languages or language implementations? Not all languages prescribe the exact behavior, and therefore it varies by implementation. – David Thornley Mar 05 '10 at 18:16
  • Asking about how each different language may treat the scenario. Some fascinating answers I've been getting, just curious as to what each language does. – Jack Mar 05 '10 at 19:29
  • I’m voting to close this question because while it is a fun question, it's not a great fit for Stack Overflow. The [help center](https://stackoverflow.com/help/dont-ask) states, "You should only ask practical, answerable questions based on actual problems that you face. Chatty, open-ended questions diminish the usefulness of our site and push other questions off the front page." This feels like it falls squarely in this category. – M. Justin Oct 07 '21 at 21:51

11 Answers11

4

The little-known Java programming language gives the special constant Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY (depending on the numerator) when you divide by zero in an IEEE floating-point context. Integer division by zero is undefined, and results in an ArithmeticException being thrown, which is quite different from your scenario of "explosion and failure".

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
  • 2
    How do you double infinity? Doesn't that just give you infinity? Moronic minds want to know! – T.E.D. Mar 05 '10 at 18:41
  • Also, if you double not-a-number, does that make it a number? – Jonathan Feinberg Mar 05 '10 at 18:48
  • 2
    @T.E.D Actually, some infinities are bigger than others because of the rate at which they are expanding, eg. SUM(1..n) is a smaller infinity than SUM(1^2...n^2), as n approaches infinity (it's to do with limits in mathematics) – Tony Breyal Jul 13 '10 at 18:32
3

The INTERCAL standard library returns #0 on divide by zero

msw
  • 42,753
  • 9
  • 87
  • 112
1

In Java, division by zero in a floating-point context produces the special value Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY.

mob
  • 117,087
  • 18
  • 149
  • 283
1

From Wikipedia:

The infinities of the extended real number line can be represented in IEEE floating point datatypes, just like ordinary floating point values like 1, 1.5 etc. They are not error values in any way, though they are often (but not always, as it depends on the rounding) used as replacement values when there is an overflow. Upon a divide by zero exception, a positive or negative infinity is returned as an exact result.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
0

i'd be surprised if any language returns 350 if you do 350/0. Just two examples, but Java throws an Exception that can be caught. C/C++ just crashes (i think it throws a Signal that can probably be caught).

mlathe
  • 2,375
  • 1
  • 23
  • 42
  • 1
    There are two distinct languages called C and C++, and I don't think either of them has any specific provision in their standards. Some implementations of one or both languages may well throw signals. – David Thornley Mar 05 '10 at 18:14
0

In Delphi, it either throw a compile-time error (if divided by a 0 value const) or a catchable runtime error if it happens at runtime.

It's the same for C and C++.

In PHP you will get a warning:

Warning: Division by zero in <file.php> on line X

So, in PHP, for something like:

$i = 123 / 0;

$i will be set to nothing. BUT $i is not === NULL and isset($i) returns true and is_string($i) returns false.

AlexV
  • 22,658
  • 18
  • 85
  • 122
0

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

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

prints out:

42
Trying to divide by 0!
GreenMatt
  • 18,244
  • 7
  • 53
  • 79
0

Floating point numbers as per the IEEE define constants NaN etc. Any continued operation involving thst value will remain unchanged until the end. Integer or whole numbers are different with exceptions being thrown...In java...

mP.
  • 18,002
  • 10
  • 71
  • 105
0

In pony division by 0 is 0 but i have yet to find a language where 0/0 is 1

meiser
  • 31
  • 3
-2

I'm working with polyhedra and trying to choose a language that likes inf. The total edges for a polyhedron {a,b} where a is edges per polygon and b is edges per corner is E = 1/(1/a + 1/b - 1/2)

if E is negative it's a negative curvature, but if E is infinity (1/0) it tiles the plane. Examples: {3,6} {4,4}

  • 1
    Could you please give us a hint how this answers the OP's question? – Binarus Sep 21 '18 at 16:54
  • It's an example of a use of infinity. I guess I commented on the wrong thread. – Robert Hayes Sep 21 '18 at 18:24
  • @RobertHayes Welcome to SO! This is a Q&A library, not a forum. There are no threads and answers aren't comments. You might want to take the [tour](https://stackoverflow.com/tour) and read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Max Vollmer Sep 22 '18 at 08:20