Skip to content

Instantly share code, notes, and snippets.

@jepler
Created February 24, 2022 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepler/d1a2440469e2228ad50073a3a94c2917 to your computer and use it in GitHub Desktop.
Save jepler/d1a2440469e2228ad50073a3a94c2917 to your computer and use it in GitHub Desktop.
from pint import UnitRegistry
ureg = UnitRegistry()
def conv_round(base, conv):
return round(base.to(conv))
def gain(base, conv):
val = conv_round(conv)
return (val/base).magnitude
units = (
ureg['m/s'],
ureg['knots'],
ureg['fathoms/sec'],
ureg['furlongs/min'],
ureg['kph'],
ureg['furlongs/hour'],
ureg['mph'],
ureg['yards/sec'])
v = 17 * ureg('mph')
e = 45 * ureg('mph')
while v < e:
print(f"{str(v):48} {round(v.to('mph'), 3):36}")
v = max(conv_round(v, u) for u in units)
print(f"{str(v):48} {round(v.to('mph')):36}")
@Lythimus
Copy link

Classy.

@Kaligule
Copy link

gain is never used, is it?

@chrisdl
Copy link

chrisdl commented Feb 24, 2022

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment