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.



Entry #100265: FerronCalc

by Dennis Ferron
Please see the Build Instructions document in the project directory. There are also various documents in the folder explaining in more detail what I briefly outline here:

My calculator is a caricature of C++. I tried to capture every aspect of the C++ language that leads to its reputation for over-engineering, and then pushed each feature as far as it would go.

First off: C++'s penchant for creating inheritance structures that fossilize naturally. What a perfect fit for a mathematical subject that has existed for thousands of years: numbers. Instead of representing numbers with int values, I built a class hierarchy with one class for every number from 0 to 99. I exploited the natural relationships the numbers have with each other, and encoded that into the inheritance hierarchy: except for prime numbers, all other numbers are defined by inheriting a template with the types of all the prime factors the number has! And to make matters especially fun, every number inherits from a class template that causes it to include every number previous to it, nested one inside another like Russian dolls. An object of NinetyNine contains a NinetyEight, which contains a NinetySeven, and so on. This allowed me to write recursive template functions that operate over every class from 99 to 0.

With only the numbers 0 to 99, the way I handle larger numbers (and even fractions) is by stringing together many smaller numbers with other "special" numbers that represent sums, products, or ratios of two or more other numbers. When you press "375" on the calculator, here is how the calculator represents those key presses:

3 x 10 + 7
3 x 10 + 7 x 10 + 5

Since an operation has a numerical "answer", I represented operations as special "numbers". These "numbers" point to other operations or other actual numbers. The calculator builds a tree in memory representing the equation you are calculating, and does not reduce that tree to any intermediate form: as long as you keep using the calculator, the tree keeps getting bigger. The answer you see displayed is the result of walking the entire tree to come up with a value. The tree isn't cleared out until you hit the clear ('C') button.

Speaking of clear, the another annoying "feature" of C++ is its lack of garbage collection. I once read about something called the "Boehm-Demers-Weiser conservative garbage collector" that works by scanning memory and only collecting items which it is absolutely sure are safe to delete. Well, I present to you: the Ferron Greedy Garbage Collector. Exactly the opposite of Boehm's algorithm, I included a garbage collector with this calculator that collects everything, whether it's still in use or not! Hope you don't have anything important allocated: you have to make a special function call to STOP the garbage collector from deleting it.

Finally, the operation of the calculator itself is very unique. I implemented the state machine design pattern - nothing unusual there - but the way I did it is unlike anything else I am aware of.

I made the calculator inherit from a different base class for each state. The base classes all have the same function names. I used a templated call-back proxy to select a different base class's version of the function depending on what state the calculator is in. The functions are not virtual. I call these "reverse-virtual" functions because with a virtual function, you have a pointer to the base class and the runtime object selects the appropriate derived function to map that to; with reverse-virtual functions, you have a pointer to the DERIVED class, and the template selects which BASE version of a non-virtual function to call.

Download0 Comments