File skyfield-pr404-comparefloat.patch of Package python-skyfield

From 84c39cb76a2dac3755a83fe104a9e834cbae5674 Mon Sep 17 00:00:00 2001
From: Ben Greiner <code@bnavigator.de>
Date: Fri, 10 Jul 2020 13:24:37 +0200
Subject: [PATCH 1/2] use numpy to compare floats

---
 skyfield/tests/test_planetarylib.py | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/skyfield/tests/test_planetarylib.py b/skyfield/tests/test_planetarylib.py
index 83877bd1..928f5386 100644
--- a/skyfield/tests/test_planetarylib.py
+++ b/skyfield/tests/test_planetarylib.py
@@ -1,4 +1,5 @@
 import numpy as np
+from numpy.testing import assert_allclose
 from skyfield.api import PlanetaryConstants, T0, load
 from skyfield.constants import AU_KM, AU_M
 from skyfield.positionlib import ICRF
@@ -43,11 +44,11 @@ def test_frame_rotation_matrices():
     ]
 
     R = frame.rotation_at(ts.tdb_jd(tdb))
-    assert (R == desired_rotation).all()  # Boom.
+    assert_allclose(R, desired_rotation)
 
     R2, Rv = frame.rotation_and_rate_at(ts.tdb_jd(tdb))
-    assert (R == R2).all()
-    assert (Rv == desired_rate).all()  # Boom.
+    assert_allclose(R, R2)
+    assert_allclose(Rv, desired_rate)
 
     # Second, a moment when the angle W is more than 2500 radians.
 
@@ -93,11 +94,11 @@ def test_frame_rotation_matrices():
     frame = pc.build_frame_named('MOON_ME_DE421')
     R = frame.rotation_at(ts.tdb_jd(tdb))
     delta = abs(R - desired_rotation)
-    assert (R == desired_rotation).all()
+    assert_allclose(R, desired_rotation)
 
     R2, Rv = frame.rotation_and_rate_at(ts.tdb_jd(tdb))
-    assert (R == R2).all()
-    assert (Rv == desired_rate).all()
+    assert_allclose(R, R2)
+    assert_allclose(Rv, desired_rate)
 
 def test_rotating_vector_into_frame():
     et_seconds = 259056665.1855896

From 5d8366e84003dbb48afe06bf1395182cc8119fb2 Mon Sep 17 00:00:00 2001
From: Ben Greiner <code@bnavigator.de>
Date: Fri, 10 Jul 2020 14:36:55 +0200
Subject: [PATCH 2/2] custom float compare function instead of standard numpy

---
 skyfield/tests/test_planetarylib.py | 48 ++++++++++++++++++++++++-----
 1 file changed, 41 insertions(+), 7 deletions(-)

diff --git a/skyfield/tests/test_planetarylib.py b/skyfield/tests/test_planetarylib.py
index 928f5386..6d29fed2 100644
--- a/skyfield/tests/test_planetarylib.py
+++ b/skyfield/tests/test_planetarylib.py
@@ -1,9 +1,43 @@
 import numpy as np
-from numpy.testing import assert_allclose
+import os
 from skyfield.api import PlanetaryConstants, T0, load
 from skyfield.constants import AU_KM, AU_M
 from skyfield.positionlib import ICRF
 
+
+def assert_floatsareclose(a, b, atol=None):
+    """compare a list of floats as tight as possible
+
+    https://github.com/skyfielders/python-skyfield/pull/404
+
+    Parameters
+    ----------
+    a, b : array_like float
+        Arrays to compare
+    atol : float, optional
+        maximum absolute difference
+
+    Raises
+    ------
+    AssertionError
+        if the difference of any element in `a` and `b` is too large
+        or the shapes don't match
+    """
+    if not atol:
+        atol = float(os.getenv("SKYFIELD_TEST_DEFAULT_ATOL", "1e-25"))
+    assert np.shape(a) == np.shape(b)
+    difference = np.asarray(a) - np.asarray(b)
+    if not np.all(np.abs(difference) < atol):
+        imax = np.argmax(difference)
+        maxe = np.max(difference)
+        raise AssertionError("The difference between\n"
+                             "{} and \n"
+                             "{}\n"
+                             "is larger than the allowed {:.3g}.\n"
+                             "Maximum difference at element {}: {:.7g}"
+                             "".format(a, b, atol, imax, maxe))
+
+
 def test_frame_rotation_matrices():
     # To produce the following matrices:
     #
@@ -44,11 +78,11 @@ def test_frame_rotation_matrices():
     ]
 
     R = frame.rotation_at(ts.tdb_jd(tdb))
-    assert_allclose(R, desired_rotation)
+    assert_floatsareclose(R, desired_rotation)
 
     R2, Rv = frame.rotation_and_rate_at(ts.tdb_jd(tdb))
-    assert_allclose(R, R2)
-    assert_allclose(Rv, desired_rate)
+    assert_floatsareclose(R, R2)
+    assert_floatsareclose(Rv, desired_rate)
 
     # Second, a moment when the angle W is more than 2500 radians.
 
@@ -94,11 +128,11 @@ def test_frame_rotation_matrices():
     frame = pc.build_frame_named('MOON_ME_DE421')
     R = frame.rotation_at(ts.tdb_jd(tdb))
     delta = abs(R - desired_rotation)
-    assert_allclose(R, desired_rotation)
+    assert_floatsareclose(R, desired_rotation)
 
     R2, Rv = frame.rotation_and_rate_at(ts.tdb_jd(tdb))
-    assert_allclose(R, R2)
-    assert_allclose(Rv, desired_rate)
+    assert_floatsareclose(R, R2)
+    assert_floatsareclose(Rv, desired_rate)
 
 def test_rotating_vector_into_frame():
     et_seconds = 259056665.1855896
openSUSE Build Service is sponsored by