why int(12) becomes 11 [duplicate] - python

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
Using Python 2.7. Here is the code and output, my purpose is simply to check if a number is a cube number.
Source code,
x = 1728 ** (1.0/3)
print x
y = int(x)
print y
Output,
12.0
11

Because you're using floating points, and the result is some very very small fraction less than 12, and when you cast x to an int, the entire decimal portion of the number is discarded.
If what you want to do is round the number, use round().

Related

Find the division remainder of a float number Python [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Python modulo on floats [duplicate]
(3 answers)
Closed 6 days ago.
Why 0.03 divide by 0.01, the reminder is 0.01, not zero??
I try 0.3 % 0.1, the result is 0.1. For 3 % 1, the result is zero. Crazy.
Use remainder can avoid this error, as shown below.
from math import remainder
remainder(0.03,0.01)

How the Ceil function is calculating the value if this expression? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
How to round a floating point number up to a certain decimal place?
(12 answers)
Closed 7 months ago.
ceil((14.84 - 14.04)/0.08)
output - 11
I was expecting the output to be 10 when manually calculated but when running it in python, it is giving output as 11
Is floating point math broken?
The float value from your equation is actually 10.000000000000009 because of how floats are handled (see link for more information). So, even though it is such a small amount above 10 the ceiling function will still place it at 11.
You can try rounding the number to a decimal point that you trust to get the value you want:
from math import ceil
ceil(round((14.84 - 14.04)/0.08, 2))
Output: 10

How to round in python? example, 45.722 to 45.72, 45.0 to 45.00 [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 2 years ago.
I wanna round after decimal point. I know that the function round does it but when the number has only zeros after decimal point I also wanna keep the amount of zeros that I want, becouse python leaves only 1 zero.
round(54.983839,2) == 54.98
round(54.0, 2) == 54.0 # and here I want 54.00
Use string formatting in python: "%.2f" % (54.0).
You can use format():
print(format(54.0, ".2f"))
The output will be:
54.00

Convert a floating point number to an integer except math.floor() [duplicate]

This question already has answers here:
Round a floating-point number down to the nearest integer?
(12 answers)
Closed 7 years ago.
Is there any way to convert a floating point number in Python to an integer except math.floor()?
I have already tried math.floor(), but I am getting an error says:
Cannot import math
Any other way?
Use the int() function
print int(5.3) # "5"
For more info
You can:
Use int(3.14)
import math then math.floor(3.14) or math.ceil(3.14) (depending on which way you want to round) (you said this doesn't work but I'll leave it for reference)
x = x - x % 1 or x -= x % 1

How python calculate this division? [duplicate]

This question already has answers here:
Negative integer division surprising result
(5 answers)
Closed 7 years ago.
How python calculate this division?
>>>-3/10
-1
Looks like python rounds the answer to the lower value.
>>> -3/4
-1
>>> -3/4.
-0.75
>>> -3/10.
-0.3
>>> -3/10
-1
This is just my guess.
Python 2, like many languages, uses integer division. Dividing two integers returns a integer (the nearest integer to the answer rounded down.)
To get a floating point result, you need to force one or more of the terms to be a float.
float(-3)/10

Categories