943,562 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 3565
  • Python RSS
Dec 13th, 2008
0

Rounding to n digits

Expand Post »
Hi again i have programmed a thing that asks for 2 numbers and does a series of things to it.But i want to round the square-rooted numbers to about 6 digits e.g. 5.12345. How can this be done?

Here is my code:
from math import sqrt
running = 'Y'

while 'Y' in running or 'y' in running:
    #defining numbers
    startn = float(raw_input('What is the starting number'))
    startn2 = startn
    endn = float(raw_input('What is the ending number?'))
    endn2 = endn
    #defining lists
    num = []
    nums = []
    numsq = []
    while startn2 != (endn2+1):
        #just the numbers
        a = int(startn2)
        num.append(a)
        startn2+=1
    startn2 = startn
    print 'numbers without changing are:', num
    while startn2 != (endn2+1):
        #numbers squared
        a = int(startn2*startn2)
        nums.append(a)
        startn2+=1
    startn2 = startn
    print 'numbers squared are:', nums
    while startn2 != (endn2+1):
        #numbers square-rooted
        #Here is my problem, i want to simplify it to about 6 digits e.g. 5.12345
        a = sqrt(startn2)
        numsq.append(a)
        startn2+=1
    startn2 = startn
    print 'numbers square-rooted are:', numsq
    running = raw_input('Try another set? (Y or N)')
Similar Threads
Reputation Points: 35
Solved Threads: 32
Posting Pro in Training
leegeorg07 is offline Offline
428 posts
since Jul 2008
Dec 13th, 2008
0

Re: Rounding to n digits

>But i want to round the square-rooted numbers to about 6 digits e.g. 5.12345. How can this be done?

Take a look at round()
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Dec 13th, 2008
0

Re: Rounding to n digits

Built-in function round() and string formatting.
Python Syntax (Toggle Plain Text)
  1. >>> round(math.pi, 5)
  2. 3.1415899999999999
  3. >>> "%0.5f" % math.pi
  4. '3.14159'
  5. >>> sig_digits = 6
  6. >>> num = 123.456789
  7. >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num)
  8. '123.457'
  9. >>> num = 1.23456789
  10. >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num)
  11. '1.23457'
  12. >>> num = 1234567890
  13. >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num)
  14. '1234567890'
  15. >>>
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Dec 14th, 2008
0

Re: Rounding to n digits

To represent a floating point number in a formatted form for print, you have to store the result as a string in your list:
python Syntax (Toggle Plain Text)
  1. # representing print formatted floats in a list
  2.  
  3. list_floats = [1/3.0, 1/6.0, 3.124567, 5.01]
  4.  
  5. print list_floats
  6.  
  7. """
  8. my output (note the typical float representation problem) -->
  9. [0.33333333333333331, 0.16666666666666666, 3.1245669999999999, 5.0099999999999998]
  10. """
  11.  
  12. # create a new list of floats using round()
  13. list_floats2 = []
  14. for n in list_floats:
  15. list_floats2.append(round(n, 5))
  16.  
  17. print list_floats2
  18.  
  19. """
  20. my output (notice we are still dealing with floats) -->
  21. [0.33333000000000002, 0.16667000000000001, 3.1245699999999998, 5.0099999999999998]
  22. """
  23.  
  24. # to make round() stick for your print
  25. # you have to store the result as a string
  26. list_floats3 = []
  27. for n in list_floats:
  28. list_floats3.append(str(round(n, 5)))
  29.  
  30. print list_floats3
  31.  
  32. """
  33. my output -->
  34. ['0.33333', '0.16667', '3.12457', '5.01']
  35. """
  36.  
  37. # or ...
  38. list_floats4 = []
  39. for n in list_floats:
  40. list_floats4.append("%0.5f" % n)
  41.  
  42. print list_floats4
  43.  
  44. """
  45. my output -->
  46. ['0.33333', '0.16667', '3.12457', '5.01000']
  47. """
The behaviour of a floating point number you notice here is typical of many computer languages.
Last edited by sneekula; Dec 14th, 2008 at 1:44 pm.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Apr 25th, 2010
0

Increase digits

I was trying to make a program that gives very accurate answers to problems. The accuracy I want is of like 20000 terms.
For eg. 10/3 is written as 3.33333333333333333...20,000 times.
So, is it possible in Python?
Also, please answer in reference to Python 3.0, if possible...
Reputation Points: 36
Solved Threads: 9
Junior Poster in Training
techie1991 is offline Offline
72 posts
since Feb 2010
Apr 25th, 2010
0

Increase digits

I was trying to make a program that gives very accurate answers to problems. The accuracy I want is of like 20000 terms.
For eg. 10/3 is written as 3.33333333333333333...20,000 times.
So, is it possible in Python?
The code I tried to get square root of 2 is:
Python Syntax (Toggle Plain Text)
  1. import math
  2. print(math.sqrt(2))
which gives:
1.4142135623730951... which does not fulfill my need.
Also, please answer in reference to Python 3.0, if possible...
Reputation Points: 36
Solved Threads: 9
Junior Poster in Training
techie1991 is offline Offline
72 posts
since Feb 2010
Apr 26th, 2010
0
Re: Rounding to n digits
Doing 10/3 in 20000 decimals:

Python Syntax (Toggle Plain Text)
  1. >>> base=10**20000
  2. >>> units,decimals = divmod(10*base/3,base) ##10/3 long number
  3. >>> print str(units)+'.'+str(decimals)

For square root you must use own function:
Python Syntax (Toggle Plain Text)
  1. >>> import math
  2. >>> x=(2*base,base)
  3. >>> math.sqrt(x[0])
  4.  
  5. Traceback (most recent call last):
  6. File "<pyshell#12>", line 1, in <module>
  7. math.sqrt(x[0])
  8. OverflowError: long int too large to convert to float
  9. >>>
Last edited by pyTony; Apr 26th, 2010 at 2:56 am.
Featured Poster
Reputation Points: 690
Solved Threads: 748
Industrious Poster
pyTony is offline Offline
4,200 posts
since Apr 2010
Apr 26th, 2010
1
Re: Rounding to n digits
Start experimenting with Python module decimal ...
Python Syntax (Toggle Plain Text)
  1. # high accuracy calculations with Python module decimal
  2. # tested with Python 3.1.2
  3.  
  4. import decimal as dc
  5.  
  6. # regular float calculation
  7. sqrt1 = 2 ** 0.5
  8.  
  9. print(type(sqrt1))
  10. print(sqrt1)
  11. print(sqrt1 * sqrt1)
  12.  
  13. # set decimal precition
  14. dc.getcontext().prec = 60
  15.  
  16. # using module decimal
  17. sqrt2 = dc.Decimal(2) ** dc.Decimal('0.5')
  18.  
  19. print(type(sqrt2))
  20. print(sqrt2)
  21. print(sqrt2 * sqrt2)
  22.  
  23. """my result -->
  24. <class 'float'>
  25. 1.41421356237
  26. 2.0
  27. <class 'decimal.Decimal'>
  28. 1.41421356237309504880168872420969807856967187537694807317668
  29. 2.00000000000000000000000000000000000000000000000000000000000
  30. """
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Drawing Analogue Clock Math
Next Thread in Python Forum Timeline: Deleting Lines in a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC