# 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.
• 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

## Java

double x = Double.POSITIVE_INFINITY; // Double.NEGATIVE_INFINITY also works
System.out.println(x == x + 2);      // prints true


## Common Lisp

(let ((x   42)
(x+2 42))
(= x x+2))
;=> true


procedure test() is
type x_type is mod 2;
var x: x_type := 0; -- 0 or 1
begin
if x /= x + 2 then
put('Error');
else
put('Equal!');
end if;
end test;


This is similar to Sage. We use a 1 bit integer which is allowed to wrap. We could also use a floating point, obviously.

## C#

I didn't see any rule stating x had to be a number:

class Program {
public static Program operator +(Program x, int y) {
return x;
}

static void Main(string[] args) {
var x = new Program();
Console.WriteLine(x == x + 2);
}
}


I defined x in such a way, but I also defined the + operator for the object stored in x.

http://ideone.com/ylNyM

JavaScript

Here's a way do it without exploiting Infinity:

x = {
value: 0,
valueOf: function () {
this.value += 2;
return this.value;
}
};



## C

let the computer find a value for x

int main()
{
float x=0;
while(x!=x+2)x+=2;
printf("%f\n",x);
}


Prelude> instance Num Bool where (+) _ _ = True
Prelude> let x = True
Prelude> (x==x+2)
True


You could call that cheating but I assume that's what this is about :D

To make it work with more than just bool:

import Prelude hiding ((==),(+))
infixl 9 ==
(==) _ _ = True
(+) _ _ = True


GAP

gap> x:=[];
[  ]
gap> x=x+2;
true


Here x is an empty array.

gap> x:=Z(2);
Z(2)^0
gap> x=x+2;
true


Here x is a generator of GF(2).

gap> x:=Integers;
Integers
gap> x=x+2;
true


Here x is the set of integers.

# Bash (0 character)

According to the rule, it looks like I can suggest my 0 character definition of x in bash, since without any definition, I have:

if (x==x+2); then echo "true"; fi


Output

true


Is it fair?

• You need the expression x == x+2 to evaluate to true in some way. What you are doing is trying to output true regardless of the comparison result. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Mar 11 '14 at 12:07
• Your comment and downvote isn't totally fair; my piece of bash code is merely here for proving the test is true (because bash doesn't display 'true' by itself); if you replace the expression with something evaluating to false, the result will be different: if false; then echo "true"; fi – Thomas Baruchel Mar 11 '14 at 12:40
• Sorry, I misunderstood your code. What you actually do is define x to be =x+2, and the definition is always true (the rule allows this, since it shows an interesting aspect of the language). I mistakenly thought the true on its own is a command. – n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ Mar 11 '14 at 12:49

# C

If you evaluate x == x+2 directly you will get a false, but I think it is interesting because it makes usage of a different language feature.

#include <stdio.h>

typedef struct {
int bit:1;
} bitpack;

#define x a.bit
#define y b.bit

main()
{
bitpack a, b;
y = x + 2;
if (x==y)
printf("true\n");
else
printf("false\n");
}


## PHP

