create equally spaced array [duplicate] - python

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I would like to create an array of number equally spaced (0.1) between 0.1 and 100
step=0.1
range_a=numpy.arange(step,100+step,step)
why my first element is
range_a[0]
Out[27]: 0.10000000000000001
and not 0.1?
and how do I get an array equal to
[0.1, 0.2, 0.3, ..., 100]

As mentioned in the comments, this is due to how floats are handled. In general, floats are used for imprecise but quick calculations, and double is used where accuracy is important. Your code can be rewritten as follows to get precisely what you want
step = 0.1
range_a = numpy.arange(step, 100+step, step).astype(numpy.double)

Related

Python's Numpy dot function returning incorrect value, why? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 9 months ago.
Real simple, my code is:
import numpy as np
a = np.array([0.4, 0.3])
b = np.array([-0.15, 0.2])
print(np.dot(a,b))
The dot product of this should be 0, and instead i get:
3.3306690738754695e-18
Floating-point!
Floating-point (i.e. non-integer) arithmetic tends not to be 100% accurate.
See here for more info.
Also, note that your result is very close to zero.

How can I consistently get correct float division in python3? [duplicate]

This question already has answers here:
Is floating point arbitrary precision available?
(5 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am trying to divide floats by each other but am having a hard time getting accurate results. I understand that computers store floats in a way where the value stored is not exact to the given number. I am simply looking for a way where I can get specific results when working with floats.
input:
x = 2.4
y = 0.2
print(x/y)
Output:
11.999999998
I highly recommend to use decimals
Example
from decimal import Decimal
x = Decimal("2.4")
y = Decimal("0.2")
print(x / y) # 12
Notice we passing number as string, as passing float numbers would have the same problem you pointed out.
But care with comparison, as 12 == x / y evaluates to False

Is there a rule about how numbers with decimal points are rounded by %.nf when 5 comes at the (n+1)-th? [duplicate]

This question already has answers here:
Python 3.x rounding behavior
(13 answers)
Why is rounding 0.5 (decimal) not exact? [duplicate]
(1 answer)
Why round(4.5) == 4 and round(5.5) == 6 in Python 3.5? [duplicate]
(2 answers)
Closed 3 years ago.
I am playing around with python print function and came across this problem.
print('%.2f' % (0.665)) # 0.67
print('%.3f' % (0.0625))# 0.062
Since when two decimal places are kept for 0.665 and the result is 0.67, I expect that the output of keeping three decimal places of 0.0625 to be 0.063 but the result is 0.062.
The general rule is that when the rounded-off part of a number is exactly 5, you choose the direction that makes the final resulting digit even - this avoids any systematic bias in these halfway cases. This applies in the case of 0.0625, which is one of the uncommon decimal numbers that have an exact representation in floating-point binary - its final digit truly is a 5. (For an example of a number that rounds up in this case, try 0.375 to two places.) The number 0.665, on the other hand, does not actually exist - the closest floating-point value is actually 0.6650000000000000355271. The rounded-off part is definitely (although only slightly) greater than 5, so it necessarily rounds up.
It is, as often, the annoying problem with floating point rounding.
.0625 can be represented in an exact way, so it is rounded down. (2 is even, so the most usual rounding algorithm decides to round down in these cases.)
.665 cannot be represented in an exact way (its internal representation is either slightly smaller or slightly bigger than the given number. In this case it is probably slightly bigger, so despite the 6 before the 5 being even, it rounds up.

Lists in Python last element [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I am new to python and I tried this:
import numpy as np
x = np.arange(0.7,1.3,0.1)
print (x)
y = np.arange(0.6,1.3,0.1)
print (y)
The output was [ 0.7 0.8 0.9 1. 1.1 1.2 1.3] and [ 0.6 0.7 0.8 0.9 1. 1.1 1.2]. Why in the first case 1.3 appears in the list and in the second case it doesn't?
This is due to rounding errors. If you actually print the last element in x in it's full precision, you'll see that it is smaller than 1.3:
>>> import numpy as np
>>> x = np.arange(0.7,1.3,0.1)
>>> 1.3 > x[-1]
True
>>> x[-1]
1.2999999999999998
Note, as stated in the 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.:
arange is not suitable for floating point numbers:
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.
I'm not familiar with the internals of numpy, but my guess is that this is a side effect of floating point numbers not being exact (meaning that they can't exactly represent some values).
See the numpy.arange documentation here:
specifically "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"

Why is np.average, among other average calculators, giving this result [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 7 years ago.
Can someone explain to my why, according to many average calculators, the average of this array is some very small negative number instead of zero?
[-5.4, -4.4, -2.4, -1.4, 13.6]
for example, numpy.average and numpy.mean return this value:
-3.5527136788e-16
Because float numerics is not perfect. And simply this does not sum to 0, but to something around 1e-14 in this system. You can find even more funny things - change order of your numbers and then calculate sum/mean and you will find that result depends on ordering too. The short story is that you cannot express 0.4 nor 0.6 in base 2, which is used in computers.

Categories

Resources