views:

169

answers:

4

I'm not a mathematician, but I assume dividing by infinity is either bad math, or, at the very least, impractical.

I just spend a half hour debugging my javascript that was working perfectly fine in Firefox but was giving me an error in IE. I finally realized it was because in certain scenarios, I was asking IE to divide by infinity.

So, I fixed that, but I'm curious as to why Firefox was OK with that. Admittedly, this might be more of a mathematics question than programming. ;)

A: 

You can't divide by zero in math. It's undefined.

Firefox probably just ignored the exception.

Zachary Burt
The question was about dividing by infinity, not by zero.
ndim
Division by zero is undefined in math, but it's defined in IEEE floating-point math and therefore in JavaScript, per standard.
Jason Orendorff
+4  A: 

Dividing by infinity is fine - it's zero.

Unless you're dividing infinity by infinity, which is undefined (in general at least)

Greg
oh! So, that explains Firefox being fine with it. IE is maybe bad at math?
DA
Some worry about signs here when you distinguish between +0 and -0 (which IEEE math does)...
dmckee
A: 

Dividing a number by infinity is perfectly reasonable from a mathematical perspective. It is a number infinitely close to zero, without actually reaching zero.

From a software perspective, this is much more difficult, as infinity is not a discretely representable value. My guess would be that your difference in behavior is based on design decisions.

In Firefox, they probably chose to return a value of zero, since for all practical purposes, this result will work for what you need.

In IE, on the other hand, it appears the developers made the conscious decision to not allow you to perform a calculation that they knew they could not give a discrete answer for.

Mike Clark
Nonsense. A number does not approach anything. A number series does.
Victor
Infinity is representable in javascript - alert(Infinity)
Greg
The pedantic circumlocution is "the limit of n/epsilon as epsilon grows without bound is zero".
dmckee
Dividing a real number by infinity is not perfectly reasonable mathematically. Infinity is not a real number! The infinity symbol is used in math on real numbers in certain contexts (such as limits), but that's just notation.
Jason Orendorff
Also: this is not something browsers get to choose randomly. The ECMAScript standard specifies how numbers are to behave. http://bclary.com/2004/11/07/#a-11.5.2
Jason Orendorff
+1  A: 

I think that's a bug in IE. According to the rules of IEEE math, n/Inf=0 (for n!=0). So if IE croaks over this, it's a problem in IE. On the other hand, using Inf in ordinary mathematical operations is problematic, and you'd be better off if your code didn't rely on it in the first place.

Why is it dangerous? Well, for starters, according to IEEE 754:

1/0 = Inf
1/-0 = -Inf

but we know that 0 = -0, hence

Inf = -Inf

which is obviously undesirable. IEEE did not mean to make Inf an ordinary number anyway. Inf and NaN exist to make floating point arithmetics in computers with its inherent limited precision and overflow issues resemble ordinary-world arithmetics, where, to take the simplest example, the set of real numbers is closed under addition (adding two real numbers always results in another real number). If you do that with finite-precision floats, you'll get cases where adding two numbers results in an overflow. To avoid having to treat this as an error condition, IEEE introduced two extra symbols, Inf and NaN, and a set of rules for their behaviour. Now the result of a mathematical operation is always a number, or Inf, or NaN, and whether the result makes any sense or not is left to the user.

wallenborn