This question already has answers here:
Python modulo on floats [duplicate]
(3 answers)
Closed 5 years ago.
def height(cent):
height= {}
height["feet"]= int(cent/30.48)
height["inch"]= int(cent%30.48)/2.54
print (height)
height (182.88)
print (182.88/30.48)
print (182.88%30.48)
The output is:
{'inch': 11, 'feet': 6}
6.0
30.479999999999993
Why does 182.88%30.48 not equal zero?
Because the value of 30.48 is really 30.4799.. This is because of the way that floating point numbers are stored in python. So when you are dividing 30.479999 by 182.88, the resulting rounded integer is 5 (i.e. 182.88 // 30.48 == 5). So the remainder is 30.47999...
Related
This question already has answers here:
What is the smallest number which can be represented in python?
(2 answers)
Is floating point math broken?
(31 answers)
Closed 2 years ago.
Why does the result of the next product yield the result 0?
print(3.944069389679206e-306*2.85043837e-46)
The product that results from that multiplication is too small for floats to hold.
Python uses double-precision floats, which can hold values from about 10 to the -308 to 10 to the 308 power.
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed 5 years ago.
scores = [20, 20, 20, 10, 9, 8]
if scores[0] == max(scores):
win = (1/(scores.count(max(scores))))
print win
I would expect this to return: 0.333
Instead it returns: 0
I put in some debugging code and confirmed that:
The if condition is being met. The subsequent code is being executed.
(scores.count(max(scores))) is returning a value of 3
So it would seem that win should obviously be set to 1/3, but it's being set to 0. I tried initializing the win variable as a float, but it made no difference.
This question already has answers here:
Easy pretty printing of floats?
(19 answers)
Closed 5 years ago.
Very quick question.
x = 10
print("value is {:d}".format(x))
returns
value is 10
on the other hand:
x = 10.0
print("value is {:d}".format(x))
returns
ValueError: Unknown format code 'd' for object of type 'float'
Why doesnt this work?
You would use f not d for floats. And then specify the precision width as 0:
>>> print("value is {:.0f}".format(x))
value is 10
From Python docs: 'd' Decimal Integer. Outputs the number in base 10.
It will output the number in base 10, thats why you are getting the ValueError.
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 6 years ago.
In Python 3 vs Python 2.6, I've noticed that I can divide two integers and get a float. How do you get the Python 2.6 behaviour back?
Is there a different method to get int/int = int?
Try this:
a = 1
b = 2
int_div = a // b
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 6 years ago.
How do I convert 45.34531 to 45.3?
Are you trying to represent it with only one digit:
print("{:.1f}".format(number)) # Python3
print "%.1f" % number # Python2
or actually round off the other decimal places?
round(number,1)
or even round strictly down?
math.floor(number*10)/10
>>> "{:.1f}".format(45.34531)
'45.3'
Or use the builtin round:
>>> round(45.34531, 1)
45.299999999999997
round(number, 1)