>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
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
To represent a floating point number in a formatted form for print, you have to store the result as a string in your list:
# representing print formatted floats in a list
list_floats = [1/3.0, 1/6.0, 3.124567, 5.01]
print list_floats
"""
my output (note the typical float representation problem) -->
[0.33333333333333331, 0.16666666666666666, 3.1245669999999999, 5.0099999999999998]
"""
# create a new list of floats using round()
list_floats2 = []
for n in list_floats:
list_floats2.append(round(n, 5))
print list_floats2
"""
my output (notice we are still dealing with floats) -->
[0.33333000000000002, 0.16667000000000001, 3.1245699999999998, 5.0099999999999998]
"""
# to make round() stick for your print
# you have to store the result as a string
list_floats3 = []
for n in list_floats:
list_floats3.append(str(round(n, 5)))
print list_floats3
"""
my output -->
['0.33333', '0.16667', '3.12457', '5.01']
"""
# or ...
list_floats4 = []
for n in list_floats:
list_floats4.append("%0.5f" % n)
print list_floats4
"""
my output -->
['0.33333', '0.16667', '3.12457', '5.01000']
"""
The behaviour of a floating point number you notice here is typical of many computer languages.
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
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...
techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9
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:
import math
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...
techie1991
Junior Poster in Training
72 posts since Feb 2010
Reputation Points: 36
Solved Threads: 9
Doing 10/3 in 20000 decimals:
>>> base=10**20000
>>> units,decimals = divmod(10*base/3,base) ##10/3 long number
>>> print str(units)+'.'+str(decimals)
For square root you must use own function:
>>> import math
>>> x=(2*base,base)
>>> math.sqrt(x[0])
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
math.sqrt(x[0])
OverflowError: long int too large to convert to float
>>>
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
Start experimenting with Python module decimal ...
# high accuracy calculations with Python module decimal
# tested with Python 3.1.2
import decimal as dc
# regular float calculation
sqrt1 = 2 ** 0.5
print(type(sqrt1))
print(sqrt1)
print(sqrt1 * sqrt1)
# set decimal precition
dc.getcontext().prec = 60
# using module decimal
sqrt2 = dc.Decimal(2) ** dc.Decimal('0.5')
print(type(sqrt2))
print(sqrt2)
print(sqrt2 * sqrt2)
"""my result -->
<class 'float'>
1.41421356237
2.0
<class 'decimal.Decimal'>
1.41421356237309504880168872420969807856967187537694807317668
2.00000000000000000000000000000000000000000000000000000000000
"""
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417