User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 422,916 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,323 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 253 | Replies: 2
Reply
Join Date: May 2008
Posts: 6
Reputation: kllera is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kllera kllera is offline Offline
Newbie Poster

simplifing fractions from mod

  #1  
May 22nd, 2008
I'm dividing integers and making it a temporary double so i can get it in a decimal.
To have it with a fraction i just divide the numbers and cout the answer with the mod answer "/" num2. I want to display both the fraction and in decimal, so..

for 10 divided by 3, 3 1/3 is good, but 125 divided by 75 yeilds "1 50/75". How can i get if the simplify the fractions? i.e. " 1 2/3"
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,117
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 929
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: simplifing fractions from mod

  #2  
May 22nd, 2008
just call modf()
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: simplifing fractions from mod

  #3  
May 22nd, 2008
That won't help to reduce/simplify fractions so much as using the Greatest Common Divisor.

Here it is with the Euclidean algorithm, unrolled one iteration.
  1. int GCD( int a, int b )
  2. {
  3. while (true)
  4. {
  5. a = a % b;
  6. if (a == 0) return b;
  7.  
  8. b = b % a;
  9. if (b == 0) return a;
  10. }
  11. }

So, to simplify a fraction, simply:
  1. void simplify( int& n, int& d )
  2. {
  3. int gcd = GCD( n, d );
  4. n /= gcd;
  5. d /= gcd;
  6.  
  7. // If negative, make it the numerator.
  8. if (d < 0)
  9. {
  10. n = -n;
  11. d = -d;
  12. }
  13. }

Hope this helps.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:36 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC