Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>C99</h2> <p><strong>Number of characters: 239</strong> (But see below for <strong>209</strong>)</p> <p>compressed function:</p> <pre><code>#define S while(*e==32)++e #define F float F strtof();char*e;F v();F g(){S;return*e++-40?strtof(e-1,&amp;e):v();}F v(){F b,a=g();for(;;){S;F o=*e++;if(!o|o==41)return a;b=g();a=o==43?a+b:o==45?a-b:o==42?a*b:a/b;}}F f(char*x){e=x;return v();} </code></pre> <p>decompressed function:</p> <pre><code>float strtof(); char* e; float v(); float g() { while (*e == ' ') ++e; return *e++ != '(' ? strtof(e-1, &amp;e) : v(); } float v() { float b, a = g(); for (;;) { while (*e == ' ') ++e; float op = *e++; if (op == 0 || op == ')') return a; b = g(); a = op == '+' ? a + b : op == '-' ? a - b : op == '*' ? a * b : a / b; } } float eval(char* x) { e = x; return v(); } </code></pre> <p>Function is not re-entrant.</p> <p><strong>EDIT from Chris Lutz</strong>: I hate to trample on another man's code, but here is a <strong>209</strong>-character version:</p> <pre><code>#define S for(;*e==32;e++) #define X (*e++-40?strtof(e-1,&amp;e):v()) float strtof();char*e;float v(){float o,a=X;for(;;){S;o=*e++;if(!o|o==41)return a;S;a=o-43?o-45?o-42?a/X:a*X:a-X:a+X;}} #define f(x) (e=x,v()) </code></pre> <p>Readable (well, not really very readable, but decompressed):</p> <pre><code>float strtof(); char *e; float v() { float o, a = *e++ != '(' ? strtof(e - 1, &amp;e) : v(); for(;;) { for(; *e == ' '; e++); o = *e++; if(o == 0 || o==')') return a; for(; *e == ' '; e++); // I have no idea how to properly indent nested conditionals // and this is far too long to fit on one line. a = o != '+' ? o != '-' ? o != '*' ? a / (*e++ != '(' ? strtof(e - 1, &amp;e) : v()) : a * (*e++ != '(' ? strtof(e - 1, &amp;e) : v()) : a - (*e++ != '(' ? strtof(e - 1, &amp;e) : v()) : a + (*e++ != '(' ? strtof(e - 1, &amp;e) : v()); } } #define f(x) (e = x, v()) </code></pre> <p>Yeah, <code>f()</code> is a macro, not a function, but it works. The readable version has some of the logic rewritten but not reordered (like <code>o != '+'</code> instead of <code>o - '+'</code>), but is otherwise just an indented (and preprocessed) version of the other one. I keep trying to simplify the <code>if(!o|o==41)return a;</code> part into the <code>for()</code> loop, but it never makes it shorter. I still believe it can be done, but I'm done golfing. If I work on this question anymore, it will be in <a href="http://en.wikipedia.org/wiki/Brainfuck" rel="nofollow noreferrer">the language that must not be named</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload