1

Javascript

0/0       // NaN
0/0.0     // NaN
1/0.0     // Infinity
1/0.0     // Infinity

Ruby

>> 0/0     # ZeroDivisionError: divided by 0
>> 0/0.00  # NaN
>> 1/0.00  # Infinity
>> -1/0.00 # -Infinity

Python

>>> 0/0    # ZeroDivisionError: integer division or modulo by zero
>>> 0/0.0  # ZeroDivisionError: integer division or modulo by zero
>>> 1/0.0  # ZeroDivisionError: integer division or modulo by zero
>>> -1/0.0 # ZeroDivisionError: integer division or modulo by zero

What's the reason behind

Integers cannot be divided by zero, but float can be?

And in python, why it's not a NaN or Infinity, but all errors?

allenhwkim
  • 27,270
  • 18
  • 89
  • 122
  • 12
    Different languages, different choices. – Martijn Pieters Feb 21 '13 at 23:10
  • 1
    Why does the answer matter? As long as you know how the language your using works that should be enough shouldn't it? – Ash Burlaczenko Feb 21 '13 at 23:14
  • 7
    All numbers in JavaScript are IEEE floats, hence *0 is the same as 0.0 in JavaScript*. Other languages may have a distinction between integers (that generally cannot represent NaN) and floats (which usually can). –  Feb 21 '13 at 23:14
  • why the need to divide by 0 anyways?? – 2potatocakes Feb 21 '13 at 23:14
  • You can conclude that ZeroDivisionError from Python takes on different meanings in other languages. Try to define in Python your custom exceptions like in http://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python – Mihai8 Feb 21 '13 at 23:16
  • In Python, you can just say `x/y if y else float('inf')` if you want that behaviour – John La Rooy Feb 21 '13 at 23:18
  • possible duplicate of [How to get NaN when I divide by zero](http://stackoverflow.com/questions/10011707/how-to-get-nan-when-i-divide-by-zero) – Makoto Feb 21 '13 at 23:27

3 Answers3

3

Floating point divide by zero can be an exception in many environments (if enabled) but since IEEE float has the ability to express some exceptional values (like Inf, Nan) it is also possible for division by zero to return an exceptional value, which integer operations cannot.

You are also observing different parsing (or promotion?) in different languages. However, a bare 0 in JavaScript is a floating point 0 rather than an integer zero.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
  • does that mean python does not follow IEEE 754 standard? – allenhwkim Feb 21 '13 at 23:21
  • @bighostkim Ask a question about Python and floating point division by 0.0: focus, focus, focus. (But better, search first - this is not a new question.) –  Feb 21 '13 at 23:21
  • 1
    The IEEE-754 standards allows returning special values or raising exceptions. Python chooses to use an exception for division by 0. The Decimal (included with Python) and gmpy2 (available separately) libraries uses contexts to allow the programmer to control when exceptions are raised. – casevh Feb 21 '13 at 23:31
2

Different languages make different choices.

The case of Javascript is easy: integers are stored as floats and has chosen to follow IEEE-754 rules on division.

Python apparently forbids division by 0 or by 0.0. See ways around this in this SO question.

Ruby makes the distinction between an exact 0 and a float like 0.0 (or -0.0). Indeed, 0.0 could stand for a very small positive number that can't be expressed within the precision of a floating point number (e.g. 0.1e-400). It is in this view that 1/0.1e-400 returns Float::INFINITY, since 1e401 is too big to be represented with any other float.

Ruby actually has two different comparison operators to make that distinction:

0 == 0.0 # => true
0.eql?(0.0) # => false

This can have some important consequences:

h = {0 => :foo}
h[0.0] # => nil
Community
  • 1
  • 1
Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
  • [zero](http://en.wikipedia.org/wiki/IEEE_754-1985#Zero) - aka 0.0 (and -0.0) - has an exact IEEE float representation .. –  Feb 21 '13 at 23:25
  • Also, the argument about the ruby equality just goes to show that `0` (fixnum) is not the same as `0.0` (Float) but does not say that they are (or are not) not numerically equal: they are. –  Feb 21 '13 at 23:27
  • @pst: `0.0` has a representation, but `0.1e-400` doesn't. As for equality, they are `==` but not `eql?`, which can be important. Answer updated. – Marc-André Lafortune Feb 21 '13 at 23:35
1

And in python, why it's not a NaN or Infinity, but all errors?

Certain modules in python do have this, when they would be appropriate:

In [1]: import numpy as np

In [2]: a = np.arange(3)

In [3]: b = np.ones(3)

In [4]: b/a
Out[4]: array([ inf,  1. ,  0.5])
askewchan
  • 45,161
  • 17
  • 118
  • 134