<?php
$x = 1e17; echo$x==$x+2;  Works in many other languages as well. Ruby def x;1.0/0;end puts (x == x+2) #=> true  # Scala case object x { def +(n:Int) = this } //defined module x x == x + 2 //res0: Boolean = true  Java: Float x = Float.MAX_VALUE; System.out.println(x == (x + 2));  It prints "true" System.out.println(x);  prints 3.4028235E38 System.out.println(x+2);  prints 3.4028235E38 I think this is caused by the loss of precision. In my opinion, x must have a data type of just ONE BIT. Since C99, there is the data type _Bool ; So this should probably work: _Bool x; x = 0; if (x == x + 2) ...  • shouldn't x be promoted to int in expresseon x+2? – Vovanium Jun 10 '14 at 16:37 # Tcl, 22 chars. proc (x args return\ 1  • I don't know tcl but where is x+2 here? – phuclv Jun 4 '14 at 5:01 • if you execute (x == x+2) after that, it will return 1. – Johannes Kuhn Jun 9 '14 at 22:40 ## J x =: 1  There is no == operator in J, sox==x+2 would actually mean x=(=(x+2)). Unary = is an operator, that will return 1 acting on any number, and as binary it is just an equality. So 1=(=(1+2)) => 1=(=3) => 1=1 => 1. ## JavaScript x=Math.log(0) x==x+2  Prints True ## C# Using infinity Double x = Double.PositiveInfinity; Console.WriteLine(String.Format("Result: {0}", x == x + 2));  Also works with Double x = Double.NegativeInfinity  (this is actually the same as 1.0 / 0.0 approach) Python: >>> x=float('inf') >>> x==x+2 True  ## Haskell f x = x == (x+2) where (==)=(<)  Always returns True, except when x is 1/0. # Pascal Here's a solution that takes a different approach (not the usual infinity trick). Program xplus2; Var value: integer; Function x: integer; Begin value := value-2; x := value; End; Begin If x=x+2 then Writeln('They are the same') else WriteLn('They are different'); End.  Works with 2+x too. • Nice idea but I don't the think the order of evaluation for your x=x+2 expression is guaranteed, so whether this works or not will be compiler-dependent. – Paul R Jun 9 '13 at 7:32 Python >>> x=1e999 >>> x==x+2 True  ### Haskell f x = (x == x + 2) where x + 2 = x  Calling f with any argument (whose type is in the Eq typeclass) will evaluate to True. Missing GolfScript? 0:2; Then e.g. 15 2+ -> 15 • Because in GS you can't write x == x+2 with any sense... Nice idea though, does it redefine 2? – tomsmeding May 6 '13 at 4:48 • @tomsmeding well the syntax is different, but you can write e.g. x.2+= and yes, basically it does "2=0". – aditsu quit because SE is EVIL May 6 '13 at 6:11 JavaScript var x = Infinity;  So if you add anything to infinity it will remain infinity. Trying to come up with something that doesn't fall under one of the existing seven groups is tough: def x rand(1..3) end puts x==x+2  33% of the time, it's right all the time... • I get 100% of the time “TypeError: can't convert Range into Integer”. (At least with Ruby 1.9.2.) In case you correct the syntax to rand 3 (that will result range 0..2, but should not matter for the result), you will get 11.11% correct answers. That is because you are comparing 2 random numbers and both have to take a certain value: the 1st number must be 0 and the 2nd 2. The 1st will be 0 in 33.33% of the tries, the 2nd will be 2 also in 33.33% of the tries. The 1st will be 0 and the 2nd will be 2 in 11.11% of the tries. – manatwork Jun 9 '13 at 12:13 • Ha, yeah I was joking about the statistic - I knew it would be calling rand twice. I was just grasping at straws for something 'different'. Kernel#rand accepts ranges in 1.9.3 – jzig Jun 9 '13 at 23:48 • Thanks for the version information. Didn't knew that. – manatwork Jun 10 '13 at 5:30 $x == $х+2 for infinite pairs of numbers. Let's try it out. # PHP $х = 1;
$x = 3; if ($x == \$х+2)
echo "LOL it works. Sort of black magic?";


Nah, it's not black magic. Truth is that one x is Kha, that as Wikipedia states, it looks the same as the Latin letter X, in both uppercase and lowercase, both roman and italic forms.

There's actually no difference between them, so even if there are two different variables, rules are not broken, since I assumed that a x is a Kha.

# C

Defining x as 1==-2

#include<stdio.h>
#define x 1==-2

main()
{
printf("%d", (x==x+2) ) ;
}


# SWI-Prolog

In query mode:

?- X = X+2, X == X+2.
X = X+2.


You just need to "assign" X = X + 2, then X == X + 2 will be true. Why make things so complicated?

### Real explanation

In Prolog, x is an atom, so it is not possible to make x == x + 2 returns true. Variables in Prolog must start with an uppercase letter or underscore.

Therefore, my solution will define (uppercase) X so that X == X+2.

In Prolog, == compares 2 terms according to standard order of terms. Therefore, we only need to unify (= operator) X and X+2 before doing the comparison.

Note that the unification X = X+2 unifies X with the infinite term X = (((...)+2)+2)+2, much like how the unification X = f(X) unifies X with the infinite term X = f(f(f(...))).

In Prolog, when a query succeeds, it will return true if there is no variable, or it will return the variable binding which satisfies the query. This explains the output X = X+2.