
Thanks for your work on this, Sebastian. I think there is a benefit for new users and learners to have visually obviously-correct results for the identities. The SciPy Developer's Guide [1] several of us worked on last week uses Snell's Law as a teaching example, and it would now give some results that would make newcomers double-take. You could argue that it's important to learn early not to expect too much from floats, but nonetheless I think something tangible is lost with this change, in a teaching context. [1] https://learn.scientific-python.org/development/tutorials/module/ Dan Daniel B. Allan, Ph.D (he/him) Data Science and Systems Integration NSLS-II Brookhaven National Laboratory ________________________________ From: Thomas Caswell <tcaswell@gmail.com> Sent: Wednesday, May 31, 2023 6:38 PM To: Discussion of Numerical Python <numpy-discussion@python.org> Subject: [Numpy-discussion] Re: Precision changes to sin/cos in the next release? Just for reference, this is the current results on the numpy main branch at special points: In [1]: import numpy as np In [2]: np.sin(0.0) Out[2]: 0.0 In [3]: np.cos(0.0) Out[3]: 0.9999999999999999 In [4]: np.cos(2*np.pi) Out[4]: 0.9999999999999998 In [5]: np.sin(2*np.pi) Out[5]: -2.4492935982947064e-16 In [6]: np.sin(np.pi) Out[6]: 1.2246467991473532e-16 In [7]: np.cos(np.pi) Out[7]: -0.9999999999999998 In [8]: np.cos(np.pi/2) Out[8]: 6.123233995736766e-17 In [9]: np.sin(np.pi/2) Out[9]: 0.9999999999999998 In [10]: np.__version__ Out[10]: '2.0.0.dev0+60.g174dfae62' On Wed, May 31, 2023 at 6:20 PM Robert Kern <robert.kern@gmail.com<mailto:robert.kern@gmail.com>> wrote: On Wed, May 31, 2023 at 5:51 PM Benjamin Root <ben.v.root@gmail.com<mailto:ben.v.root@gmail.com>> wrote: I think it is the special values aspect that is most concerning. Math is just littered with all sorts of identities, especially with trig functions. While I know that floating point calculations are imprecise, there are certain properties of these functions that still held, such as going from -1 to 1. As a reference point on an M1 Mac using conda-forge: ```
import numpy as np np.__version__ '1.24.3' np.sin(0.0) 0.0 np.cos(0.0) 1.0 np.sin(np.pi) 1.2246467991473532e-16 np.cos(np.pi) -1.0 np.sin(2*np.pi) -2.4492935982947064e-16 np.cos(2*np.pi) 1.0
Not perfect, but still right in most places.
FWIW, those ~0 answers are actually closer to the correct answers than 0 would be because `np.pi` is not actually π. Those aren't problems in the implementations of np.sin/np.cos, just the intrinsic problems with floating point representations and the choice of radians which places particularly special values at places in between adjacent representable floating point numbers.
I'm ambivalent about reverting. I know I would love speed improvements because transformation calculations in GIS is slow using numpy, but also some coordinate transformations might break because of these changes.
Good to know. Do you have any concrete example that might be worth taking a look at in more detail? Either for performance or accuracy.
--
Robert Kern
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org<mailto:numpy-discussion@python.org>
To unsubscribe send an email to numpy-discussion-leave@python.org<mailto:numpy-discussion-leave@python.org>
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/<https://urldefense.com/v3/__https://mail.python.org/mailman3/lists/numpy-discussion.python.org/__;!!P4SdNyxKAPE!AN49UtwKc5L2e0XZQOM8cmjUjtJb6jv0Ut6c-8COS1z8tgzfuRIb9stzIPAmrtbW8DyDnXNAmB4V6Nw$>
Member address: tcaswell@gmail.com<mailto:tcaswell@gmail.com>
--
Thomas Caswell
tcaswell@gmail.com<mailto:tcaswell@gmail.com>