When (X+1==X) is true or how Friday Free Board Quiz became Brainstorm


Image Each Friday we run Quiz on Twitter and give away one of our boards. This runs for more than year now and it very popular. Usually the questions I ask there are related to Hardware to make balance with the Weekend Programming Challenge. Last Friday the question was: “define X in such way that (X+1 == X) is true” my idea was that if X is big enough due to the computer arithmetic precision the statement above will be evaluated as true. so my answer was: #define X 1.0/0.0 The Quiz runs for one hour and for my big surprise this became real brainstorm and I got many correct answers which were not like mine: #define X FLT_MAX float x = 1e20 double X = INFINITY; int a() {static int x=0; return x++;} #define X a() #define X 1|1 #define X 1||1 #define X std::numeric_limits<double>::infinity() var X= 1.7976931348623157E+10308 #define x=log(0) #define X 2 && 1 #define X 1,1 well done! … although I still try to figure out why #define X 1,1 works🙂

2 Comments (+add yours?)

  1. kasimir
    Aug 26, 2013 @ 12:15:52

    Never new this could be used outside for-loop headers… obviously it can🙂
    http://en.wikipedia.org/wiki/Comma_operator

    Reply

  2. Frank L.A.
    Aug 26, 2013 @ 14:35:29

    AFAIR, the comma operator evaluates both sides (expressions) and returns the right hand side value as result. Therefore, with #define X 1,1 for instance if(X+1==X) expands into if(1,1+1==1,1), which evaluates to if(1,2==1,1) and further if(1,0,1), returning the rightmost 1 as end result, which is interpreted as the boolean true.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: