Skip to content

Instantly share code, notes, and snippets.

@jimmyhoran
Last active June 4, 2019 11:28
Show Gist options
  • Save jimmyhoran/bfe7b5242685fe28992f8029645ba57b to your computer and use it in GitHub Desktop.
Save jimmyhoran/bfe7b5242685fe28992f8029645ba57b to your computer and use it in GitHub Desktop.
Swift data normalisation
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