Help: rational number to decimal with certain digits

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 5
Reputation: jaymeaux77 is an unknown quantity at this point 
Solved Threads: 1
jaymeaux77's Avatar
jaymeaux77 jaymeaux77 is offline Offline
Newbie Poster

Help: rational number to decimal with certain digits

 
0
  #1
Sep 30th, 2009
Hi, I am fairly new at the whole programming racket and I am having trouble with an assignment for my computer science class. I know homework help is discouraged, I read that sticky, but I'm curious if I am missing a fairly simple mistake.

The assignment is to make a program that takes input for a numerator and a denominator of a rational number and then divides that rational number to a certain amount of decimal places. Basically it does long division and multiplies the remainder by 10 a certain number of times specified by the user. So if the user enters 22, 7, and 20 it should print out 3.14...up to 20 digits of the rational number. My code's behavior is simply 3.1111... though.

Here is my code so far (and I am pretty sketchy with the modulo operator %; so I think I have done something wrong with it.)

  1. def rationalnum(n, d, num_iterations):
  2. A = n/d
  3. B = n%d
  4. print A
  5. print '.'
  6. for n in range(num_iterations):
  7. n = B*10
  8. c=n/d
  9. n=n%d
  10. print c
  11.  
  12. def main():
  13. num = input('Please Enter a numerator to the rational number: ')
  14. denom = input('Please Enter a denominator to the rational number: ')
  15. num_iterations = input('Please Enter a number of decimal places to show: ')
  16. rationalnum(num, denom, num_iterations)
  17.  
  18.  
  19. main()

Any suggestions would be greatly appreciated. And I'm sorry to add another lame homework question but I am stumped.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,031
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 290
woooee woooee is online now Online
Veteran Poster

Re: Help: rational number to decimal with certain digits

 
1
  #2
Oct 1st, 2009
Your problem is here
  1. for n in range(num_iterations):
  2. print "n before =", n
  3. n = B*10
  4. print "n after =", n
You use "n" twice and are always multiplying the same number, "B". Something like this should give you the idea of how to go about it. Also, I've introduced divmod which is just another way to get the whole integer and the remainder (not decimal). Your main problem is that you haven't tried including print statements to see what is happening, so can not tell that you are getting the remainder as an integer and not as a float/decimal.
  1. def rationalnum(n, d, num_iterations):
  2. whole, remain = divmod(n, d)
  3. print "divmod =", whole, remain
  4. print "% =", n%d, n, d
  5.  
  6. ## convert to decimal
  7. dec = float(remain) / d
  8. print "dec =", dec
  9.  
  10. ##--- you could also use this to convert
  11. number = float(n) / d
  12. number_int = int(number)
  13. number_decimal = number - number_int
  14.  
  15. for n in range(num_iterations):
  16. dec *= 10
  17. print dec
  18.  
  19. print "\nthe decimal multiplied %d times = %d\n" % \
  20. (num_iterations, int(dec))
  21.  
  22. def main():
  23. num = input('Please Enter a numerator to the rational number: ')
  24. denom = input('Please Enter a denominator to the rational number: ')
  25. num_iterations = input('Please Enter a number of decimal places to show: ')
  26. rationalnum(num, denom, num_iterations)
  27.  
  28. main()
Finally, there is no reason to have the main() function, with one orphaned statement that calls it, unless you want to call it from another program, in which case it should be named something more descriptive than "main".
Last edited by woooee; Oct 1st, 2009 at 1:37 am.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Help: rational number to decimal with certain digits

 
0
  #3
Oct 1st, 2009
Originally Posted by jaymeaux77 View Post
Hi, I am fairly new at the whole programming racket and I am having trouble with an assignment for my computer science class. I know homework help is discouraged ...
We will give you homework help, as long as you show an effort! However, we will not do the homework for you.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 5
Reputation: jaymeaux77 is an unknown quantity at this point 
Solved Threads: 1
jaymeaux77's Avatar
jaymeaux77 jaymeaux77 is offline Offline
Newbie Poster
 
0
  #4
Oct 6th, 2009
Thank you very much for the help. It definitely was more than helpful
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC