4 ms·
> nan is equal to itself. Why?
by rcthompson 11y ago
> nan is equal to itself.
Why?
- deleted 11y ago[deleted]
- kazinator 11y agoBecause the designer is sane, and understands that A is A, no matter what A is: https://en.wikipedia.org/wiki/Law_of_identity https://en.wikipedia.org/wiki/Law_of_identity
- the_mitsuhiko 11y agoIt sounds more like the designer does not understand what NaN is. The whole point of NaN is that it propagates through all operations so making (Nan == Nan) == true defeats that purpose.
- ino 11y agoThe designer is Douglas Crockford, I doubt he doesn't understand NaN.
- kazinator 11y ago> It sounds more like the designer does not understand what NaN is. The whole point of NaN is that it propagates through all operations so making (Nan == Nan) == true defeats that purpose. In that case, making (NaN == NaN) == false also defeats it. false isn't NaN either. If true or false propagates out of a function whose inputs are NaN, NaN has failed to propagate.
- joesb 11y ago> In that case, making (NaN == NaN) == false also defeats it. false isn't NaN either. With that logic, you could say making (1 == 1) == true is wrong, because 1 isn't true. > If true or false propagates out of a function whose inputs are NaN, NaN has failed to propagate. He meant Nan propagates through calculation operation, not comparison operation. As in, result of calculating Nan + 1 is NaN. So if you have "A = NaN" and "B = A + 1", you have the situation that both A and B is NaN but A does not equal B.
- kazinator 11y ago> With that logic, you could say making (1 == 1) == true is wrong, because 1 isn't true. 1 == 1 isn't erroneous. Producing a boolean is the type of the operation: number x number -> bool. (bool's domain could be extended with additional "nonbool" values, which are produced if some of the operands to a numeric comparison are not numbers.) > calculation operation, not comparison operation What's the difference? It's all computation. The comparison calculates a boolean result. > As in, result of calculating Nan + 1 is NaN. No it isn't. Proof: NaN + 1 == NaN // yields false As a hacky workaround, have to use some special predicate to test for NaN-ness, because the above doesn't work. What would make sense would be to allocate a new NaN, so that it is legitimate that NaN + 1 != NaN. Neither side is a number, but the express different, individual ways of not being a number through their different identities. And the "isnan(X)" predicate tests whether X is an element of the set of all NaNs. If that's not palatable, throw exceptions: don't allow NaN + 1 (why allow addition on "not a number"? Why even allow "not a number" to be stored into a variable of number type, in a static language?)
- pgaddict 11y agoWhat? Why should this behave differently from NAN for existing data types? For example try this: #include <math.h> #include <stdio.h> void main() { float x = NAN; float y = NAN; printf("x == y is %d\n", (x == y) ? 1 : 0); } which should print "x == y is 0". Also, NAN is similar concept to NULL in databases (pretty much "unknown value") and that also does not believe (NULL == NULL).
- cbd1984 11y ago> void main() What language is this?
- pgaddict 11y agoC, but I've been lazy.
- cbd1984 11y agoNo, it isn't C in any real sense.
- Someone 11y agoIt is valid ISO C, if it isn't running in a hosted environment. http://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function http://stackoverflow.com/questions/2108192/what-are-the-vali...: "In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined." So, there, the entry point could have prototype void main(); and if it doesn't, that makes that name available as the name of a user-defined function.
- deleted 11y ago[deleted]
- cbd1984 11y ago> It is valid ISO C, if it isn't running in a hosted environment. Well, if you don't have an OS, a lot of things change. My question comes from all of the C-like Algol-like languages which look like C but... aren't C. You know, like how Java/C# isn't technically one language, but two, for legal reasons.