






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)
-Lushi
Link to this Page
- Cases last edited on 30 July 2011 at 2:33 am by r59h132.res.gatech.edu