|
| 1 | +// |
| 2 | +// Percent.swift |
| 3 | +// Units |
| 4 | +// |
| 5 | +// Created by Jason Jobe on 9/5/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +/* |
| 10 | + NOTE: Should consider introducing `protocol Scalar` |
| 11 | + based on `VectorArithmetic` |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + Math operators with percentages treat the percent as its decimal equivalent (e.g., 25% = 0.25) |
| 16 | + but in the case of `+` and `-` the calculation is less direct. |
| 17 | + |
| 18 | + Here’s how math operators work with percentages in typical calculations: |
| 19 | + |
| 20 | + Multiplication (100 * 25%) |
| 21 | + |
| 22 | + When you multiply a number by a percentage, you’re finding that percent of the number. |
| 23 | + • 25% is the same as 0.25. |
| 24 | + • So, 100 * 25% = 100 * 0.25 = 25. |
| 25 | + |
| 26 | + Division (100 / 30%) |
| 27 | + |
| 28 | + Dividing by a percentage means dividing by its decimal form. |
| 29 | + • 30% is 0.3. |
| 30 | + • So, 100 / 30% = 100 / 0.3 ≈ 333.33. |
| 31 | + |
| 32 | + Addition (100 + 10%) |
| 33 | + |
| 34 | + Adding a percentage to a number is less direct, but usually means increasing the number by that percent. |
| 35 | + • 10% of 100 is 10. |
| 36 | + • So, 100 + 10% = 100 + (100 * 0.10) = 110. |
| 37 | + |
| 38 | + General Rule |
| 39 | + • Percent means “per hundred,” so 25% = 25/100 = 0.25. |
| 40 | + • Replace the percent with its decimal equivalent before performing the operation. |
| 41 | + |
| 42 | + Example Table |
| 43 | + =========== |
| 44 | + Expression Decimal Form Result |
| 45 | + ------------------------------ |
| 46 | + 100 * 25% 100 * 0.25 25 |
| 47 | + 100 / 30% 100 / 0.3 333.33 |
| 48 | + 100 + 10% 100 + (100*0.10) 110 |
| 49 | + |
| 50 | + |
| 51 | + If you see a percent sign in a calculation, just convert it to a decimal and proceed as usual. If you want to know how subtraction works with percentages, or how to handle more complex expressions, let me know! |
| 52 | + */ |
| 53 | +public struct Percent: Numeric, Equatable, Codable { |
| 54 | + |
| 55 | + public private(set) var magnitude: Double |
| 56 | + |
| 57 | + /// Create a new Percent |
| 58 | + /// - Parameters: |
| 59 | + /// - value: The magnitude of the percent |
| 60 | + public init( |
| 61 | + magnitude: Double |
| 62 | + ) { |
| 63 | + self.magnitude = magnitude |
| 64 | + } |
| 65 | + |
| 66 | + func percent(of measure: Measurement) -> Measurement { |
| 67 | + .init(value: magnitude * measure.value, unit: measure.unit) |
| 68 | + } |
| 69 | + |
| 70 | + func percent(of other: Double) -> Double { |
| 71 | + magnitude * other |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// MARK: Percent as Unit |
| 76 | +extension Percent { |
| 77 | + public var unit: Unit { Self.unit } |
| 78 | + |
| 79 | + public static let unit = Unit( |
| 80 | + definedBy: try! DefinedUnit( |
| 81 | + name: "percent", |
| 82 | + symbol: "%", |
| 83 | + dimension: [:], |
| 84 | + coefficient: 0.01 |
| 85 | + )) |
| 86 | +} |
| 87 | + |
| 88 | +// MARK: Numeric Conformance |
| 89 | +public extension Percent { |
| 90 | + init(integerLiteral value: Double) { |
| 91 | + magnitude = value |
| 92 | + } |
| 93 | + |
| 94 | + static func *= (lhs: inout Percent, rhs: Percent) { |
| 95 | + lhs.magnitude *= rhs.magnitude |
| 96 | + } |
| 97 | + |
| 98 | + static func - (lhs: Percent, rhs: Percent) -> Percent { |
| 99 | + Percent(magnitude: lhs.magnitude - rhs.magnitude) |
| 100 | + } |
| 101 | + |
| 102 | + init?<T>(exactly source: T) where T : BinaryInteger { |
| 103 | + magnitude = Double(source) |
| 104 | + } |
| 105 | + |
| 106 | + static func * (lhs: Percent, rhs: Percent) -> Percent { |
| 107 | + Percent(magnitude: lhs.magnitude * rhs.magnitude) |
| 108 | + } |
| 109 | + |
| 110 | + static func + (lhs: Percent, rhs: Percent) -> Percent { |
| 111 | + Percent(magnitude: lhs.magnitude + rhs.magnitude) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +postfix operator % |
| 116 | + |
| 117 | +extension BinaryInteger { |
| 118 | + public static postfix func % (value: Self) -> Percent { |
| 119 | + Percent(magnitude: Double(value)/100) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +extension BinaryFloatingPoint { |
| 124 | + public static postfix func % (value: Self) -> Percent { |
| 125 | + Percent(magnitude: Double(value)/100) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// AdditiveArithmetic operations `*` and `/` |
| 130 | + |
| 131 | +public extension Measurement { |
| 132 | + /// Calculate the percentage of the Measurement |
| 133 | + /// - Parameters: |
| 134 | + /// - lhs: The left-hand-side measurement |
| 135 | + /// - rhs: The right-hand-side measurement |
| 136 | + /// - Returns: A new measurement with the summed scalar values and the same unit of measure |
| 137 | + @_disfavoredOverload |
| 138 | + static func + (lhs: Measurement, rhs: Percent) -> Measurement { |
| 139 | + return Measurement( |
| 140 | + value: lhs.value + rhs.percent(of: lhs.value), |
| 141 | + unit: lhs.unit |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + @_disfavoredOverload |
| 146 | + static func - (lhs: Measurement, rhs: Percent) -> Measurement { |
| 147 | + return Measurement( |
| 148 | + value: lhs.value - rhs.percent(of: lhs.value), |
| 149 | + unit: lhs.unit |
| 150 | + ) |
| 151 | + } |
| 152 | + |
| 153 | + @_disfavoredOverload |
| 154 | + static func += (lhs: inout Measurement, rhs: Percent) { |
| 155 | + lhs = lhs + rhs |
| 156 | + } |
| 157 | + |
| 158 | + @_disfavoredOverload |
| 159 | + static func -= (lhs: inout Measurement, rhs: Percent) { |
| 160 | + lhs = lhs - rhs |
| 161 | + } |
| 162 | + |
| 163 | +} |
| 164 | + |
| 165 | +// Scalar operations `*` and `/` |
| 166 | +public extension Measurement { |
| 167 | + |
| 168 | + @_disfavoredOverload |
| 169 | + static func * (lhs: Measurement, rhs: Percent) -> Measurement { |
| 170 | + return Measurement( |
| 171 | + value: lhs.value * rhs.magnitude, |
| 172 | + unit: lhs.unit |
| 173 | + ) |
| 174 | + } |
| 175 | + |
| 176 | + @_disfavoredOverload |
| 177 | + static func / (lhs: Measurement, rhs: Percent) -> Measurement { |
| 178 | + return Measurement( |
| 179 | + value: lhs.value / rhs.magnitude, |
| 180 | + unit: lhs.unit |
| 181 | + ) |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments