View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

String asFraction

Adding String asFraction functionality to SQUEAK.

During the stock ticker assignment, the need arose to convert a fraction (in the form of a string) to an actual Fraction (object). We didnŪt want to store the data as a string because a String object would be practically useless for anything but displaying it in the exact same form we got it in. With a Fraction object, however, we have an actual number and are therefore able to process it in any way we want. Once it is in number form, we can use it to generate statistical information, compute averages, minimums and maximums, generate graphs, and any number of other types of outputs. With a string we are only able to parrot back our input. This mad it evident that we would have to convert the Strings parsed from the web page into Fractions that we can analyze.

My first reaction was to add functionality to the String class by just adding the function asFraction. My group members said that I had žDesign Arrogance,Ó believing that I could go in and change the basic source code of Squeak. I argued that that is the beauty of Squeak÷you want extra functionality? Go ahead and add it yourself! I also argued that I wasnŪt changing existing code, but rather adding new functionality, so my code could not hurt any other components of squeak that may be calling String asFraction already. We also considered subclassing string and adding the functionality there, but decided that that wouldnŪt help either because the objects we were returning were really strings and should be treated as such÷we shouldnŪt need to convert them to some other class to process them. Finally I won and they let me write it in String.

I coded it by breaking the fraction into a numerator and a denominator string and converting them to numbers. Once I had 2 numbers, I could build a Fraction. I also need to do a workaround to allow converting decimal fractions. When I was done, it still had strange behavior; for many numbers it still didnŪt work÷.for example ž-0.375 asFractionÓ should return ž(-3/8)Ó but instead it returns: ž(-1688849860263938/4503599627370501)Ó. It took a long time to hunt downth problem, but I discovered that just calling asFration calls žasApproximateFractionÓ instead of žasTrueFractionÓ. žasApproximateFractionÓ is faster, but not always correct÷Ó asTrueFractionÓ always gets the correct value. After I fixed this it worked much more accurately.

Missing File (/cs2340/uploads/asFraction.cs)


Link to this Page