Reputation: 13
I tried to calculate an exponential calculation, I tried both ways, writing it with the normal **
operator and the pow()
function.
If I directly use numbers, everything works completely fine. But as soon as I use variables which get their value from input() functions the result is rounded, although I use the float argument.
I am quite new to coding, so please don't go to hard on me.
Code below:
pow(1.05,5), everything fine, result is 1.2762815625 and so on
float(pow(int(a),int(b)) the result is just 1.0, although it should be the same as above.
Upvotes: 1
Views: 91
Reputation: 586
it is because you are doing the power operation on int i.e first you are converting the 1.05
to 1
by doing int(1.05)
and then calculating the power()
. you need to apply pow()
on float()
print(float(pow(float(1.05),float(5))))
or either
print(pow(float(1.05),float(5)))
Upvotes: 2
Reputation: 2270
The reason the float function returns 1.0 is because of how it deals with integers.
For example:
integer = 5.5236734
print(float(integer))
The above code's output will be:
5.5236734
Now lets say you make some changes:
integer = 5.5236734
print(int(integer))
First, we made integer
a decimal number, and then we said to print the int()
form of the decimal.
This will be the result:
5
So, to fix your code, you just need to do this:
a = 1.05
b = 5
print(float(pow(a, b)))
Which will output:
1.2762815625000004
Hope this helps!
Upvotes: 1