So i ask the user for some numerical inputs with a loop.
After that the inputs are being processed the mathematical result is slightly different than the actual math done with a calculator.
Does it take the 0 break value as input and somehow messes up my average ?
values = []
while True:
m = float(input("Number of shares?, 0 for Exit: "))
n = float(input("Total price of purchase?, 0 for Exit:"))
if m == 0:
break
if n == 0:
break
values.append(m)
sum = n/m
print(sum)
First things first:
I assume, you want to save the prize of a transaction (shares * price), put this number in the array values and then you want to calculate the average number from your array values. Your code doesn't do that.
sum = n/m # calculates **only** n / m and saves it **only** in the variable sum.
# You overwrite sum every loop.
Try this:
values = []
while True:
m = float(input("Number of shares?, 0 for Exit: ")) # Caution! It's not a good idea to use a float here!
n = float(input("Total price of purchase?, 0 for Exit:")) # Caution! It's not a good idea to use a float here!
if m == 0:
break
if n == 0:
break
values.append(n * m) # append the price of the transaction into values
print( sum(values) / len(values) ) # gives you the average prize per transaction
Caution! Exmample: Give m = 1.1 and n = 1.1 and only one transaction. Output is 1.2100000000000002!
Why?
Short Answer:
Avoid using floats. Instead use doubles. And if you are handling currencies never ever use floats/doubles. In python you could use decimal, if you need more precision.
Long Answer:
You are trying to calculate with floats.
Most modern programming language use the standard IEEE_754-1985 to store floats. In IEEE-754, some numbers couldn't be exact represented as an float. Because of it's binary Structure. As an example the number 0.1.
1.0 as a float is:
0 01111111 00000000000000000000000
The first bit stands for the sign. If it is zero the number is a positiv number. If it is one, it is negative.
-1.0:
1 01111111 00000000000000000000000
The 8 bits after the sign-bit is the exponent.
The 23 bits after the exponent bits are the mantissa.
What happens if we have a one in the most significant bit in the mantissa?
0 01111111 10000000000000000000000
We have now a 1.0 + 2^-1. That is 1.5.
And what is:
0 01111111 01000000000000000000000
We have now a 1.0 + 2^-2. That is 1.25.
1.1 is:
0 01111111 00011001100110011001101
1.0 + 2^-4 + 2^-5 + 2^-8 + 2^-9 + 2^-12 + 2^-13 + 2^-16 + 2^-17 +2^-20 + 2^-21 + 2^-23
That's: 1.10000002384185791015625
The Single precision gives you approximately 1.10000000.
You could use Double precision. A double contains 64 bits. 1 for the sign, 11 for the exponent and 52 bit for the mantissa. But 0.1 is still only an approximately number and not exact 0.1.
Solution
from decimal import Decimal
values = []
while True:
m = Decimal(input("Number of shares?, 0 for Exit: "))
n = Decimal(input("Total price of purchase?, 0 for Exit:"))
if m == 0:
break
if n == 0:
break
values.append(n * m)
average = sum(values) / len(values)
print(average)
Im stuck on a problem where I have to write a function that converts a denary number into a binary number using the repeated division by two algorithm. Steps Include:
The number to be converted is divided by two.
The remainder from the division is the next binary digit. Digits are added to the front of the sequence.
The result is truncated so that the input to the next division by two is always an integer.
The algorithm continues until the result is 0.
Please click the link below to see what the output should be like:
https://i.stack.imgur.com/pifUO.png
def dentobi(user):
denary = user
divide = user / 2
remainder = user % 2
binary = remainder
if user != 0:
print("Denary:", denary)
print("Divide by 2:", divide)
print("Remainder:", remainder)
print("Binary:", binary)
user = int(input("Please enter a number: "))
dentobi(user)
This is what I have done so far but Im not getting anywhere.
Can someone explain how I would do this?
The Answer provided by #user2390182 is functionally correct except that it returns an empty string when num is zero. However, I have noted on several occasions that divmod() is rather slow. Here are three slightly different techniques and their performance statistics.
import time
# This is the OP's original code edited to allow for num == 0
def binaryx(num):
b = ""
while num:
num, digit = divmod(num, 2)
b = f"{digit}{b}"
return b or '0'
# This is my preferred solution
def binaryo(n):
r = []
while n > 0:
r.append('1' if n & 1 else '0')
n >>= 1
return ''.join(reversed(r)) or '0'
# This uses techniques suggested by my namesake
def binaryy(n):
r = ''
while n > 0:
r = str(n & 1) + r
n >>= 1
return r or '0'
M = 250_000
for func in [binaryx, binaryo, binaryy]:
s = time.perf_counter()
for _ in range(M):
func(987654321)
e = time.perf_counter()
print(f'{func.__name__} -> {e-s:.4f}s')
Output:
binaryx -> 1.3817s
binaryo -> 0.9861s
binaryy -> 1.6052s
One way, using divmod to divide by 2 and get the remainder in one step:
def binary(num):
b = ""
while num:
num, digit = divmod(num, 2)
b = f"{digit}{b}"
return b
binary(26)
'11010'
This assumes a positive number but can easily be extended to work for 0 and negatives.
I've seen a couple posts explaning how to iterate through the digits of a number in Python, but they all turn the number into a string before iterating through it...
For example:
n=578
print d for d in str(n)
How can I do this without the conversion into a string?
10**int(log(n, 10)) is basically 10*, such that it is the same length as n. The floor division of n by that will give us the leading digit, while the modulo % gives us the rest of the number.
from math import log
def digits(n):
if n < 0:
yield '-'
n = -1 * n
elif n == 0:
yield 0
return
xp = int(log(n, 10).real)
factor = 10**xp
while n:
yield int(n/factor)
n = n % factor
try:
xp, old_xp = int(log(n, 10).real), xp
except ValueError:
for _ in range(xp):
yield 0
return
factor = 10**xp
for _ in range(1, old_xp-xp):
yield 0
for x in digits(12345):
print(x)
prints
1
2
3
4
5
Edit: I switched to this version, which is much less readable, but more robust. This version correctly handles negative and zero values, as well as trailing and internal 0 digits.
This question already has answers here:
Easy way of finding decimal places
(16 answers)
Closed 4 months ago.
I'm trying to write a Python 2.5.4 code to write a function that takes a floating-point number x as input and returns the number of digits after the decimal point in x.
Here's my code:
def number_of_digits_post_decimal(x):
count = 0
residue = x -int(x)
if residue != 0:
multiplier = 1
while int(multiplier * residue) != (multiplier * residue):
count += 1
multiplier = 10 * multiplier
print count
print multiplier
print multiplier * residue
print int(multiplier * residue)
return count
print number_of_digits_post_decimal(3.14159)
The print statements within the while loop are only for debugging purposes.
Now when I run this code, I get the following as output.
1
10
1.4159
1
2
100
14.159
14
3
1000
141.59
141
4
10000
1415.9
1415
5
100000
14159.0
14158
6
1000000
141590.0
141589
7
10000000
1415900.0
1415899
8
100000000
14159000.0
14158999
9
1000000000
....
The final value of count as returned by this function is 17.
How to modify this code in order to achieve our desired result?
Here's a shortcut that you might like:
def num_after_point(x):
s = str(x)
if not '.' in s:
return 0
return len(s) - s.index('.') - 1
This was interesting! So if you run the following:
x = 3.14159
residue = x - int(x)
print residue
You will get the following result:
0.14158999999999988
This decimal does in fact have 17 digits. The only way that I found to override this was to avoid doing the subtraction (which is the root cause of the error, as you can see from the inaccuracy here). So this code should work as you expect:
def number_of_digits_post_decimal(x):
count = 0
residue = x -int(x)
if residue != 0:
multiplier = 1
while not (x*multiplier).is_integer():
count += 1
multiplier = 10 * multiplier
return count
This will just shift the decimal to the right until python identifies it as an integer (it will do a rightward shift exactly the number of times you want too). Your code actually worked as you intended for it to, something unintended just happened during the subtraction process. Hope this helps!
def decimal_places(f):
exp = -1
remainder = True
while remainder:
exp += 1
a = f * 10**exp
remainder = int(a) - a
return(exp)
def precision(f):
integer, remainder = str(f).split('.')
return len(remainder)
I want to generate the digits of the square root of two to 3 million digits.
I am aware of Newton-Raphson but I don't have much clue how to implement it in C or C++ due to lack of biginteger support. Can somebody point me in the right direction?
Also, if anybody knows how to do it in python (I'm a beginner), I would also appreciate it.
You could try using the mapping:
a/b -> (a+2b)/(a+b) starting with a= 1, b= 1. This converges to sqrt(2) (in fact gives the continued fraction representations of it).
Now the key point: This can be represented as a matrix multiplication (similar to fibonacci)
If a_n and b_n are the nth numbers in the steps then
[1 2] [a_n b_n]T = [a_(n+1) b_(n+1)]T
[1 1]
which now gives us
[1 2]n [a_1 b_1]T = [a_(n+1) b_(n+1)]T
[1 1]
Thus if the 2x2 matrix is A, we need to compute An which can be done by repeated squaring and only uses integer arithmetic (so you don't have to worry about precision issues).
Also note that the a/b you get will always be in reduced form (as gcd(a,b) = gcd(a+2b, a+b)), so if you are thinking of using a fraction class to represent the intermediate results, don't!
Since the nth denominators is like (1+sqrt(2))^n, to get 3 million digits you would likely need to compute till the 3671656th term.
Note, even though you are looking for the ~3.6 millionth term, repeated squaring will allow you to compute the nth term in O(Log n) multiplications and additions.
Also, this can easily be made parallel, unlike the iterative ones like Newton-Raphson etc.
EDIT: I like this version better than the previous. It's a general solution that accepts both integers and decimal fractions; with n = 2 and precision = 100000, it takes about two minutes. Thanks to Paul McGuire for his suggestions & other suggestions welcome!
def sqrt_list(n, precision):
ndigits = [] # break n into list of digits
n_int = int(n)
n_fraction = n - n_int
while n_int: # generate list of digits of integral part
ndigits.append(n_int % 10)
n_int /= 10
if len(ndigits) % 2: ndigits.append(0) # ndigits will be processed in groups of 2
decimal_point_index = len(ndigits) / 2 # remember decimal point position
while n_fraction: # insert digits from fractional part
n_fraction *= 10
ndigits.insert(0, int(n_fraction))
n_fraction -= int(n_fraction)
if len(ndigits) % 2: ndigits.insert(0, 0) # ndigits will be processed in groups of 2
rootlist = []
root = carry = 0 # the algorithm
while root == 0 or (len(rootlist) < precision and (ndigits or carry != 0)):
carry = carry * 100
if ndigits: carry += ndigits.pop() * 10 + ndigits.pop()
x = 9
while (20 * root + x) * x > carry:
x -= 1
carry -= (20 * root + x) * x
root = root * 10 + x
rootlist.append(x)
return rootlist, decimal_point_index
As for arbitrary big numbers you could have a look at The GNU Multiple Precision Arithmetic Library (for C/C++).
For work? Use a library!
For fun? Good for you :)
Write a program to imitate what you would do with pencil and paper. Start with 1 digit, then 2 digits, then 3, ..., ...
Don't worry about Newton or anybody else. Just do it your way.
Here is a short version for calculating the square root of an integer a to digits of precision. It works by finding the integer square root of a after multiplying by 10 raised to the 2 x digits.
def sqroot(a, digits):
a = a * (10**(2*digits))
x_prev = 0
x_next = 1 * (10**digits)
while x_prev != x_next:
x_prev = x_next
x_next = (x_prev + (a // x_prev)) >> 1
return x_next
Just a few caveats.
You'll need to convert the result to a string and add the decimal point at the correct location (if you want the decimal point printed).
Converting a very large integer to a string isn't very fast.
Dividing very large integers isn't very fast (in Python) either.
Depending on the performance of your system, it may take an hour or longer to calculate the square root of 2 to 3 million decimal places.
I haven't proven the loop will always terminate. It may oscillate between two values differing in the last digit. Or it may not.
The nicest way is probably using the continued fraction expansion [1; 2, 2, ...] the square root of two.
def root_two_cf_expansion():
yield 1
while True:
yield 2
def z(a,b,c,d, contfrac):
for x in contfrac:
while a > 0 and b > 0 and c > 0 and d > 0:
t = a // c
t2 = b // d
if not t == t2:
break
yield t
a = (10 * (a - c*t))
b = (10 * (b - d*t))
# continue with same fraction, don't pull new x
a, b = x*a+b, a
c, d = x*c+d, c
for digit in rdigits(a, c):
yield digit
def rdigits(p, q):
while p > 0:
if p > q:
d = p // q
p = p - q * d
else:
d = (10 * p) // q
p = 10 * p - q * d
yield d
def decimal(contfrac):
return z(1,0,0,1,contfrac)
decimal((root_two_cf_expansion()) returns an iterator of all the decimal digits. t1 and t2 in the algorithm are minimum and maximum values of the next digit. When they are equal, we output that digit.
Note that this does not handle certain exceptional cases such as negative numbers in the continued fraction.
(This code is an adaptation of Haskell code for handling continued fractions that has been floating around.)
Well, the following is the code that I wrote. It generated a million digits after the decimal for the square root of 2 in about 60800 seconds for me, but my laptop was sleeping when it was running the program, it should be faster that. You can try to generate 3 million digits, but it might take a couple days to get it.
def sqrt(number,digits_after_decimal=20):
import time
start=time.time()
original_number=number
number=str(number)
list=[]
for a in range(len(number)):
if number[a]=='.':
decimal_point_locaiton=a
break
if a==len(number)-1:
number+='.'
decimal_point_locaiton=a+1
if decimal_point_locaiton/2!=round(decimal_point_locaiton/2):
number='0'+number
decimal_point_locaiton+=1
if len(number)/2!=round(len(number)/2):
number+='0'
number=number[:decimal_point_locaiton]+number[decimal_point_locaiton+1:]
decimal_point_ans=int((decimal_point_locaiton-2)/2)+1
for a in range(0,len(number),2):
if number[a]!='0':
list.append(eval(number[a:a+2]))
else:
try:
list.append(eval(number[a+1]))
except IndexError:
pass
p=0
c=list[0]
x=0
ans=''
for a in range(len(list)):
while c>=(20*p+x)*(x):
x+=1
y=(20*p+x-1)*(x-1)
p=p*10+x-1
ans+=str(x-1)
c-=y
try:
c=c*100+list[a+1]
except IndexError:
c=c*100
while c!=0:
x=0
while c>=(20*p+x)*(x):
x+=1
y=(20*p+x-1)*(x-1)
p=p*10+x-1
ans+=str(x-1)
c-=y
c=c*100
if len(ans)-decimal_point_ans>=digits_after_decimal:
break
ans=ans[:decimal_point_ans]+'.'+ans[decimal_point_ans:]
total=time.time()-start
return ans,total
Python already supports big integers out of the box, and if that's the only thing holding you back in C/C++ you can always write a quick container class yourself.
The only problem you've mentioned is a lack of big integers. If you don't want to use a library for that, then are you looking for help writing such a class?
Here's a more efficient integer square root function (in Python 3.x) that should terminate in all cases. It starts with a number much closer to the square root, so it takes fewer steps. Note that int.bit_length requires Python 3.1+. Error checking left out for brevity.
def isqrt(n):
x = (n >> n.bit_length() // 2) + 1
result = (x + n // x) // 2
while abs(result - x) > 1:
x = result
result = (x + n // x) // 2
while result * result > n:
result -= 1
return result