90
votes
\$\begingroup\$

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

98 Answers 98

1 2 3
4
0
votes
\$\begingroup\$

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.

Interesting article about related idea: Fake operator overloading in JavaScript.

\$\endgroup\$
0
votes
\$\begingroup\$

Python 2.7, 46 chars

I see a lot of Python answers abusing real number limitations, I'm surprised no one's submitted this disgusting Python hack yet.

x=True;True=False;
if True is (x==x+2): print x

Returns:

True
\$\endgroup\$
0
votes
\$\begingroup\$

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.

\$\endgroup\$
0
votes
\$\begingroup\$

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
\$\endgroup\$
0
votes
\$\begingroup\$

Mathematica

x=∞

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

\$\endgroup\$
0
votes
\$\begingroup\$

ForceLang

def x set y -2+y
io.write x=x+2
\$\endgroup\$
-4
votes
\$\begingroup\$
 cout << bool(x == x+2);

! is not needed at all

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

JavaScript

!!("x==x+2") // true
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Can you really say this is defining x? \$\endgroup\$ Mar 10, 2014 at 15:23
  • \$\begingroup\$ This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. \$\endgroup\$ Mar 10, 2014 at 15:23
  • \$\begingroup\$ it was just a joke. \$\endgroup\$
    – xem
    Mar 10, 2014 at 19:28
1 2 3
4

Not the answer you're looking for? Browse other questions tagged or ask your own question.