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 |
|
|
|
pebkac programming question! Feb 1 2008, 01:38 PM
Spectatrix Oh here we go, better example (scroll to the botto... Feb 1 2008, 03:10 PM
pebkac QUOTE (Spectatrix @ Feb 1 2008, 03:10 PM)... Feb 1 2008, 07:44 PM
blaarg QUOTE (pebkac @ Feb 1 2008, 07:44 PM) Thi... Feb 2 2008, 01:13 PM
Jessica Im posting in this thread so I feel smart Feb 1 2008, 03:12 PM
theIncredibleEdibleDuane My advice...pick a new major. Feb 1 2008, 03:20 PM
impala454 interesting project... now I must do it.. ass. Feb 1 2008, 06:10 PM
zetec Derosiers does suck. He was supposed to be my advi... Feb 2 2008, 02:26 AM
Jim QUOTE (zetec @ Feb 2 2008, 02:26 AM) Dero... Feb 2 2008, 11:14 AM
pebkac Meh, I sent Derosiers an email and he basically to... Feb 3 2008, 06:27 PM
impala454 I haven't gotten around to doing this yet (ref... Feb 4 2008, 12:27 AM
zetec RE: programming question! Feb 4 2008, 01:48 PM
impala454 Finally got around to this in some bored time at w... Feb 15 2008, 10:13 AM
Spectatrix Too long. Cliff notes?
(I kid) Feb 15 2008, 10:22 AM
impala454 QUOTE (Spectatrix @ Feb 15 2008, 10:22 AM... Feb 15 2008, 11:30 AM
Spectatrix QUOTE (impala454 @ Feb 15 2008, 11:30 AM)... Feb 15 2008, 11:31 AM
pebkac Interesting. I can see why Desrosiers told me not... Feb 15 2008, 10:29 AM
zetec I was right about to say, why aren't you using... Feb 15 2008, 02:03 PM![]() ![]() |
| Lo-Fi Version | Time is now: 14th August 2026 - 02:50 PM |