About The Contest

The first ever Olympiad of Misguided Geeks contest at Worse Than Failure (or OMGWTF for short) is a new kind of programming contest. Readers are invited to be creative with devising a calculator with the craziest code they can write. One lucky and potentially insane winner will get either a brand new MacBook Pro or comparable Sony VAIO laptop.

Navigation

Advertisements

Entry #100209: Calcohol 2

by Henrik Alsing Pedersen
When I laid eyes upon her, the Skeleton Solution, I knew there was too little to work with. I decided to replace the floats with something else, a custom-made type perhaps. Strings were too easy. That's when I thought: There's only 10 digits so it doesn't take more than 4bits to store them. And 2^4 minus 10 equals 6 additional combinations that could be used for stuff like the decimal seperator.. It was perfect!

A little integer division for indexing, and a little modulo-controlled bitshifting to do the extraction. If index was even (index % 2 == 0) then extract the bits on the left, otherwise extract the bits on the right. Before returning, remember to shift the bits to the right so that the return value always reads 0000xxxx and never xxxx0000.

The sign was made a boolean inspired by the floating point sign flag, but also because it made things easier. In with a xor-operator and you have the sign of A after A=A*B or A=A/B (nearly as simple for addition and substraction):
-------------- --------------
|XOR | 0 1 | |MUL | + - |
-------------- --------------
| 0 | 0 1 | | + | + - |
| 1 | 1 0 | | - | - + |
-------------- --------------

The arithmetics was harsher so instead of slaving away doing 500 not-that-funny lines of code, I chose the way that made this custom-made type completely pointless in a WTFish way: I convert into a float, do the aritmetics, and convert back. Now you can't use the bignum functionality, doesn't save any memory, and waste alot of CPU-time -- lovely (^_^)

The arithmetic operators is collected in a math-array. Each possible character (values between 0 and 255) can be used as an index to the array, which gives an initialized object with a calc function. So math['/'] will give you the object associated with division. This is an attempt to make a flexible system in a very clumsy way (there are 255 similar lines of initiation code that you can't collect in a standard loop, because it all happens in precompiling time, because I'm using templates).

Also, only '+', '-', '*' and '/' are in use, so what happens in case of other characters? Well, the good programmer though that some alternative error handling would be in place, and said to himself: If that value has nothing implemented, then the programmer properly ment some nearby implemented value closest to what he wrote. And so the search bursts off, building a colossal stack of object compositions. Combined an incredible 20KB of memory usage for the trade-off to the flexible system -- nice (^_^)

Still not satisfied? Then let me tell you this. The math-array is only visible inside the sourcefile where the custom-made type has been implemented. The programmer who is actually going to use the custom-made type, will have to call the arithmic functions through the object itself. This has currently been realized with operation-overloading on the tokens +, -, * and /. So the flexible system is infact nothing but an illusion.

Happy coding to all of you..

Download0 Comments