|
var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Max();
var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Min();
var isNaNSmaller = double.NaN < 1; So NaN is not the biggest value, it's still bigger than one, but it's also the smallest value.
I hate to sound infinitely negative, but that's messed up
|
|
|
|
|
NaN means Not a Number, so you cannot compare it to a proper number and get a valid response.
|
|
|
|
|
I know, so always treat it as smallest value, or always as biggest value or, better yet, throw an exception when comparing it to numbers.
These results are contradictory and just don't make any sense at all!
If this was JavaScript I'd be okay with it, but we're talking C# here. I expected better from C#
|
|
|
|
|
This has nothing to do with C#. I refer you to IEEE 754.
|
|
|
|
|
I'm glad that a lot of people have thought about how NaN should have contradictory results in different usages
<Edit>
So it seems the contradictory result are in IEEE 754, but the weird behavior in Min and Max is Microsoft[^]
</Edit>
modified 2-Oct-16 17:16pm.
|
|
|
|
|
That is true. But keep in mind that the Min and Max functions have nothing to do with C# - they are written in C#; yes, but they are part of the LINQ / the .NET Framework.
The difference in both is that Max excludes NaN, while Min will take NaN exclusively. You can see the difference here (Min)[^] and here (Max)[^].
That is different to some JS functions (such as everything in Math), which are defined by the same specification as the language.
|
|
|
|
|
"Not a number" means exactly that. So you cannot equate it to any numeric value.
|
|
|
|
|
Richard MacCutchan wrote: So you cannot equate it to any numeric value Yes you can, and that's the point.
I expected either an exception (compile or run time) or at least a predictable weird behavior (well, it's predictable once you know all the edge cases I guess).
Now whether you should is a different discussion...
I found this because I had some weird JavaScript bug by the way, casting some object to a number results in NaN and I was wondering how C# handled the case the followed.
NaN is not smaller than 1 (when comparing and when using the Min function), but when both are thrown into the Max function NaN is smaller than 1. Got it
Luckily, I've never had to work with NaN in C# because why would there even be a NaN anyway...
|
|
|
|
|
Sander Rossel wrote: why would there even be a NaN anyway...
- 0/0
- ∞/∞
- ∞+(-∞)
- √-1
- and so on...
Just because you are not familiar with the subject, does not make others wrong.
|
|
|
|
|
0/0 should throw a DivideByZeroException (which it does for integers).
And apparently 1/0 equals infinity.
Now what is it? NaN, infinity or just plain not possible?
Doesn't it sound weird (and, indeed, very wrong) that a NUMERIC type has a value "NOT A NUMBER"!?
Anyway, when I said "why would there even be a NaN anyway" I was referring to NaN in actual real life business cases that make sense and have practical use
|
|
|
|
|
I would recommend you to this article before you continue your rant about IEEE754.
|
|
|
|
|
Tried to read it once, but to me it makes as much sense as the whole NaN implementation: none whatsoever (and I admit my limited math skills are to blame).
However, I tried reading the NaN part and what they basically say is that in some edge cases you don't want computations to stop (throw exceptions) when some bogus values are inserted (e.g. divide by 0). The workaround without NaN would be to catch exceptions and simply try again. Unfortunately, every language handles exceptions differently so they standardized on NaN.
Awesome, they destroyed our numeric system to support some edge cases (correct me if I'm wrong)
Luckily .NET offers some sensible numeric types with int, long and decimal
Unfortunately, I'm currently working in JavaScript, with floating point arithmetic, where NaN is quite common, and 0.1 + 0.2 equals 0.30000000000000004 (yes, I know that's IEEE754, but that doesn't make it right)
|
|
|
|
|
Sander Rossel wrote: they destroyed our numeric system to support some edge cases
Any representation of real numbers in computer memory will destroy 'numeric system' since you're trying to represent infinite set with finite amount of memory.
Sander Rossel wrote: Luckily .NET offers some sensible numeric types with int, long and decimal
decimal comes with performance costs as it has no support in hardware so it might not be suitable as a replacement in fields that traditionally use floating point arithmetic. It is meant to be used for financial stuff since IEEE754 is not suitable for that purpose, so not everyone have to invent their own way of doing math.
Sander Rossel wrote: 0.1 + 0.2 equals 0.30000000000000004
That's the problem with converting real numbers between different bases.
|
|
|
|
|
Sander Rossel wrote: And apparently 1/0 equals infinity. No, it does not, and never has.
|
|
|
|
|
double x = 1;
double y = 0;
double z = x / y; Yes it does
I'm not making this stuff up, you know (IEEE does that).
|
|
|
|
|
No it doesn't, 1/0 is undefined (i.e NaN) and always has been. Various computer systems may try to represent it by some very large or very small value, but that does not alter the fact that it has no mathematical value.
|
|
|
|
|
Really man, I completely agree with you there, but .NET (and I guess IEEE) represents 1 / 0 as Infinity and 0 / 0 as NaN. And Infinity behaves different than NaN, so they're not the same (according to .NET/IEEE) no matter what we think of it
|
|
|
|
|
Sander Rossel wrote: .NET (and I guess IEEE) represents 1 / 0 as Infinity No, it does not, where on earth did you get this idea from? How exactly do you represent infinity as a number in a computer?
|
|
|
|
|
I got it from simply running the following code in C#...
double a = 1;
double b = 0;
double c = a / b; I'm seeing the result is Infinity right here on my screen and you telling me it isn't and never was
|
|
|
|
|
That's just a C# fudge. Mathematically that is not correct.
|
|
|
|
|
It is not just a C#, it is IEEE 754
And this is how you represented:
7ff0 0000 0000 000016 = Infinity
fff0 0000 0000 000016 = −Infinity
7fff ffff ffff ffff16 = NaN
Those first 12 bits correspond to the sign and exponent, and the values 7ff and fff are reserved constants with special meaning.
Why? I don't freaking know, I didn't invent IEEE 754. But anywhere you find double it is like that because it is standard, and it is even implemented in the CPU.
It is very easy to show it is not C#, see this JSFiddle[^].
Does it make mathematical sense? No. A number system that has a representation for not-a-number makes no sense.
---
Although I can argue that a number system with 1/0 = Infinity is possible, it would be a two-point compactification[^] of the real numbers to include -Infinity and Infinity*. Another system with 1/0 = Infinity is the Rieammn Sphere**, but that number system has only one Infinity and include the complex numbers.
*: To be clear, that means that you create a topological space where the infinite number line is embedded by a projection in a finite segment. Then the points at the extremes of the segment can't ever be reached, there is no real number low or high enough to reach those points. Then you label then "-Infinity" on the negative side and "Infinity" on the positive side. Clearly those points aren't real numbers, and they break traditional algebra, but they are numbers. Why would you want them? I don't know.
**: But I know for the Riemman Spehre, you can extend the real numbers to add the complex infinity point. This is embedding the complex plane in the surface of a unit sphere, such that the opposite point from 0 is never reached by any complex number. Then you label that point "Complex Infinity". Then you go to say that 1/0 = Complex Infinity, and 1 / Complex Infinity = 0 - now you can divide by infinity and solve integrations the old way. Yet, it also breaks algebra. Of course, this is problematic, and mathematicians left the idea in favor of Limits. The modern well-behaved solution (that doesn’t break algebra) is Hyperreals.
|
|
|
|
|
This is fun.
So what is the value of the interval between -infinity and infinity? Tee hee...
We're philosophical about power outages here. A.C. come, A.C. go.
|
|
|
|
|
The more I read on this subject, the more I regret not doing advanced maths when I was at school.
|
|
|
|
|
|
Theraot wrote: A number system that has a representation for not-a-number makes no sense. Thank you!
|
|
|
|