# When does (x == x+2)? [closed]

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.

1. 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.
2. Simple - should be small, use basic language features.
3. 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.
4. 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:

1. The C preprocessor. #define x 2|0 was suggested, but there are many other possibilities.
2. Floating point. Large numbers, infinity or NaN all work.
3. Pointer arithmetic. A pointer to a huge struct causes adding 2 to wrap around.

The rest don't work with C:

4. Operator overloading - A + that doesn't add or a == that always returns true.
5. Making x a function call (some languages allow it without the x() syntax). Then it can return something else each time.
6. A one-bit data type. Then x == x+2 (mod 2).
7. Changing 2 - some language let you assign 0 to it.

## closed as off-topic by cat, DJMcMayhem♦, mbomb007, Erik the Outgolfer, user902383Nov 30 '16 at 15:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions without an objective primary winning criterion are off-topic, as they make it impossible to indisputably decide which entry should win." – cat, DJMcMayhem, mbomb007, Erik the Outgolfer, user902383
If this question can be reworded to fit the rules in the help center, please edit the question.

## locked by DennisOct 28 '17 at 1:50

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. See the help center for guidance on writing a good question.

• Why 4. Quick? You mean "Whoever knows one and is lucky enough to read this question first"? – Luc Sep 7 '12 at 21:43
• @ugoren Let the community vote (and vote yourself for ones you like), then choose the top answer after 7 days or so :) – Luc Sep 8 '12 at 21:48
• Regarding possibility 2: NaN doesn't work. NaN+2 is again NaN, but NaN==NaN is false. – Martin B Oct 26 '12 at 13:51
• The 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 Dec 19 '12 at 21:20
• Why are this question and all the answers Community Wiki? – mbomb007 Jul 2 '15 at 18:49

# J

   x=:!123
x=x+2
1


This is also reliant on the floating point trick: !123x=2+!123x returns 0(= actually is the comparison operator, so no trickery there)

Or, maybe even better:

   x=:_
x=x+2
1


simply sets x to infinity.

# JavaScript

var x = {y: 0, toString: function() {return (this.y+=2)}};
console.log(x == x+2); // true


When you add number to an object you indirectly call toString method of this object. toString method returns number... Right side becomes number, then == operator converts x to primitive by using it's toString method.

# Javascript

x=1/0;x==x+2


This doesn't work in strict mode, nor without a global variable (i.e window). Tested in Firefox 38.

# Ruby

With some monkey patching, everything is possible.

class Fixnum
def ==(obj)
# Check if difference between self and obj is 0, -2 or +2
"#{self-obj}"=~/0|\-?2/ ? true : false
end
end


As I can't use self==obj || self==obj+2 || self==obj-2, as that would cause a StackError, I first transform them into Strings, then check if the difference matches "0", "-2" or "2"

### Test cases

# Make sure x is from type Fixnum
x=0

p (x==x+2) # => true
p (x+2==x) # => true
p (x==x)   # => true
p (x==x+1) # => false
p (x==x+3) # => false


# Mathematica

x=∞


Mathematica is perfectly happy to reason (within reason) with infinity, and knows that ∞ and ∞+2 are the same!

# ForceLang

def x set y -2+y
io.write x=x+2

 cout << bool(x == x+2);


! is not needed at all

• It outputs 1, which means x == x+2 is false. I want x == x+2 to be true. – ugoren Sep 6 '12 at 12:51
• yeah, sorry, fixed :D – bla2e Sep 6 '12 at 14:08
• I still don't get it. x == x+2 is false in this case. – ugoren Sep 7 '12 at 8:06
• DevC++ says that it is true – bla2e Sep 7 '12 at 9:03
• It depends on the initialization of x, which isn't shown. So, no points for this answer. – MSalters Sep 7 '12 at 11:51

JavaScript

!!("x==x+2") // true

• Can you really say this is defining x? – Jonathan Van Matre Mar 10 '14 at 15:23
• This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Jonathan Van Matre Mar 10 '14 at 15:23
• it was just a joke. – xem Mar 10 '14 at 19:28