Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
August 26th, 2004, 09:25 PM
 khiloa
Registered User

Join Date: Aug 2004
Posts: 18
Time spent in forums: 2 h 13 m 38 sec
Reputation Power: 0
square root precision

I saw a C++ program the other day that asked for a number and found the square root to as many decimal places as you told it. I couldn't get it to compile, but since I am learning python I thought I would try to write it in python..

It also timed how long it took to calculate the number. So I want to make a python program that does this:

Number? 3
Precision? 5

Time: 3 seconds

obviously made up those numbers and it for sure wouldn't take 3 seconds to find the square root of three to 5 decimal places. :P

So anyone want to tell me how to set the precision and create a timer? Thanks!!

#2
August 27th, 2004, 02:27 AM
 gen_rec
Registered User

Join Date: Jul 2004
Posts: 10
Time spent in forums: 13 m 48 sec
Reputation Power: 0
Code:
```A = 8.251 # sqrt(A)
P = 45    # precition

e = long(10**P)
a = long(A*e)
x = a
for _ in range(20):
x_ = (x+a*e/x)/2
if x_ == x:
print x # x/e
break
else:
x = x_

#
# test
#
import math
print math.sqrt(A)```

#3
August 27th, 2004, 02:30 AM
 gen_rec
Registered User

Join Date: Jul 2004
Posts: 10
Time spent in forums: 13 m 48 sec
Reputation Power: 0
Code:
```import time

cur = time.time()

# do something...
for _ in xrange(1000000): a = 12.565/12312.2313 # for example

print 'time:', time.time()-cur, 'sec'```

#4
August 27th, 2004, 02:39 AM
 gen_rec
Registered User

Join Date: Jul 2004
Posts: 10
Time spent in forums: 13 m 48 sec
Reputation Power: 0
I've mistaken!
Don't use FOR _ IN RANGE!!!

Try better:
Code:
```
A = 200 # sqrt(A)
P = 80    # precition

e = long(10**P)
a = long(A*e)
x = a
while True:
x_ = (x+a*e/x)/2
if x_ == x:
print x # x/e
break
else:
x = x_

#
# test
#
import math
print math.sqrt(A) ```

#5
August 27th, 2004, 02:29 PM
 Strike
Contributing User

Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
FYI: you shouldn't expect any decent precision with floating point math in Python
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

#6
August 27th, 2004, 04:06 PM
 jim mcnamara
......@.........

Join Date: Jun 2004
Posts: 1,345
Time spent in forums: 1 Week 4 Days 39 m 18 sec
Reputation Power: 55
C++ and C extended precision libraries are available and you can have python call them. It requires work on your part.

You are better off just getting the GMP (gnu multiple precison) library from
ftp.gnu.org and using it with your C or C++ compiler.

#7
August 27th, 2004, 04:22 PM
 khiloa
Registered User

Join Date: Aug 2004
Posts: 18
Time spent in forums: 2 h 13 m 38 sec
Reputation Power: 0
Thanks for the replies guys.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > square root precision