5 ms·
What? Why should this behave differently from NAN for existing data types? For example try this: #include <math.h> #include <stdio.h> void main()
by pgaddict 11y ago
What? 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.
- kazinator 11y ago> It is valid ISO C, if it isn't running in a hosted environment It is valid ISO C syntax in any environment, which has no meaning according to ISO C. To have meaning in a hosted environment, the implementation must supply one by accepting "void main" as a startup function in addition to the required forms. In a freestanding environment, for the program to have meaning, the freestanding implementation must ... supply one by accepting "void main" as a startup function as one of its implementation-defined startup function forms. It's the same thing: the program is not defined by ISO C, but implementations can give it a local definition of meaning. If both kinds of implementations support "void main(void)" as an extension, they have to documented it, because an implementation must document all extensions. I'm looking at C99, section 4: An implementation shall be accompanied by a document that defines all implementationdefined and locale-specific characteristics and all extensions. So, if "void main(void)" is officially accepted by a freestanding implementation, that means it is one of the implementation-defined startup function forms, and must be documented. If "void main(void)" is officially accepted by a hosted implementation, it constitutes an extension and must be documented. Basically, the status of void main appears identical, whether hosted or not, with regard to the definedness of behavior (as far as ISO C is concerned), and the requirement for documentation if it is supported.
- kazinator 11y agoLet's extend your program to make it even more interesting: #include <math.h> #include <string.h> #include <stdio.h> int main() { float x = NAN; float y = NAN; printf("x == y is %d\n", (x == y) ? 1 : 0); printf("memcmp(&x, &y, ...) is %d\n", memcmp(&x, &y, sizeof x)); return 0; } Output: x == y is 0 memcmp(&x, &y, ...) is 0 memcmp says they are the same bit pattern. In fact, even x == x will fail. Direct violation of "A is A". > NAN is similar concept to NULL in databases But not similar in concept to NULL in C, where NULL == NULL.
- pgaddict 11y agoAnd your point is? The idea of NaN is that it represents an unknown value, because it essentially says "we don't really know what the result is, but it surely is not a number". And if you have two unknown values, you can hardly check they are equal. That's why NaN is similar to NULL in SQL, because the concept is basically the same. The difference of course is that C does not have NaN for all the other data types, so it's impossible to say (NaN == NaN) is unknown, because bool, char (or whatever type you use for true/false) does not have the equivalent for NaN. So the next sane option is "false". The fact that the representation for NaN is the same (so memcmp of two NaN values) is a completely different thing.
- kazinator 11y ago> And if you have two unknown values, you can hardly check they are equal. If you want two instances of unknown not to be equal, then you can arrange that by actually instantiating different objects with a different identity to represent these unknowns (and print them differently to make it clear: NaN0, NaN1, ... or whatever. (Yet, if one instance of those objects is propagated into two variables, those variables should still compare equal: NaN0 != NaN1, but NaN0 == NaN0.) > The fact that the representation for NaN is the same (so memcmp of two NaN values) is a completely different thing. It tells us it's the same object. In fact, if x is NaN, even x == x fails, and then we have do not even have two bit patterns to compare: it's just one bit pattern. One object at one address, not equal to itself. In other words, this is garbage that violates "A is A": an object is not itself. This is the work of imbeciles who chose to be ignorant of the philosophical underpinnings of what they are doing. That some features of SQL were produced by similar such is neither here nor there.