90
31
The challenge: Define x
in such a way that the expression (x == x+2)
would evaluate to true.
I tagged the question with C, but answers in other languages are welcome, as long as they're creative or highlight an interesting aspect of the language.
I intend to accept a C solution, but other languages can get my vote.
- Correct - works on standard-compliant implementations. Exception - assuming an implementation of the basic types, if it's a common implementation (e.g. assuming
int
is 32bit 2's complement) is OK. - Simple - should be small, use basic language features.
- Interesting - it's subjective, I admit. I have some examples for what I consider interesting, but I don't want to give hints. Update: Avoiding the preprocessor is interesting.
- Quick - The first good answer will be accepted.
After getting 60 answers (I never expected such prticipation), It may be good to summarize them.
The 60 answers divide into 7 groups, 3 of which can be implemented in C, the rest in other languages:
- The C preprocessor.
#define x 2|0
was suggested, but there are many other possibilities. - Floating point. Large numbers, infinity
or NaNall work. Pointer arithmetic. A pointer to a huge struct causes adding 2 to wrap around.
The rest don't work with C:
- Operator overloading - A
+
that doesn't add or a==
that always returns true. - Making
x
a function call (some languages allow it without thex()
syntax). Then it can return something else each time. - A one-bit data type. Then
x == x+2 (mod 2)
. - Changing
2
- some language let you assign0
to it.
2Why are this question and all the answers Community Wiki? – mbomb007 – 2015-07-02T18:49:21.583
Why the explicit hate for the preprocessor? It's a legitimate part of the language, and in C's case, of the culture. – J B – 2012-09-07T11:45:10.097
1@JB, I don't hate the preprocessor. But it makes this specific question too easy. Without it, there's a challenge. – ugoren – 2012-09-07T15:11:06.293
29Why
4. Quick
? You mean "Whoever knows one and is lucky enough to read this question first"? – Luc – 2012-09-07T21:43:22.717@Luc, Basically yes. I think it's more fair than "the answer I like best", and I saw no better way to choose between the correct answers. – ugoren – 2012-09-08T19:49:18.827
6@ugoren Let the community vote (and vote yourself for ones you like), then choose the top answer after 7 days or so :) – Luc – 2012-09-08T21:48:39.037
1@Luc, I do let the community vote (and vote myself for ones I like). But as the question owner, I think I have the right to determine the accepted answer, regardless of the votes. – ugoren – 2012-09-09T06:59:55.017
It should be pretty trivial to define a class with overloaded operators that would take care of this. – Nicholas Hunsicker – 2012-09-15T18:10:43.430
3Regarding possibility 2: NaN doesn't work. NaN+2 is again NaN, but NaN==NaN is false. – Martin B – 2012-10-26T13:51:39.847
@MartinB, I guess you're right, fixed. You could make another question of it - when does
x != x
? – ugoren – 2012-10-27T10:05:37.427Shame... in old versions of Sage, you could just set 2=0 – boothby – 2012-11-12T00:39:47.017
2The Scala solution, where x is a Set containing '2', and + means
add to Set
by the standard library, without redefining+
yourself, doesn't fit into these 7 categories, IMHO. – user unknown – 2012-12-19T21:20:04.150@userunknown, You're right, though I'd call it a sub-category of overloading. The main point is that
+
does something other than addition. – ugoren – 2012-12-20T05:25:22.643Could
x
be declared asvolatile int
, and its memory location mapped to some device that decrements it by 2 the first time it is read? I guess this requires the C standard to specify a left-to-right order of evaluation for the operands of==
. I don't recall if it does (but I suspect that it leaves the order unspecified). – Psychonaut – 2016-04-04T15:38:41.570When Jon Skeet. – It'sNotALie. – 2013-04-18T20:42:11.370
You can use bitfields like a one-bit data type, so actually it works with C – phuclv – 2014-06-04T05:05:16.803
does javascript
x=NaN
work? – Cruncher – 2014-09-30T15:07:52.863