@@ -199,6 +199,7 @@ def cart2spher(r, theta: bool = True, cos_phi: bool = False, maxR=None):
199199 If `cos_phi` is ``True`` this is :math:`\cos(\phi)`, otherwise
200200 :math:`\phi` is returned (the polar angle from the :math:`z` axis)
201201 """
202+ eps = 1e-16
202203 r = _a .asarray (r )
203204 if maxR is None :
204205 rr = sqrt (square (r ).sum (- 1 ))
@@ -207,7 +208,12 @@ def cart2spher(r, theta: bool = True, cos_phi: bool = False, maxR=None):
207208 else :
208209 theta = None
209210 phi = zeros_like (rr )
210- idx = rr != 0.0
211+ # Typically the angles then becomes a precision of the length
212+ # precision.
213+ # So calculating angles, and re-calculating coordinates results
214+ # in differences on the scale of this `eps` number.
215+ # Hence, we just choose it around numerical accuracy.
216+ idx = rr > eps
211217 divide (r [..., 2 ], rr , out = phi , where = idx )
212218 if not cos_phi :
213219 arccos (phi , out = phi , where = idx )
@@ -228,7 +234,7 @@ def cart2spher(r, theta: bool = True, cos_phi: bool = False, maxR=None):
228234 theta = None
229235
230236 phi = zeros_like (rr )
231- idx0 = rr != 0.0
237+ idx0 = rr > eps
232238 divide (r [..., 2 ], rr , out = phi , where = idx0 )
233239 if not cos_phi :
234240 arccos (phi , out = phi , where = idx0 )
0 commit comments