Skip to content

Commit

Permalink
Fixed rounding.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwatsonforster committed Oct 1, 2023
1 parent 5b46d4a commit bb8eac2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
58 changes: 24 additions & 34 deletions airpower/aircraft/_normalflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -567,17 +567,17 @@ 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

def determinerequiredhfpvfpmix():

flighttype = self._flighttype
lastflighttype = self._lastflighttype
fp = self._fp
fp = int(self._fp)

minhfp = 0
maxhfp = fp
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions airpower/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bb8eac2

Please sign in to comment.