From 753554db67f38788931ad21d9ec70008b2b67dc6 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Fri, 7 Jan 2022 20:19:23 -0500 Subject: [PATCH] Make demo tests less sensitive to tiny floating point errors. More work getting MacOS build process to work. Avoid excessive number of floating point digits of output in the demo tests, so that insignificant floating point variations don't cause unit test failures. --- demo/csharp/horizon/horizon.cs | 7 ++++++- demo/csharp/test/horizon_correct.txt | 4 ++-- demo/nodejs/horizon.js | 6 +++++- demo/nodejs/test/horizon_correct.txt | 4 ++-- demo/python/astronomy.py | 11 ++++++++--- demo/python/camera.py | 8 ++++---- demo/python/test/camera_correct.txt | 4 ++-- demo/python/test/triangulate_correct.txt | 4 ++-- generate/template/astronomy.py | 11 ++++++++--- source/python/README.md | 5 +++++ source/python/astronomy.py | 11 ++++++++--- 11 files changed, 52 insertions(+), 23 deletions(-) diff --git a/demo/csharp/horizon/horizon.cs b/demo/csharp/horizon/horizon.cs index d04c8e27..160cbb38 100644 --- a/demo/csharp/horizon/horizon.cs +++ b/demo/csharp/horizon/horizon.cs @@ -141,7 +141,12 @@ namespace horizon else direction = "descends"; /* azimuth is more toward the west than the east */ - Console.WriteLine("Ecliptic longitude {0,9:0.0000} {1} through horizon az {2,9:0.0000}, alt {3,12:0.000e+00}", ex, direction, hx.lon, hx.lat); + Console.WriteLine("Ecliptic longitude {0,9:0.0000} {1} through horizon at azimuth {2,9:0.0000}", ex, direction, hx.lon); + if (Math.Abs(hx.lat) > 5.0e-7) + { + Console.Error.WriteLine("FindEclipticCrossings: Excessive altitude = {0}", hx.lat); + return 1; + } } } return 0; diff --git a/demo/csharp/test/horizon_correct.txt b/demo/csharp/test/horizon_correct.txt index 3708b37f..24824282 100644 --- a/demo/csharp/test/horizon_correct.txt +++ b/demo/csharp/test/horizon_correct.txt @@ -1,2 +1,2 @@ -Ecliptic longitude 93.6460 descends through horizon az 296.3883, alt -1.733e-07 -Ecliptic longitude 274.9704 ascends through horizon az 115.7274, alt 2.104e-07 +Ecliptic longitude 93.6460 descends through horizon at azimuth 296.3883 +Ecliptic longitude 274.9704 ascends through horizon at azimuth 115.7274 diff --git a/demo/nodejs/horizon.js b/demo/nodejs/horizon.js index 9befffdd..e5bc10ac 100644 --- a/demo/nodejs/horizon.js +++ b/demo/nodejs/horizon.js @@ -107,7 +107,11 @@ function FindEclipticCrossings(observer, time) { else direction = 'descends'; - console.log(`Ecliptic longitude ${s.ex.toFixed(4)} ${direction} through horizon az ${s.h.lon.toFixed(4)}, alt ${s.h.lat.toExponential(4)}`); + console.log(`Ecliptic longitude ${s.ex.toFixed(4)} ${direction} through horizon at azimuth ${s.h.lon.toFixed(4)}`); + if (Math.abs(s.h.lat) > 5.0e-7) { + console.error(`FindEclipticCrossing: excessive altitude = ${s.h.lat}`); + process.exit(1); + } } } } diff --git a/demo/nodejs/test/horizon_correct.txt b/demo/nodejs/test/horizon_correct.txt index d782c830..f126be69 100644 --- a/demo/nodejs/test/horizon_correct.txt +++ b/demo/nodejs/test/horizon_correct.txt @@ -1,2 +1,2 @@ -Ecliptic longitude 93.6460 descends through horizon az 296.3883, alt -1.7327e-7 -Ecliptic longitude 274.9704 ascends through horizon az 115.7274, alt 2.1042e-7 +Ecliptic longitude 93.6460 descends through horizon at azimuth 296.3883 +Ecliptic longitude 274.9704 ascends through horizon at azimuth 115.7274 diff --git a/demo/python/astronomy.py b/demo/python/astronomy.py index af4699db..c9ec09a1 100644 --- a/demo/python/astronomy.py +++ b/demo/python/astronomy.py @@ -170,6 +170,11 @@ class Vector: def __sub__(self, other): return Vector(self.x - other.x, self.y - other.y, self.z - other.z, self.t) + def format(self, coord_format): + """Returns a custom format string representation of the vector.""" + layout = '({:' + coord_format + '}, {:' + coord_format + '}, {:' + coord_format + '}, {})' + return layout.format(self.x, self.y, self.z, str(self.t)) + class StateVector: """A combination of a position vector, a velocity vector, and a time. @@ -758,10 +763,10 @@ class Observer: def __str__(self): text = '(' text += 'S' if (self.latitude < 0) else 'N' - text += '{}, '.format(abs(self.latitude)) + text += '{:0.8f}, '.format(abs(self.latitude)) text += 'W' if (self.longitude < 0) else 'E' - text += '{}, '.format(abs(self.longitude)) - text += '{}m'.format(self.height) + text += '{:0.8f}, '.format(abs(self.longitude)) + text += '{:0.3f}m'.format(self.height) text += ')' return text diff --git a/demo/python/camera.py b/demo/python/camera.py index 6279354d..71da36be 100755 --- a/demo/python/camera.py +++ b/demo/python/camera.py @@ -59,9 +59,9 @@ def Camera(observer, time): # Convert to unit vector. radius = vec.Length() vec.x /= radius - vec.y /= radius - vec.z /= radius - print('Moon check: x={:0.6f}, y={:0.6f}, z={:0.6f}'.format(vec.x, abs(vec.y), abs(vec.z))) + vec.y = abs(vec.y / radius) # prevent "-0" + vec.z = abs(vec.z / radius) # prevent "-0" + print('Moon check:', vec.format('0.8f')) if abs(vec.x - 1.0) > tolerance: print("Excessive error in moon check (x).") return 1 @@ -80,7 +80,7 @@ def Camera(observer, time): vec = astronomy.RotateVector(rot, sun_equ.vec) # Don't bother normalizing the Sun vector, because in AU it will be close to unit anyway. - print('Sun vector: {}'.format(vec)) + print('Sun vector:', vec.format('0.8f')) # Calculate the tilt angle of the sunlit side, as seen by the camera. # The x-axis is now pointing directly at the object, z is up in the camera image, y is to the left. diff --git a/demo/python/test/camera_correct.txt b/demo/python/test/camera_correct.txt index 00afd510..8fdc2909 100644 --- a/demo/python/test/camera_correct.txt +++ b/demo/python/test/camera_correct.txt @@ -1,6 +1,6 @@ Moon horizontal position: azimuth = 274.486, altitude = 52.101 -Moon check: x=1.000000, y=0.000000, z=0.000000 -Sun vector: (-0.08853032065573567, -0.31939127849027193, -0.9396631499852881, 2021-03-22T02:45:00.000Z) +Moon check: (1.00000000, 0.00000000, 0.00000000, 2021-03-22T02:45:00.000Z) +Sun vector: (-0.08853032, -0.31939128, -0.93966315, 2021-03-22T02:45:00.000Z) Tilt angle of sunlit side of the Moon = -108.773 degrees counterclockwise from up. Moon magnitude = -10.27, phase angle = 84.22 degrees. Angle between Moon and Sun as seen from Earth = 95.64 degrees. diff --git a/demo/python/test/triangulate_correct.txt b/demo/python/test/triangulate_correct.txt index 77f83b4e..c5e30b66 100644 --- a/demo/python/test/triangulate_correct.txt +++ b/demo/python/test/triangulate_correct.txt @@ -1,2 +1,2 @@ -Solution #1 = (N48.28914299803454, E24.562600140892187, 3862.010218580508m), err = 24.405 meters -Solution #2 = (N48.28914299803454, E24.562600140892187, 3862.010218580508m), err = 24.405 meters +Solution #1 = (N48.28914300, E24.56260014, 3862.010m), err = 24.405 meters +Solution #2 = (N48.28914300, E24.56260014, 3862.010m), err = 24.405 meters diff --git a/generate/template/astronomy.py b/generate/template/astronomy.py index f81ca7af..8d4f1df4 100644 --- a/generate/template/astronomy.py +++ b/generate/template/astronomy.py @@ -170,6 +170,11 @@ class Vector: def __sub__(self, other): return Vector(self.x - other.x, self.y - other.y, self.z - other.z, self.t) + def format(self, coord_format): + """Returns a custom format string representation of the vector.""" + layout = '({:' + coord_format + '}, {:' + coord_format + '}, {:' + coord_format + '}, {})' + return layout.format(self.x, self.y, self.z, str(self.t)) + class StateVector: """A combination of a position vector, a velocity vector, and a time. @@ -758,10 +763,10 @@ class Observer: def __str__(self): text = '(' text += 'S' if (self.latitude < 0) else 'N' - text += '{}, '.format(abs(self.latitude)) + text += '{:0.8f}, '.format(abs(self.latitude)) text += 'W' if (self.longitude < 0) else 'E' - text += '{}, '.format(abs(self.longitude)) - text += '{}m'.format(self.height) + text += '{:0.8f}, '.format(abs(self.longitude)) + text += '{:0.3f}m'.format(self.height) text += ')' return text diff --git a/source/python/README.md b/source/python/README.md index c736c717..e836026c 100644 --- a/source/python/README.md +++ b/source/python/README.md @@ -829,6 +829,11 @@ The vector also includes a time stamp. Returns the length of the vector in AU. + +### Vector.format(self, coord_format) + +Returns a custom format string representation of the vector. + --- diff --git a/source/python/astronomy.py b/source/python/astronomy.py index af4699db..c9ec09a1 100644 --- a/source/python/astronomy.py +++ b/source/python/astronomy.py @@ -170,6 +170,11 @@ class Vector: def __sub__(self, other): return Vector(self.x - other.x, self.y - other.y, self.z - other.z, self.t) + def format(self, coord_format): + """Returns a custom format string representation of the vector.""" + layout = '({:' + coord_format + '}, {:' + coord_format + '}, {:' + coord_format + '}, {})' + return layout.format(self.x, self.y, self.z, str(self.t)) + class StateVector: """A combination of a position vector, a velocity vector, and a time. @@ -758,10 +763,10 @@ class Observer: def __str__(self): text = '(' text += 'S' if (self.latitude < 0) else 'N' - text += '{}, '.format(abs(self.latitude)) + text += '{:0.8f}, '.format(abs(self.latitude)) text += 'W' if (self.longitude < 0) else 'E' - text += '{}, '.format(abs(self.longitude)) - text += '{}m'.format(self.height) + text += '{:0.8f}, '.format(abs(self.longitude)) + text += '{:0.3f}m'.format(self.height) text += ')' return text