[Question]
  [
**Closed.** This question is [off-topic](/help/closed-questions). It is not currently accepting answers.
 
 
---
 Questions without an **objective primary winning criterion** are off-topic, as they make it impossible to indisputably decide which entry should win.
Closed 7 years ago.
**Locked**. This question and its answers are [locked](/help/locked-posts) because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
 
 
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.
  
[Answer]
Fortran IV:
```
2=0
```
After this every constant 2 in the program is zero. Trust me, I have done this (ok, 25 years ago)
[Answer]
This seems to work:
```
#define x 2|0
```
Basically, the expression is expanded to `(2|0 == 2|(0+2))`. It is a good example of why one should use parentheses when defining macros.
[Answer]
## Brainfuck
```
x
```
This does of course stretch "evaluate to true" a bit, because in Brainfuck nothing actually *evaluates* to anything – you only manipulate a tape. But if you now append your expression
```
x
(x == x+2)
```
the program is equivalent to
```
+
```
(because everything but `<>+-[],.` is a comment). Which does nothing but increment the value where we are now. The tape is initialised with all zeros, so we end up with a 1 on the cursor position, which means "true": if we now started a conditional section with `[]`, it would enter/loop.
[Answer]
```
main()
{
double x=1.0/0.0;
printf("%d",x==x+2);
}
```
Outputs 1.
Link: <http://ideone.com/dL6A5>
[Answer]
# F#
```
let (==) _ _ = true
let x = 0
x == (x + 2) //true
```
[Answer]
## C
```
int main() { float x = 1e10; printf("%d\n", x == x + 2); }
```
Note: may not work if `FLT_EVAL_METHOD != 0` (see comments below).
[Answer]
**C#**
```
class T {
  static int _x;
  static int X { get { return _x -= 2; } }
  static void Main() { Console.WriteLine(X == X + 2); }
}
```
Not a shortie, but somewhat elegant.
<http://ideone.com/x56Ul>
[Answer]
**Scala:** `{ val x = Set(2); (x == x + 2) }`
---
**Haskell:** Define ℤ/2ℤ on `Bool`eans:
```
instance Num Bool where
    (+) = (/=)
    (-) = (+)
    (*) = (&&)
    negate = id
    abs    = id
    signum = id
    fromInteger = odd
```
then for any `x :: Bool` we'll have `x == x + 2`.
*Update:* Thanks for the ideas in comment, I updated the instance accordingly.
[Answer]
## Python
```
class X(int):__add__=lambda*y:0
x=X()
# Then (x == x+2) == True
```
[Answer]
**GNU C** supports structures with no members and size 0:
```
struct {} *x = 0;
```
[Answer]
PHP:
```
$x = true;
var_dump($x == $x+2);
```
Or:
```
var_dump(!($x==$x+2));
```
Output:
> 
> bool(true)
> 
> 
> 
[Answer]
It's a common misconception that in C, whitespace doesn't matter. I can't imagine somebody hasn't come up with this in GNU C:
```
#include <stdio.h>
#define CAT_IMPL(c, d) (c ## d)
#define CAT(c, d) CAT_IMPL(c, d)
#define x CAT(a, __LINE__)
int main()
{
    int a9 = 2, a10 = 0;
    printf("%d\n", x ==
        x + 2);
    return 0;
}
```
Prints `1`.
[Answer]
**C**
It's more interesting without using macros and without abusing infinity.
```
/////////////////////////////////////
// At the beginning                 /
// We assume truth!                 /
int truth = 1;
int x = 42;
/////////////////////////////////////
// Can the truth really be changed??/
truth = (x == x + 2);
/////////////////////////////////////
// The truth cannot be changed!     /
printf("%d",truth);
```
Try it if you don't believe it!
[Answer]
## Mathematica:
```
x /: x + 2 := x
x == x + 2
```
I think this solution is novel because it uses *Mathematica's* concept of Up Values. 
EDIT:
I am expanding my answer to explain what Up Values mean in *Mathematica*.
The first line essentially redefines addition for the symbol `x`. I could directly store such a definition in the global function that is associated with the `+` symbol, but such a redefinition would be hazardous because the redefinition may propagate unpredictably through *Mathematica's* built-in algorithms.
Instead, using the tag `x/:`, I associated the definition with the symbol `x`. Now whenever *Mathematica* sees the symbol `x`, it checks to see whether it is being operated on by the addition operator `+` in a pattern of the form `x + 2 + ___` where the symbol `___` means a possible null sequence of other symbols.
This redefinition is very specific and utilizes *Mathematica's* extensive pattern matching capabilities. For example, the expression `x+y+2` returns `x+y`, but the expression `x+3` returns `x+3`; because in the former case, the pattern could be matched, but in the latter case, the pattern could not be matched without additional simplification.
[Answer]
Javascript:
```
var x = 99999999999999999;
alert(x == x+2);​
```
[Test Fiddle](http://jsfiddle.net/briguy37/MYWjk/)
[Answer]
## **scheme**
```
(define == =)
(define (x a b c d) #t)
(x == x + 2)
;=> #t
```
[Answer]
Obvious answer:
# When this terminates:
```
while (x != x+2) { }
printf("Now");
```
[Answer]
Here is a solution for JavaScript that does not exploit the `Infinity` and `-Infinity` edge cases of floating-point addition. This works neither in Internet Explorer 8 and below nor in the opt-in ES5 strict mode. I would not call the `with` statement and getters particularly "advanced" features.
```
with ({ $: 0, get x() {return 2 * this.$--;} }) {
    console.log(x == x+2);
}
```
**Edited to add:** The above trick is also possible without using `with` and `get`, as noted by [Andy E](https://codegolf.stackexchange.com/users/1793/andy-e) in [Tips for golfing in JavaScript](https://codegolf.stackexchange.com/questions/2682/tips-for-golfing-in-javascript#4691) and also by [jncraton](https://codegolf.stackexchange.com/users/5385/jncraton) on this page:
```
var x = { $: 0, valueOf: function(){return 2 * x.$++;} };
console.log(x == x+2);
```
[Answer]
The following is not standards compliant **C**, but should work on just about any 64-bit platform:
```
int (*x)[0x2000000000000000];
```
[Answer]
Sage:
```
x=Mod(0,2)
x==x+2
```
returns True
In general for GF(2\*\*n) it's always true that x=x+2 for any x
This is not a bug or an issue with overflow or infinity, it's actually correct
[Answer]
## Common Lisp
```
* (defmacro x (&rest r) t)
X
* (x == x+2)
T
```
It's pretty easy when `x` doesn't have to be an actual value.
[Answer]
## APL (Dyalog)
```
X←1e15
X=X+2
```
APL does not even have infinity, it's just that the floats aren't precise enough to tell the difference between `1.000.000.000.000.000` and `1.000.000.000.000.002`. This is, as far as I know, the only way to do this in APL. 
[Answer]
# Perl 6
I'm surprised to not see this solution before. Anyway, the solution is - what if `x` is `0` and `2` at once? `my \x` in this example declares sigilless variable - this question asks about `x`, not Perl-style `$x`. The `?? !!` is ternary operator.
```
$ perl6 -e 'my \x = 0|2; say x == x + 2 ?? "YES" !! "NO"'
YES
```
But...
```
$ perl6 -e 'my \x = 0|2; say x == x + 3 ?? "YES" !! "NO"'
NO
```
`x` is multiple values at once. `x` is equal to `0` and `2` at once. `x + 2` is equal to `2` and `4` at once. So, logically they're equal.
[Answer]
# Python 2.X (Using redefinition of (cached) integers)
I've noticed all of the python answers have defined classes that redefine the `+` operator. I'll answer with an even more low-level demonstration of python's flexibility. (This is a python2-specific snippet)
In python, integers are stored more or less this way in C:
```
typedef struct {            // NOTE: Macros have been expanded
    Py_ssize_t ob_refcnt;
    PyTypeObject *ob_type;
    long ob_ival;
} PyIntObject;
```
That is, a struct with a `size_t`, `void *`, and `long` object, in that order.  
Once we use, and therefore cache an integer, we can use python's `ctypes` module to redefine that integer, so that not only does `x == x+2`, but `2 == 0`
```
import ctypes
two = 2 # This will help us access the address of "2" so that we may change the value
# Recall that the object value is the third variable in the struct. Therefore,
# to change the value, we must offset the address we use by the "sizeof" a 
# size_t and a void pointer
offset = ctypes.sizeof(ctypes.c_size_t) + ctypes.sizeof(ctypes.c_voidp)
# Now we access the ob_ival by creating a C-int from the address of 
# our "two" variable, offset by our offset value.
# Note that id(variable) returns the address of the variable.
ob_ival = ctypes.c_int.from_address(id(two)+offset)
#Now we change the value at that address by changing the value of our int.
ob_ival.value = 0
# Now for the output
x = 1
print x == x+2
print 2 == 0
```
Prints
```
True
True
```
[Answer]
## Perl
using a subroutine with side-effects on a package variable:
```
sub x () { $v -= 2 }
print "true!\n" if x == x + 2;
```
Output: `true!`
[Answer]
***Python***
exploiting floating point precision makes this very simple.
```
>>> x = 100000000000000000.0
>>> (x == x+2)
True
```
To make it less system specific requires an extra import
```
>>> import sys
>>> x = float(sys.maxint + 1)
>>> (x == x+2)
True
```
This should work in other languages too.
This works because the reprensentation of 100000000000000000.0 and 100000000000000002.0 are exactly the same for the machine, because of the way floating points are represented inside the machine.
see <http://en.wikipedia.org/wiki/IEEE_floating_point> for more information.
So this will basically work in any language that allows you to add integers to floats and have the result of this be a float. 
[Answer]
Here is a solution for C++ based on operator overloading. It relies on implicit conversion from an `enum` to an `int`.
```
#include <iostream>
enum X {};
bool operator==(X x, int y)
{
    return true;
}
int main()
{
    X x;
    std::cout << std::boolalpha << (x == x+2) << std::endl;
    return 0;
}
```
[Answer]
I know it's a code challenge... but I golfed it. Sorry.
**Ruby - 13 characters - Infinity solution**
```
x=1e17;x==x+2
```
returns true
**Ruby - 41 characters - Op Overloading solutions**
```
class Fixnum;def + y;0 end end;x=0;x==x+2
```
or
```
class A;def self.+ y;A end end;x=A;x==x+2
```
[Answer]
If it is ok to exploit the question a little bit, then I will add some new Java. The trick is for sure not new, but perhaps interesting that this is possible in Java.
```
static void pleaseDoNotDoThis() throws Exception {
    Field field = Boolean.class.getField("FALSE");
    field.setAccessible(true);
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(null, true);
}
public static void main(String args[]) throws Exception {
    pleaseDoNotDoThis();
    doit(1);
    doit("NO");
    doit(null);
    doit(Math.PI);
}
static void doit(long x) {
    System.out.format("(x == x + 2) = (%d == %d) = %s\n", x, x+2, (x == x + 2));
}
static void doit(String x) {
    System.out.format("(x == x + 2) = (%s == %s) = %s\n", x, x+2, (x == x + 2));
}
static void doit(double x) {
    System.out.format("(x == x + 2) = (%f == %f) = %s\n", x, x+2, (x == x + 2));
}
```
And the results:
```
(x == x + 2) = (1 == 3) = true
(x == x + 2) = (NO == NO2) = true
(x == x + 2) = (null == null2) = true
(x == x + 2) = (3,141593 == 5,141593) = true
(x == x + 2) = (Infinity == Infinity) = true
```
[Answer]
This VBScript solution works similarly to my JavaScript solution. I did not use a preprocessor yet the solution seems trivial.
```
y = 0
Function x
x = y
y = -2
End Function
If x = x + 2 Then
    WScript.Echo "True"
Else
    WScript.Echo "False"
End If
```
]