0

I ran into this question in Math Stack Exchnage which is regarding division of numbers by 0.

Now, regarding x/0, the accepted answer states that:

Since there are no solutions to the equation, there is no such thing as "x/0". So x/0 does not represent any number.

So, implementing this in C#, if we divide a number(double) by 0.0, it should result as NaN. However, it shows infinity.

On the other hand, for the operation 0.0 / 0.0, it's written that:

the problem here is that the equation 0z=0 has any value of z as a solution (that's what the "infinite solutions" means).

However, doing this in C#, it results as NaN.

Why is this behaviour opposite in C#? Did they make a mistake while implementing it?

Here is a working fiddle to demonstrate that.

Community
  • 1
  • 1
Dumbledore
  • 450
  • 1
  • 5
  • 20
  • 5
    "it should result as NaN" --- what this statement is based on? C# behaviour pretty match follows the math. – zerkms Mar 25 '15 at 19:26
  • Check out this answer (different question): http://stackoverflow.com/a/14244726/2359643 or this one http://stackoverflow.com/a/15117594/2359643 – JNYRanger Mar 25 '15 at 19:28
  • 7
    C# is just following the IEEE specs. – Servy Mar 25 '15 at 19:28
  • This is why for any programming environment, if I'm doing any kind of division, I make sure that there's no possible way I'm dividing by zero, so I don't have to deal with NaN, or infinity, or an exception, or however a language chooses to describe it. – Joe Enos Mar 25 '15 at 19:30
  • @zerkms It's based on mathematical interpretation of the two scenarios? – Dumbledore Mar 25 '15 at 19:35
  • @JNYRanger No, my question is different. I'm concerned with why is the behaviour opposite to the mathematical explanation of the two scenarios. – Dumbledore Mar 25 '15 at 19:37
  • 1
    @Dumbledore on "arithmetic" interpretation. "Math" is a broad field. And still, "does not represent any number" still can be an infinity. Infinity is not a number. – zerkms Mar 25 '15 at 19:38
  • @zerkms Fully agreed. Then why are `NaN` and `infinity` different in C#? Why didn't they just use `infinity`? – Dumbledore Mar 25 '15 at 19:40
  • Just nitpicking: Infinity and NaN are not opposite. PositiveInfinity and NegativeInfinity are opposite. Only dividing 0 by 0 will give NaN. Dividing any other number by zero will give one of the infinities. – Dennis_E Mar 25 '15 at 19:41
  • @Dumbledore because `0/0` is *uncertainty*. It can be any value - 0, any number, infinity; depending on the order of the 0. – zerkms Mar 25 '15 at 19:42
  • @Dumbledore There are all sorts of values that aren't numbers. Positive/negative infinity are just two types of values that aren't numbers. Rather than wrapping up every single value that isn't a number into one NaN value, different, more specific, types of non-numeric values exist. You can use `double.IsNaN` to determine if a value is any type of non-numeric value. – Servy Mar 25 '15 at 19:43
  • @Dumbledore it's called "uncertainty", it's an official calculus term. – zerkms Mar 25 '15 at 19:46
  • @zerkms Sure, any number. So, what's the point of using `NaN` if it's still a number? It's `x/0` that's undefined or that has absolutely no possible solution. Shouldn't that be NaN? – Dumbledore Mar 25 '15 at 19:48
  • @Servy Yes, I agree. And yes, you're right we can use `Double.IsNaN` to determine non-numeric values. However, I was just comparing it to basic math and wondering why is it different? – Dumbledore Mar 25 '15 at 19:51
  • @Dumbledore It's not different. In abstract math there are all sorts of values that aren't numbers, and positive/negative infinity are two specific values that aren't numbers. This models that. Of course not all specific non-numeric values have their own `double` value, so those that weren't bothered to be specifically represented were wrapped in the bucket of `NaN` to just be "all other non-numeric values". It's not a super detailed model, but it's certainly not a fundamentally different model. – Servy Mar 25 '15 at 19:54
  • @Dumbledore: it's not a number, it's an "uncertainty". It's the [*limit*](http://en.wikipedia.org/wiki/Limit_(mathematics)) operation that returns the value for the uncertainty. You want to say "it's a number" - fine, apply *limit* to it first. – zerkms Mar 25 '15 at 19:56
  • @Servy Yes, that makes perfect sense. Thanks for explaining it. – Dumbledore Mar 25 '15 at 19:59
  • @zerkms I don't think you can apply limit to `0/0` as there is no variable here. Anyways, you mention that it's "uncertainity". I totally agree to that. So, actually there is an _infinite_ _set_ _of_ _numbers_, not just one one number. That's why it's `NaN`. Thanks for the discussion. – Dumbledore Mar 25 '15 at 20:04
  • @Dumbledore: here I used `0/0` to represent the type of uncertainty, not as the actual expression. – zerkms Mar 25 '15 at 20:04
  • @Servy If you post that as an answer, I'll accept it. – Dumbledore Mar 25 '15 at 20:05

5 Answers5

2

Actually, by "infinite solutions", they mean any given number is a solution to the equation, and as such, the solution is not applicable.

However, when you divide a nonzero constant by zero, the answer does, in fact, go to (±) infinity. I'm too lazy to explain the calculus, so take a look at this: www.wolframalpha.com/input/?i=lim+1/x+as+x+approaches+0

Evan Geng
  • 81
  • 2
2

There are all sorts of values that aren't numbers, and positive/negative infinity are two specific values that aren't numbers. (Another would be say the square root of negative one.) This models that.

Of course not all specific non-numeric values have their own double value, so those that weren't bothered to be specifically represented were wrapped in the bucket labeled NaN to just be "all other non-numeric values". It's not a super detailed model as there aren't all that many specific non-numeric values listed, but it's certainly not a fundamentally different model.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • `double.IsNaN` only seems to check specifically for `double.NaN`. E.g. `double.IsNaN( double.PositiveInfinity )` will return `false`. Likewise for `double.NegativeInfinity`. If you want to check for non-real values, you'll have to do something like `!double.IsInfinity( x ) && !double.IsNaN( x )` – Kyle Mar 25 '15 at 20:13
1

The question you're referring to has tags algebra-precalculus, arithmetic.

Whereas there is calculus section of the math that defines the numbers behaviour that C# implements.

So that answer is correct for the field it was asked within.

zerkms
  • 249,484
  • 69
  • 436
  • 539
0

Well, if we're talking about real numbers, then the expression 'x/0' has no real solution. Now, mathematically we could move to a different set of numbers, like the real projective line, in which division by 0 is defined.

So really what you've discovered here is that the set of numbers represented by a double (or most any floating point values) is not the set of real numbers. So the usual rules about the real numbers don't really apply in such a straight-forward way. For that reason it's perfectly acceptable to define division in this way. Even mathematically.

Kyle
  • 6,500
  • 2
  • 31
  • 41
0

From: Gnu

IEEE 754 floating point numbers can represent positive or negative infinity, and NaN (not a number). These three values arise from calculations whose result is undefined or cannot be represented accurately. You can also deliberately set a floating-point variable to any of them, which is sometimes useful. Some examples of calculations that produce infinity or NaN:

1/0 = ∞
log (0) = -∞
sqrt (-1) = NaN

When a calculation produces any of these values, an exception also occurs; see FP Exceptions.

The basic operations and math functions all accept infinity and NaN and produce sensible output. Infinities propagate through calculations as one would expect: for example, 2 + ∞ = ∞, 4/∞ = 0, atan (∞) = π/2. NaN, on the other hand, infects any calculation that involves it. Unless the calculation would produce the same result no matter what real value replaced NaN, the result is NaN.

In comparison operations, positive infinity is larger than all values except itself and NaN, and negative infinity is smaller than all values except itself and NaN. NaN is unordered: it is not equal to, greater than, or less than anything, including itself. x == x is false if the value of x is NaN. You can use this to test whether a value is NaN or not, but the recommended way to test for NaN is with the isnan function (see Floating Point Classes). In addition, <, >, <=, and >= will raise an exception when applied to NaNs.

math.h defines macros that allow you to explicitly set a variable to infinity or NaN.

Macro: float INFINITY An expression representing positive infinity. It is equal to the value produced by mathematical operations like 1.0 / 0.0. -INFINITY represents negative infinity.

You can test whether a floating-point value is infinite by comparing it to this macro. However, this is not recommended; you should use the isfinite macro instead. See Floating Point Classes.

This macro was introduced in the ISO C99 standard.

Macro: float NAN An expression representing a value which is “not a number”. This macro is a GNU extension, available only on machines that support the “not a number” value—that is to say, on all machines that support IEEE floating point.

You can use ‘#ifdef NAN’ to test whether the machine supports NaN. (Of course, you must arrange for GNU extensions to be visible, such as by defining _GNU_SOURCE, and then you must include math.h.)

IEEE 754 also allows for another unusual value: negative zero. This value is produced when you divide a positive number by negative infinity, or when a negative result is smaller than the limits of representation.

Eloy
  • 13
  • 1