numpy.arange floating point errors [duplicate] - python

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.

Related

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

Python numpy arange [duplicate]

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)

How to eliminate numpy.logspace numeric errors for integer powers of 10 (i.e., 10^n, where n is int) [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I am using numpy.logspace(-4,1,6) to create [0.0001, 0.001, 0.01, 0.1, 1, 10].
I noticed the numbers generated with numpy.logspace have numeric errors.
print('{:.24f}'.format(np.logspace(-4, 1, 6)[3]))
prints "0.100000000000000005551115" versus "0.1" i expected.
Anyways to eliminate numpy.logspace numeric errors for integer powers of 10 (i.e., 10^n, where n is integer)?
Thanks.
The simple answer is: no.
The somewhat longer answer is, that no floating point number with the value 0.1 exists, at least not "exact" (well, it's exact, i.e. within machine precision, for the computer but not in an analytical/mathematical sense)
Check out: https://en.wikipedia.org/wiki/Floating-point_arithmetic for further info.

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

Generating a list with foating point values [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
I wish to generate a list with these value:
[0,0.01,0.02...0.49]
and I though that this was the correct way to do it:
[0.01*i for i in range(50)]
But to my horror I observed the following:
>>> 35*0.01
0.35000000000000003
>>> float("0.35")
0.35
What am I doing wrong? If I understand correctly It seems 35*0.01 does not give the closest floating point representation of 0.35 but float("0.35") does.
To guarantee the nearest valid floating point number, you need to start with values that are exact. 0.01 isn't exact, but 100.0 is.
[i/100.0 for i in range(50)]

Categories