Python numpy arange [duplicate] - python

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
Below code displays from 12.01 till 16.01. But shouldn't it display only till 16.00?
import numpy as np
for i in np.arange(12.01, (16.01), 0.01):
print(float('{num:0.2f}'.format(num=i)))

From the numpy.arange documentation:
When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use linspace for these cases.
So linspace might be more appropriate for your case
If you want 400 evenly spaced numbers from 12.01 to 16:
np.linspace(12.01, 16, num=400)

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 float in multiple of 5 [duplicate]

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!!!

Python OverflowError: math range error [duplicate]

This question already has answers here:
Why do I get "OverflowError: (34, 'Result too large')" or "OverflowError: (34, 'Numerical result out of range')" from floating-point exponentiation?
(6 answers)
Closed 9 days ago.
I get an Overflow error when i try this calculation,
output=math.exp(1391.12694245)*100
i know this happens because that the number used is 'outside the range of a double'.
but is there any way for solving this and getting the value of output.
Can someone please help?
Use extra precision floating point numbers from numpy:
import numpy as np
np.exp(np.array([1391.12694245],dtype=np.float128))*100
# array([ 1.4413011e+606], dtype=float128)

numpy.arange floating point errors [duplicate]

This question already has answers here:
python numpy arange unexpected results
(5 answers)
Closed 7 years ago.
Why does np.arange(5, 60, 0.1)[150] yield 19.999999999999947. But np.arange(5, 60, 0.5)[30] yield 20.0?
Why does this happen?
That's because floats (most of the time) cannot represent the exact value you put in. Try print("%.25f" % np.float64(0.1)) which returns 0.1000000000000000055511151 that's not exactly 0.1.
Numpy already provides a good workaround for almost-equal (floating point) comparisons: np.testing.assert_almost_equal so you can test by using np.testing.assert_almost_equal(20,np.arange(5, 60, 0.1)[150]).
The reason why your second example provides the real valus is because 0.5 can be represented as exact float 2**(-1) = 0.5 and therefore multiplications with this value do not suffer from that floating point problem.

Categories