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)
Related
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
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
>>> import math
>>> math.sin(math.radians(180))
1.2246467991473532e-16
>>>
Isn't the sine of 180ยบ 0.0?
It is an approximation of pi. You can round it to 0.
This question already has answers here:
Round to 5 (or other number) in Python
(21 answers)
How do I round to the nearest 0.5?
(10 answers)
Closed 2 years ago.
I got this number 1.12412 and I want to round it to 1.12415 or 1.12410 (muliple of 5 in last decimal)
If using the Round(X,4) function I get 1.1241 (4 decimals).
Is there a function that can make that happen?
Thanks!
There is an answer in stack but using c# not python
My way to do that is to specify rounding unit first and then simple trick as below:
import numpy as np
rounding_unit = 0.00005
np.round(1.12412/rounding_unit) * rounding_unit
You may:
Multiply your number by 2
Use Round(X,4)
Divide the result by 2
profit!!!
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().
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