Member Avatar for leegeorg07

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)')

Recommended Answers

All 7 Replies

>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()

Built-in function round() and string formatting.

>>> round(math.pi, 5)
3.1415899999999999
>>> "%0.5f" % math.pi
'3.14159'
>>> sig_digits = 6
>>> num = 123.456789
>>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num)
'123.457'
>>> num = 1.23456789
>>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num)
'1.23457'
>>> num = 1234567890
>>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num)
'1234567890'
>>>

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.

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...

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...

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
>>>

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
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.