Last active
June 4, 2019 11:28
-
-
Save jimmyhoran/bfe7b5242685fe28992f8029645ba57b to your computer and use it in GitHub Desktop.
Swift data normalisation
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 | |
/// Normalise a value to a 0...1 range. | |
func normalise(value: Double, lower: Double, upper: Double) -> Double { | |
return max(0.0, min(1.0, (value - lower) / (upper - lower))) | |
} | |
normalise(value: 0, lower: 0, upper: 2) // -> 0 | |
normalise(value: 1, lower: 0, upper: 2) // -> 0.5 | |
normalise(value: 22, lower: 0, upper: 50) // -> 0.44 | |
normalise(value: 48, lower: 0, upper: 50) // -> 0.96 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment