Feb 1 2008, 01:38 PM
Post
#1
|
|
![]() From Atlantis to Interzone Group: Global Moderators Posts: 2,512 Joined: 23-February 06 From: Somewhere in space and time Member No.: 65 |
Ok, so I'm taking Desrosiers (yeah, he sucks) for CS 1412 and I'm working on a programming assignment right now (in C#).
What I basically have to do is create a class that stores a fraction. One of the things it's supposed to do is take a floating point number and convert that to a fraction. What my class basically does is round the floating point number to 5 decimal places, then multiplies it by 100,000. It then uses the resulting integer as the numerator and 100,000 as the denominator and simplifies that. That works fine for the most part, but what I would also like to be able to do is have it recognize .33333 as 1/3, .66667 as 2/3, 1.33333 as 4/3, etc. I can take the modulus of the floating point number and see if the remainder is zero, but I think that way would be really prone to rounding errors. I don't necessarily have to implement that (he said that the method can have limitations as long as we document them), but it would be nice if I could do that. Does anyone have any advice? -------------------- Holy shit, pebkac, you're awesome! "Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind." - Theodor Seuss Geisel (AKA Dr. Seuss) "An idea that is not dangerous is unworthy of being called an idea at all." - Oscar Wilde |
|
|
|
![]() |
Feb 1 2008, 02:03 PM
Post
#2
|
|
|
Group: Admin Posts: 6,906 Joined: 22-February 06 From: Austin Member No.: 9 |
If it's got a repeating digit (e.g. 1/3 = 0.333, 2/3 = 0.666, 1/6 = 0.1666), etc:
Call the floating point number x. Multiply it by 10 to shift the decimal point over one. Call this y. y-x will get rid of the repeating part of the floating point number. Thus you'll have 9x = some rational fraction. Then you can plug this into your remaining stuff. Rational fraction * 100000 as your numerator, 900000 as your denominator. Simplify. Got that general algorithm from here: http://www.webmath.com/dec2fract.html Plug in a decimal number to see a sample work-through. Edit: This is ONLY for irrational numbers with a single repeating digit. If it's a repeating sequence (e.g. 1/11 = 0.090909...) then you'd need to multiply by a power of 10 equal to the number of digits that are repeated. 100x for two digits (like 1/11), 1000x for three digits, etc. Might be more complicated than you want to implement. This post has been edited by Spectatrix: Feb 1 2008, 03:06 PM -------------------- |
|
|
|
Feb 1 2008, 02:32 PM
Post
#3
|
|
|
Let's Bother Snape!!! Group: Members Posts: 1,598 Joined: 22-February 06 From: Albuquerque, NM Member No.: 10 |
If it's got a repeating digit (e.g. 1/3 = 0.333, 2/3 = 0.666, 1/6 = 0.1666), etc: Call the floating point number x. Multiply it by 10 to shift the decimal point over one. Call this y. y-x will get rid of the repeating part of the floating point number. Thus you'll have 9x = some rational fraction. Then you can plug this into your remaining stuff. Rational fraction * 10000 as your numerator, 90000 as your denominator. Simplify. Got that general algorithm from here: http://www.webmath.com/dec2fract.html Plug in a decimal number to see a sample work-through. That program is borked. Put in an irrational number and see what happens. My example was .4657101685 It appears that there is a certain threshold the program can take (which is understandable). However, the programmers should have seen this and then had the output be something different saying that the limit has been reached instead of giving me -(2/-4) as the answer. -------------------- ![]() |
|
|
|
Feb 1 2008, 02:57 PM
Post
#4
|
|
|
Group: Members Posts: 519 Joined: 27-June 07 Member No.: 1,288 |
That program is borked. Put in an irrational number and see what happens. My example was .4657101685 It appears that there is a certain threshold the program can take (which is understandable). However, the programmers should have seen this and then had the output be something different saying that the limit has been reached instead of giving me -(2/-4) as the answer. That's what I said. -------------------- Fuckmuffin. That word and muffintop are the two coolest things I've ever seen on this place. What would happen if a fuckmuffin fucked a chick with a muffintop? That's a lot of muffins. A lot of motherfuffin muffuckins.
-TTULOW2 |
|
|
|
Feb 1 2008, 03:10 PM
Post
#5
|
|
|
Group: Admin Posts: 6,906 Joined: 22-February 06 From: Austin Member No.: 9 |
Oh here we go, better example (scroll to the bottom): http://math2.org/math/general/arithmetic/fradec.htm
QUOTE Need to convert a repeating decimal to a fraction? Follow these examples:
Note the following pattern for repeating decimals: 0.22222222... = 2/9 0.54545454... = 54/99 0.298298298... = 298/999 Division by 9's causes the repeating pattern. Note the pattern if zeros preceed the repeating decimal: 0.022222222... = 2/90 0.00054545454... = 54/99000 0.00298298298... = 298/99900 Adding zero's to the denominator adds zero's before the repeating decimal. To convert a decimal that begins with a non-repeating part, such as 0.21456456456456456..., to a fraction, write it as the sum of the non-repeating part and the repeating part. 0.21 + 0.00456456456456456... Next, convert each of these decimals to fractions. The first decimal has a divisor of power ten. The second decimal (which repeats) is convirted according to the pattern given above. 21/100 + 456/99900 Now add these fraction by expressing both with a common divisor 20979/99900 + 456/99900 and add. 21435/99900 Finally simplify it to lowest terms 1429/6660 and check on your calculator or with long division. = 0.2145645645... This post has been edited by Spectatrix: Feb 1 2008, 03:14 PM -------------------- |
|
|
|
Feb 1 2008, 03:12 PM
Post
#6
|
|
![]() New son Donovan Charles Mummert born July 17, 2008 Group: Members Posts: 8,635 Joined: 22-February 06 From: Port Wentworth, GA Member No.: 15 |
Im posting in this thread so I feel smart
|
|
|
|
Feb 1 2008, 03:20 PM
Post
#7
|
|
|
Group: Members Posts: 1,566 Joined: 23-February 06 From: Houston, TX Member No.: 66 |
My advice...pick a new major.
|
|
|
|
Feb 1 2008, 06:10 PM
Post
#8
|
|
![]() Group: Members Posts: 10,620 Joined: 23-February 06 From: Houston, TX Member No.: 48 |
interesting project... now I must do it.. ass.
|
|
|
|
Feb 1 2008, 07:44 PM
Post
#9
|
|
![]() From Atlantis to Interzone Group: Global Moderators Posts: 2,512 Joined: 23-February 06 From: Somewhere in space and time Member No.: 65 |
Oh here we go, better example (scroll to the bottom): http://math2.org/math/general/arithmetic/fradec.htm This is all well and good, but first I have to get the program to recognize that the number is repeating. I could probably analyze the number digit by digit, but as this isn't the primary focus of the project, I don't want to go into that much depth. I could also see if the remainder is 0 if I divide by a set group of integers, but that may not be of much help as there will probably be a significant number of rounding errors. -------------------- Holy shit, pebkac, you're awesome! "Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind." - Theodor Seuss Geisel (AKA Dr. Seuss) "An idea that is not dangerous is unworthy of being called an idea at all." - Oscar Wilde |
|
|
|
Feb 2 2008, 02:26 AM
Post
#10
|
|
![]() Retired Funk-bringer Group: Moderators Posts: 2,656 Joined: 22-February 06 From: Dallas Member No.: 14 |
Derosiers does suck. He was supposed to be my advisor, but he was NEVER in his office, EVER. Even during his hours. Every time i've had to talk to Sherri instead. (Sherri rocks.)
-------------------- WAIT. I'm not finished.
|
|
|
|
Feb 2 2008, 11:14 AM
Post
#11
|
|
|
Let's Bother Snape!!! Group: Members Posts: 1,598 Joined: 22-February 06 From: Albuquerque, NM Member No.: 10 |
Derosiers does suck. He was supposed to be my advisor, but he was NEVER in his office, EVER. Even during his hours. Every time i've had to talk to Sherri instead. (Sherri rocks.) This is truth. Super truth. Also, regarding the repeating number bit, you should make it an array, then, and do some fancy array manipulation. -------------------- ![]() |
|
|
|
Feb 2 2008, 01:13 PM
Post
#12
|
|
![]() Group: Members Posts: 926 Joined: 2-May 07 Member No.: 1,015 |
This is all well and good, but first I have to get the program to recognize that the number is repeating. That's easy to do with a loop depending on how you handle the number. shrug* Also, regarding the repeating number bit, you should make it an array, then, and do some fancy array manipulation. this is what I mean when I say "depending on how you handle the number" -------------------- ![]() |
|
|
|
Feb 3 2008, 06:27 PM
Post
#13
|
|
![]() From Atlantis to Interzone Group: Global Moderators Posts: 2,512 Joined: 23-February 06 From: Somewhere in space and time Member No.: 65 |
Meh, I sent Derosiers an email and he basically told me not to worry about it. Oh well, one less thing I have to worry about.
-------------------- Holy shit, pebkac, you're awesome! "Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind." - Theodor Seuss Geisel (AKA Dr. Seuss) "An idea that is not dangerous is unworthy of being called an idea at all." - Oscar Wilde |
|
|
|
Feb 4 2008, 12:27 AM
Post
#14
|
|
![]() Group: Members Posts: 10,620 Joined: 23-February 06 From: Houston, TX Member No.: 48 |
I haven't gotten around to doing this yet (reformatted back to xp from vista, super bowl, etc etc), but if you want to recognize a pattern, use the Regex class.
|
|
|
|
Feb 4 2008, 01:48 PM
Post
#15
|
|
![]() Retired Funk-bringer Group: Moderators Posts: 2,656 Joined: 22-February 06 From: Dallas Member No.: 14 |
-------------------- WAIT. I'm not finished.
|
|
|
|
Feb 15 2008, 10:13 AM
Post
#16
|
|
![]() Group: Members Posts: 10,620 Joined: 23-February 06 From: Houston, TX Member No.: 48 |
Finally got around to this in some bored time at work. Works pretty damn good so far. Feel free to break it and let me know what did it.
CODE using System;
using System.Collections.Generic; using System.Text; //------------------------- using System.Text.RegularExpressions; using System.Collections; namespace FractionConvert { class Program { static void Main(string[] args) { Fraction f = new Fraction(); double d = 0; Double.TryParse(args[0], out d); Fraction.TryParse(d, out f); Console.WriteLine(f.ToString()); } } class Fraction { int m_nNumerator; int m_nDenominator; bool m_bNegative; double m_dDecimal; public Fraction() { m_nNumerator = 0; m_nDenominator = 0; m_dDecimal = 0; m_bNegative = false; } public Fraction(double DecimalToConvert) { m_nNumerator = 0; m_nDenominator = 0; m_dDecimal = DecimalToConvert; m_bNegative = false; ParseFraction(); } public static bool TryParse(double DecimalInput, out Fraction FractionOutput) { try { FractionOutput = new Fraction(DecimalInput); } catch (Exception) { FractionOutput = new Fraction(0); return false; } return true; } public override string ToString() { if(m_bNegative) return string.Format("-{0}/{1}", m_nNumerator, m_nDenominator); else return string.Format("{0}/{1}", m_nNumerator, m_nDenominator); } private void ParseFraction() { if (m_dDecimal == 0) { m_nNumerator = 0; m_nDenominator = 1; // this could be any number really return; } string Whole = ""; string Decimal = m_dDecimal.ToString(); // strip off the minus sign if there is one if (Decimal[0] == '-') { m_bNegative = true; Decimal = Decimal.Substring(1, Decimal.Length - 1); } // split out the whole number, no need to parse that if (Decimal.Contains(".")) { Whole = Decimal.Split('.')[0]; Decimal = Decimal.Split('.')[1]; } string Pattern = GetPattern(Decimal); int ParseInt = 0; int Divisor = 1; ArrayList Divisors = new ArrayList(); if (!Int32.TryParse(Pattern, out ParseInt)) { throw new Exception("Not a valid decimal number"); return; } while (ParseInt > Divisor) { if (ParseInt % Divisor == 0) Divisors.Insert(0, Divisor); Divisor++; } int WholeNumber = 0; Int32.TryParse(Whole, out WholeNumber); // if we only have one divisor, obviously it's 1 if (Divisors.Count == 1) { m_nNumerator = 1; m_nDenominator = ParseInt; return; } foreach (int n in Divisors) { if (10 % n == 0) { if(Pattern.Length <= 1) m_nDenominator = 10 * n / ParseInt; else m_nDenominator = (Pattern.Length - 1) * (int)Math.Pow(10, (double)n+1) / ParseInt; if (WholeNumber > 0) m_nNumerator = m_nDenominator * WholeNumber + n; else m_nNumerator = n; break; } } } private string GetPattern(string Decimal) { // the accuracy can't be any better than half the number of digits! //if (m_nAccuracy > Decimal.Length / 2) int Accuracy = Decimal.Length / 2; string Pattern = ""; int NumRepeats = 0; int MeasuredAccuracy = 0; int MaxRepeats = 0; for (int i = 1; i <= Accuracy; i++) { NumRepeats = 0; Pattern = Decimal.Substring(0, i); for (int j = 0; j <= Decimal.Length - Accuracy; j += Pattern.Length) { if (Decimal.Substring(j, Pattern.Length) == Pattern) NumRepeats++; else break; } if (NumRepeats >= MaxRepeats) { MaxRepeats = NumRepeats; MeasuredAccuracy = Pattern.Length; } } // if we didn't find the pattern more than once, there is no pattern if (MaxRepeats <= 1) return Decimal; else return Decimal.Substring(0, MeasuredAccuracy); } } } |
|
|
|
Feb 15 2008, 10:22 AM
Post
#17
|
|
|
Group: Admin Posts: 6,906 Joined: 22-February 06 From: Austin Member No.: 9 |
Too long. Cliff notes?
-------------------- |
|
|
|
Feb 15 2008, 10:29 AM
Post
#18
|
|
![]() From Atlantis to Interzone Group: Global Moderators Posts: 2,512 Joined: 23-February 06 From: Somewhere in space and time Member No.: 65 |
Interesting. I can see why Desrosiers told me not to worry about it now.
At any rate, it's pretty helpful to know how to actually do that. -------------------- Holy shit, pebkac, you're awesome! "Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind." - Theodor Seuss Geisel (AKA Dr. Seuss) "An idea that is not dangerous is unworthy of being called an idea at all." - Oscar Wilde |
|
|
|
Feb 15 2008, 11:30 AM
Post
#19
|
|
![]() Group: Members Posts: 10,620 Joined: 23-February 06 From: Houston, TX Member No.: 48 |
|
|
|
|
Feb 15 2008, 11:31 AM
Post
#20
|
|
|
Group: Admin Posts: 6,906 Joined: 22-February 06 From: Austin Member No.: 9 |
DO IT WITH LESS CODE THEN BITCH oh and actually, didn't use regexes, so that "Using Regexes" line at the top can be removed. OK MAYBE I WILL! -------------------- |
|
|
|
Feb 15 2008, 02:03 PM
Post
#21
|
|
![]() Retired Funk-bringer Group: Moderators Posts: 2,656 Joined: 22-February 06 From: Dallas Member No.: 14 |
I was right about to say, why aren't you using regular expressions again?
-------------------- WAIT. I'm not finished.
|
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 14th August 2026 - 02:50 PM |