Hi Andrew,
I have no clue what future versions of Python will do, but on my setup
there is no optimization done on this (I have taken arrays as float32, just
to show the differences better):
In [19]: N = 10
In [20]: x0 = numpy.random.uniform(size=(N, )).astype(numpy.float32)
In [21]: h = numpy.random.uniform(size=(N, )).astype(numpy.float32)
In [22]: dx = (x0 + h) - x0
In [23]: h
Out[23]:
array([0.55208284, 0.3112231 , 0.93941885, 0.25871968, 0.3018999 ,
0.29337496, 0.05489019, 0.01224913, 0.3677995 , 0.05231242],
dtype=float32)
In [24]: dx
Out[24]:
array([0.5520829 , 0.3112231 , 0.9394188 , 0.25871968, 0.30189988,
0.29337496, 0.05489022, 0.01224911, 0.3677995 , 0.0523124 ],
dtype=float32)
In [24]: dx - h
Out[25]:
array([ 5.9604645e-08, 0.0000000e+00, -5.9604645e-08, 0.0000000e+00,
-2.9802322e-08, 0.0000000e+00, 2.2351742e-08, -1.3038516e-08,
0.0000000e+00, -1.1175871e-08], dtype=float32)
But it also happens with float64 types, with much smaller differences of
course (in the order of 1e-16). My (limited) understanding of the way
Python works is that parenthesis will always be resolved first, the rest
after.
Assuming I understood your question correctly, of course :-) .
Andrea.
On Fri, 20 Dec 2024 at 03:46, Andrew Nelson via NumPy-Discussion <
[email protected]> wrote:
> Hi all,
> Let's assume we have two float arrays, `x0` and `h`, both of which have
> the same shape. Let's calculate the sum of those two arrays:
>
> ```
> x1 = x0 + h
> ```
>
> The true representable difference between `x1` and `x0` is:
>
> ```
> dx = x1 - x0
> ```
>
> Is it also possible to get that exact representation by doing the
> following:
>
> ```
> dx = (x0 + h) - x0
> ```
>
> Or is there a risk that the Python interpreter would prematurely optimize
> that to give:
>
> ```
> dx = h
> ```
>
>
> --
> _____________________________________
> Dr. Andrew Nelson
>
>
> _____________________________________
> _______________________________________________
> NumPy-Discussion mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: [email protected]
>
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: [email protected]