问题
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. ;)
回答1:
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.
回答2:
Dividing by infinity is fine - it's zero.
Unless you're dividing infinity by infinity, which is undefined (in general at least)
回答3:
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.
来源:https://stackoverflow.com/questions/1613988/dividing-by-infinity