by PopeDotNinja · 8y ago · 0 comments
>>> 1 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> 0 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> 1 / 0.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float division by zero
>>> 0.0 / 0.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float division by zero
Meanwhile, in Elixir v1.7.1... iex(1)> 1 / 0
** (ArithmeticError) bad argument in arithmetic expression: 1 / 0
:erlang./(1, 0)
iex(1)> 0 / 0
** (ArithmeticError) bad argument in arithmetic expression: 0 / 0
:erlang./(0, 0)
iex(1)> 1.0 / 0.0
** (ArithmeticError) bad argument in arithmetic expression: 1.0 / 0.0
:erlang./(1.0, 0.0)
iex(1)> 0.0 / 0.0
** (ArithmeticError) bad argument in arithmetic expression: 0.0 / 0.0
:erlang./(0.0, 0.0)
Meanwhile, in Ruby v2.5.1... irb(main):001:0> 1 / 0
Traceback (most recent call last):
3: from /Users/mpope/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):1
1: from (irb):1:in `/'
ZeroDivisionError (divided by 0)
irb(main):002:0> 0 / 0
Traceback (most recent call last):
3: from /Users/mpope/.rbenv/versions/2.5.1/bin/irb:11:in `<main>'
2: from (irb):2
1: from (irb):2:in `/'
ZeroDivisionError (divided by 0)
irb(main):003:0> 1 / 0.0
=> Infinity
irb(main):004:0> 0.0 / 0.0
=> NaN
Meanwhile, in PHP v7.0.8... > echo 1 / 0;
>
INF
Division by zero :1
> echo 0 / 0;
>
NAN
Division by zero :1
> echo 1 / 0.0;
>
INF
Division by zero :1
> echo 0.0 / 0.0;
>
NAN
Division by zero :1
Meanwhile, in Node v10.7.0... Infinity
> 1 / 0
Infinity
> 0 / 0
NaN
> 1 / 0.0
Infinity
> 0 / 0.0
NaN In [1]: 255 + 1 is 256
Out[1]: True
In [2]: 256 + 1 is 257
Out[2]: False
Every language has its inconsistencies, when it comes to math, because they run on real-world computers. All these are "slow" interpreted languages that allow runtime failures and don't use plain machine integers. Just a different trade-off.Anyway, the Inf/NaN trick is not better than the 0 trick in my opinion, because all subsequent operations result happily in NaN. I've never seen in any NaN language a program that checks for NaN the result of each and every division.
with a NaN-kind of language, it would be:
a = b/c
if a == NaN:
print "oops"
with a 0-trick language: a = b/c
if a == 0 and c == 0:
print "oops"
not THAT different, IMO :)