From bb8eac2e991cbf3733854e38981a6862c8633749 Mon Sep 17 00:00:00 2001 From: alanwatsonforster <68709385+alanwatsonforster@users.noreply.github.com> Date: Sat, 30 Sep 2023 18:48:58 -0600 Subject: [PATCH] Fixed rounding. --- airpower/aircraft/_normalflight.py | 58 +++++++++++++----------------- airpower/math.py | 30 ++++++++++++++++ 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/airpower/aircraft/_normalflight.py b/airpower/aircraft/_normalflight.py index 586d2cd8..d78f8bbb 100644 --- a/airpower/aircraft/_normalflight.py +++ b/airpower/aircraft/_normalflight.py @@ -4,7 +4,7 @@ import math from typing_extensions import LiteralString -from airpower.math import onethird, twothirds +from airpower.math import onethird, twothirds, roundtoquarter import airpower.altitude as apaltitude import airpower.hex as aphex @@ -567,9 +567,9 @@ def determinemininitialhfp(): else: mininitialhfp = 0 if mininitialhfp == 1: - self._log("- last flight type was %s so the first FP must be an HFP." % lastflighttype) + self._log("- the first FP must be an HFP.") elif mininitialhfp > 1: - self._log("- last flight type was %s so the first %d FPs must be HFPs." % (lastflighttype, mininitialhfp)) + self._log("- the first %d FPs must be HFPs." % mininitialhfp) self._mininitialhfp = mininitialhfp @@ -577,7 +577,7 @@ def determinerequiredhfpvfpmix(): flighttype = self._flighttype lastflighttype = self._lastflighttype - fp = self._fp + fp = int(self._fp) minhfp = 0 maxhfp = fp @@ -628,40 +628,30 @@ def determinerequiredhfpvfpmix(): elif flighttype == "UL": - # See rule 8.2.2 and 8.2.3. + # See rules 8.2.2 and 8.2.3. if lastflighttype == "VD": minunloadedfp = math.floor(self._speed / 2) else: - maxvfp = 0 + minunloadedfp = 0 assert minvfp == 0 - if maxvfp != fp: - if minvfp == maxvfp: - self._log("- exactly %d FPs must be VFPs." % minvfp) - elif minvfp > 0: - assert maxvfp == fp - self._log("- at least %d FPs must be VFPs." % minvfp) - else: - assert minvfp == 0 - self._log("- at most %d FPs can be VFPs." % maxvfp) - else: - assert maxvfp == fp - if minhfp == maxhfp: - self._log("- exactly %d FPs must be HFPs." % minhfp) - elif minhfp > 0: - assert maxhfp == fp - self._log("- at least %d FPs must be HFPs." % minhfp) - else: - assert minhfp == 0 - self._log("- at most %d FPs can be HFPs." % maxhfp) - - if minunloadedhfp == maxunloadedhfp: - self._log("- exactly %d FPs must be unloaded HFPs." % minunloadedhfp) - elif minunloadedhfp > 0: - assert maxunloadedhfp == fp + assert (minvfp == 0 and maxvfp == fp) or (minhfp == 0 and maxhfp == fp) + assert minhfp == 0 or maxhfp == fp + + if maxvfp == 0: + self._log("- all FPs must be HFPs.") + elif minhfp == maxhfp: + self._log("- exactly %d FPs must be HFPs." % minhfp) + elif minhfp > 0: + self._log("- at least %d FPs must be HFPs." % minhfp) + elif maxhfp < fp: + self._log("- at most %d FPs can be HFPs." % maxhfp) + elif maxvfp < fp: + self._log("- at most %d FPs can be VFPs." % maxvfp) + + if minunloadedhfp > 0: self._log("- at least %d FPs must be unloaded HFPs." % minunloadedhfp) - else: - assert minunloadedhfp == 0 + if maxunloadedhfp > 0: self._log("- at most %d FPs can be unloaded HFPs." % maxunloadedhfp) self._minhfp = minhfp @@ -803,8 +793,8 @@ def determinealtitudeap(): # See rule 8.2.4. altitudeap = 0 - # Round to nearest 0.25. See rule 6.2. - altitudeap = int(altitudeap * 4 + 0.5) / 4 + # Round to nearest quarter. See rule 6.2. + altitudeap = roundtoquarter(altitudeap) self._altitudeap = altitudeap diff --git a/airpower/math.py b/airpower/math.py index e3e41e19..98fb2592 100644 --- a/airpower/math.py +++ b/airpower/math.py @@ -24,3 +24,33 @@ def twothirds(x): """ return x - onethird(x) + +def round(x): + + """ + Return the argument rounded to the nearest integer, breaking ties by + rounding towards zero. + """ + + if x > 0: + return int(x + 0.5) + else: + return int(x - 0.5) + +def rounftohalf(x): + + """ + Return the argument rounded to the nearest half-integer, breaking ties by + rounding towards zero. + """ + + return round(x * 2) / 2 + +def roundtoquarter(x): + + """ + Return the argument rounded to the nearest quarter-integer, breaking ties by + rounding towards zero. + """ + + return round(x * 4) / 4 \ No newline at end of file