This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 8 years ago.
While solving Project Euler problems with Python (which I am a beginner at), I got this following error. The question is to find the sum of the digits of 2^1000. For that I wrote the following code :
sum=0
x=2**1000
while(x):
sum += x%10
print(sum) #Just to check whats happening
x /= 10
print("\n"*5)
print("Sum = ",sum)
For this, I get decimal added up somewhere in between.
Output :
6
10.0
10.0
12.0
16.0
....
1116.0
1122.0
1131.625 #Why does the decimal get added?
1138.59375
.....
1181.495136589947
1186.5812084526442
1188.089815638914
1195.240676357541
1195.9557624294036
1197.0272710365898
1197.1344218973084
1197.1451369833803
1197.1462084919874
.....
1197.1463275484991 #This number gets repeated a lot of times
1197.1463275484991
1197.1463275484991
Sum = 1197.1463275484991
Please explain what's going on and help.
Use integer division instead of floating point:
x //= 10
Do not know if you're looking for an alternative implementation, but this might be more straightforward if you don't want to risk crossing over into floating point land.
# Python 2.7
x = str(2**1000)
print sum([int(i) for i in x])
Related
This question already has answers here:
Is floating point arbitrary precision available?
(5 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am trying to divide floats by each other but am having a hard time getting accurate results. I understand that computers store floats in a way where the value stored is not exact to the given number. I am simply looking for a way where I can get specific results when working with floats.
input:
x = 2.4
y = 0.2
print(x/y)
Output:
11.999999998
I highly recommend to use decimals
Example
from decimal import Decimal
x = Decimal("2.4")
y = Decimal("0.2")
print(x / y) # 12
Notice we passing number as string, as passing float numbers would have the same problem you pointed out.
But care with comparison, as 12 == x / y evaluates to False
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 3 years ago.
Pretty new to python, facing a problem that requires basically the opposite of the remainder "%" function. For example, if I wanted to divide 81.5 by 20, my output would be 4. My best attempt is as follows:
amount = 81.504
round(amount, 2)
num20s = amount / 20
int(num20s)
I've tried several different combinations of the above code, but nothing has worked so far. This is the closest I've gotten to what I want, but it won't work in edge cases, and for some reason still represents the number with a ".0" at the end, so that last line must not be doing anything.
Integer division operator in python is "//".
>>> amount = 81.504
>>> amount // 20
Out[3]: 4.0
>>> int(amount // 20)
Out[4]: 4
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 5 years ago.
I made a small python file on my desktop for fun, and I'm frustrated to find that no values are being returned. Here is the code from the file:
def calc(x):
chance =1
for i in range(1,x+1):
chance *= ((365-i)/365)
return chance * 100
string = calc(23)
print string
And here is what I type into the terminal:
cd Desktop #where file is located
python py.py #py is the name of the file
And this is what it returns:
0
Since 365 and i are integers, you are performing integer division, which only returns the "whole" part of the result. Since 365-i is lesser than 365 dividing the two will result in 0, and continuously multiplying it by anything will still give 0.
You could use the 365.0 notation to use floating point division, which will eventually generate the result 46.1655742085:
chance = 1
for i in range(1,x+1):
chance *= ((365.0 - i) / 365.0)
# Floating point^-----------^
Well so on your first iteration your i = 1. 365-1 = 364. 364/365 = 0 because it is integer division in python. So the answer will always give you 0 unless you change it to allow for you to give a float.
Changing 365 to 365.0 will give you your answer
chance = chance * ((365-i)/365.0)
I just changed the 365 to 365.0 and I got
2.23071139399e-05
As my output
The result is correct in Python2.x. When a/b and a is less than b, result is 0.
Modify 365 to 365.0 it will cast the division result to float
chance *= ((365-i)/365.0)
This question already has answers here:
How do you check whether a number is divisible by another number?
(9 answers)
Closed 6 years ago.
Wondering what is the most efficient way to check if an integer can or cannot be divided by another number (could be float) in Python 2.7. Or more general, what is the most efficient way to check if an integer can be divided by n (n could be a float number) in Python 2.7.
My pain point is, if I try to get x/n, it is always an integer.
Try
if x % n == 0 :
hope this helps !
Here:
x = 25
y = 2.5 # Or something
if not x % y: # Works with float too
print 'can divide'
else:
print 'cannot divide'
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 6 years ago.
I am trying to do the calculation
Using the python decimal module with the following code:
from decimal import *
getcontext().prec = 9
sum = Decimal(0)
for i in range(1,11):
sum += Decimal(1/(i**4))
print sum
however, this outputs 1, not a very small fraction like I would expect. I can't find much information here https://docs.python.org/2/library/decimal.html about what is wrong with the code. My guess is sum is not being used as a Decimal in the loop, but I am unsure how to resolve that.
If you use Python 2.x, then in the expression: 1/(i**4), the integer devision is used, as result for i=1, it equals to 1 and for all other i>1, it gets 0.
Just add floating point to 1: 1./(i**4), this should fix the problem.
PS In Python 3.x, your code should work as expected, because operator / is defined on floating point numbers, while operator // is defined for integers.
First of all, don't use sum as a variable name, as it is a built-in.
And its sort of necessary to provide at least one float for arithmetic if you expect a float-type answer, here:
s = Decimal(0)
for i in range(1,11):
s += Decimal(1./(i**4)) # dividing 1. or 1.0 instead of just 1
print s
this gives:
1.08203658