forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath.swift
More file actions
24 lines (21 loc) · 680 Bytes
/
Math.swift
File metadata and controls
24 lines (21 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//
// Math.swift
// LoopKitUI
//
// Created by Michael Pangburn on 3/23/19.
// Copyright © 2019 LoopKit Authors. All rights reserved.
//
func fractionThrough<Metric: FloatingPoint>(
_ value: Metric,
in range: ClosedRange<Metric>,
using transform: (Metric) -> Metric = { $0 }
) -> Metric {
let transformedLowerBound = transform(range.lowerBound)
return (transform(value) - transformedLowerBound) / (transform(range.upperBound) - transformedLowerBound)
}
func interpolatedValue<Metric: FloatingPoint>(
at fraction: Metric,
through range: ClosedRange<Metric>
) -> Metric {
fraction * (range.upperBound - range.lowerBound) + range.lowerBound
}