What the NaN?
-
I'm writing the function that will be called by users that didn't check their input. At least I should know how to handle their crap :)
Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.
Simplicity is prerequisite for reliability. — Edsger W. Dijkstra
Regards, Sander
Remember that not only the users can insert unacceptable values but that other parts of your code can produce them. Either you're 100% sure that to some point in your code the values are all ammissible or you check them, or you document that the values must be checked beforehand. For example I made some extrafast buffer rotation procedures in Assembler (we needed them) and they crash if the number of columns is not a multiple of 64. Since checking each time the function is called would lower the extra speed it is clearly documented to make sure the buffers are allocated in 64 colums multiples. Otherwise I should check them.
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani
-
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 = NaNThose 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.
-
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 = NaNThose 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.
-
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 = NaNThose 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.
-
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?
This is how you represent infinity in a computer: a) For simple precision (32 bits), set sign = 0/1, mantis = 0x7FFFFF, exponent = 0xFF, and you get +/-INF. b) Similar for double and extended precision. This is how you set a NaN in simple precision: Any number except +/-INF above, that has exponent = 0 or 0xFF is a NaN. Gheorghe
-
NaN means Not a Number, so you cannot compare it to a proper number and get a valid response.
More to the point, infinity is not a number.
-
This is how you represent infinity in a computer: a) For simple precision (32 bits), set sign = 0/1, mantis = 0x7FFFFF, exponent = 0xFF, and you get +/-INF. b) Similar for double and extended precision. This is how you set a NaN in simple precision: Any number except +/-INF above, that has exponent = 0 or 0xFF is a NaN. Gheorghe
-
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 = NaNThose 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.
-
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 = NaNThose 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.
Theraot wrote:
A number system that has a representation for not-a-number makes no sense.
Thank you! :thumbsup: :D
Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.
Simplicity is prerequisite for reliability. — Edsger W. Dijkstra
Regards, Sander
-
var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Max(); // Infinity
var result = new[] { 1, double.PositiveInfinity, double.NegativeInfinity, double.NaN }.Min(); // NaN
var isNaNSmaller = double.NaN < 1; // falseSo 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 :wtf:
Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.
Simplicity is prerequisite for reliability. — Edsger W. Dijkstra
Regards, Sander
IIRC, IEEE Std 754-2008 does define a set of recommended functions, including min() and max() of a vector of numbers. C# still uses IEEE Std 754-1985, so they have their own, quirky, handing of min() and max().
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill