Last active
May 18, 2020 09:06
-
-
Save ekurutepe/395e7d8fe1555c2d560ac5b72e743df5 to your computer and use it in GitHub Desktop.
Conversions in Measurement too accurate for you? Here take this…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Measurement where UnitType: Dimension { | |
func approximatelyConverted(to otherUnit: UnitType) -> Measurement<UnitType> { | |
switch (unit, otherUnit) { | |
case (UnitLength.meters, UnitLength.feet): | |
return Measurement(value: value * 3, unit: otherUnit) | |
case (UnitLength.feet, UnitLength.meters): | |
return Measurement(value: value * 3 / 10, unit: otherUnit) | |
case (UnitSpeed.knots, UnitSpeed.metersPerSecond): | |
return Measurement(value: value / 2.0, unit: otherUnit) | |
case (UnitSpeed.metersPerSecond, UnitSpeed.knots): | |
return Measurement(value: value * 2.0, unit: otherUnit) | |
default: | |
return converted(to: otherUnit) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Disclaimer: This will introduce all kind of errors in your conversions. Do NOT use in cases where you actually need the accuracy.