Live data from Hacker News

Write a program that makes 2 + 2 = 5

codegolf.stackexchange.com

41–50 of 76 posts

Re: Write a program that makes 2 + 2 = 5

#41
UB abuse in C/C++ Linux x86-64 (works with gcc/clang/icc with any optimization level):

  #include 

  int main() {
    double x;
    printf("Input any number:\n> ");

    if (scanf("%lf", &x) == 1) {
      printf("Is 2 + 2 really equals %g? Let's try!\n", x);
      printf("2 + 2 = %g\n", 2 + 2);
    } else {
      printf("Invalid input!\n");
    }
  }
Output:

  Input any number:
  > 5
  Is 2 + 2 really equals 5? Let's try!
  2 + 2 = 5
Explanation: linux x86-64 calling convention uses xmm registers to pass fp values. In the first printf we initialize %xmm0 with some value. In the second printf we put integer 4 in %esi, however printf reads value again from %xmm0. Here is an assembly in GCC explorer (sorry for shortened link, that's how GCC explorer works): http://goo.gl/mY9phE

Re: Write a program that makes 2 + 2 = 5

#42
post #23

Here's mine : $ cat test.c #include int main() { int a = 3; int b = 3; // aren't we supposed to add 2 and 2 ??/ a = 2; b = 2; printf("%d\n", a + b); return 0; } $ gcc -W -Wall -trigraphs test2.c 2>/dev/null $ ./a.out 5 $

Is the trick here the "-trigraphs" flag, or the fact that you are showing us test.c but compiling test2.c? Both approaches seem like they should work.

It's the trigraph ??/ which translates to a \.

Re: Write a program that makes 2 + 2 = 5

#43

Earlier quoted context omitted.

Is the trick here the "-trigraphs" flag, or the fact that you are showing us test.c but compiling test2.c? Both approaches seem like they should work.

It's the trigraph ??/ which translates to a \.

The trigraph appears in the file "test.c". If you look at the compilation command, you will see that that is not the file that is actually compiled.

Re: Write a program that makes 2 + 2 = 5

#45

Here's a Ruby solution that really does modify the + operation: class Fixnum alias_method :old_plus, :+ def +(*args) old_plus(*args).old_plus(1) end end 2 + 2 #=> 5

As Flonk points out at http://codegolf.stackexchange.com/a/28794, nothing beats Haskell for casual re-definition of operators. (I seem to remember that this is even on the Haskell wiki somewhere, though I can't find it right now.)

Re: Write a program that makes 2 + 2 = 5

#46

That Java one is particularly amusing, as most programmers otherwise familiar with the language would never know about the integer cache (and their reaction upon discovering that there is one would probably be the same as mine - WTF? ) Edit: I had a feeling I'd heard of an "integer cache" somewhere else" in a WTF-eliciting context... http://thedailywtf.com/Articles/The-Integer-Cache.aspx !

Considering the Flyweight is a GoF Design Pattern, and that it talks about doing this specifically for managing integer objects... this shouldn't be a WTF for most people. It's a trick as old as the hills too. Smalltalk has had it going back decades.

Re: Write a program that makes 2 + 2 = 5

#47

Earlier quoted context omitted.

It's the trigraph ??/ which translates to a \.

The trigraph appears in the file "test.c". If you look at the compilation command, you will see that that is not the file that is actually compiled.

If you compile "test.c" you will see that that isn't relevant issue.

Re: Write a program that makes 2 + 2 = 5

#49
post #36
post #23

Here's mine : $ cat test.c #include int main() { int a = 3; int b = 3; // aren't we supposed to add 2 and 2 ??/ a = 2; b = 2; printf("%d\n", a + b); return 0; } $ gcc -W -Wall -trigraphs test2.c 2>/dev/null $ ./a.out 5 $

Very sneaky. For anyone else wondering why this works, look up trigraphs.

And after looking it up, you better then realise that if you actually ever use them in production code you better have a really good reason, or your colleagues will forever curse your name..

I think the above is probably the first time I've seen trigraphs used in the last 20 years or so.

Re: Write a program that makes 2 + 2 = 5

#50
post #34
post #17

Earlier quoted context omitted.

It's only likely, but not certain that a or b get overwritten. How your stack is laid out is entirely up to the compiler. arr[1] is already out of bounds, however, we don't know for certain what's immediately above the array.

With compiler optimizations turned on, it's almost guaranteed not to happen, because a and b are very likely to be stored in CPU registers.

Or rather, a and b will be constant folded so that the printf call is optimized to "push #4; push ptrFmt; call printf"
Post reply on HN