|
| 1 | +import Foundation |
| 2 | +/* |
| 3 | + NOTE: Should consider introducing `protocol Scalar` |
| 4 | + based on `VectorArithmetic` |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + Math operators with percentages treat the percent as its decimal equivalent (e.g., 25% = 0.25) |
| 9 | + but in the case of `+` and `-` the calculation is less direct. |
| 10 | + |
| 11 | + Here’s how math operators work with percentages in typical calculations: |
| 12 | + |
| 13 | + Multiplication (100 * 25%) |
| 14 | + |
| 15 | + When you multiply a number by a percentage, you’re finding that percent of the number. |
| 16 | + • 25% is the same as 0.25. |
| 17 | + • So, 100 * 25% = 100 * 0.25 = 25. |
| 18 | + |
| 19 | + Division (100 / 30%) |
| 20 | + |
| 21 | + Dividing by a percentage means dividing by its decimal form. |
| 22 | + • 30% is 0.3. |
| 23 | + • So, 100 / 30% = 100 / 0.3 ≈ 333.33. |
| 24 | + |
| 25 | + Addition (100 + 10%) |
| 26 | + |
| 27 | + Adding a percentage to a number is less direct, but usually means increasing the number by that percent. |
| 28 | + • 10% of 100 is 10. |
| 29 | + • So, 100 + 10% = 100 + (100 * 0.10) = 110. |
| 30 | + |
| 31 | + General Rule |
| 32 | + • Percent means “per hundred,” so 25% = 25/100 = 0.25. |
| 33 | + • Replace the percent with its decimal equivalent before performing the operation. |
| 34 | + |
| 35 | + Example Table |
| 36 | + =========== |
| 37 | + Expression Decimal Form Result |
| 38 | + ------------------------------ |
| 39 | + 100 * 25% 100 * 0.25 25 |
| 40 | + 100 / 30% 100 / 0.3 333.33 |
| 41 | + 100 + 10% 100 + (100*0.10) 110 |
| 42 | + |
| 43 | + |
| 44 | + 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! |
| 45 | + */ |
| 46 | +public struct Percent: Numeric, Equatable, Codable { |
| 47 | + |
| 48 | + public private(set) var magnitude: Double |
| 49 | + |
| 50 | + /// Create a new Percent |
| 51 | + /// - Parameters: |
| 52 | + /// - value: The magnitude of the percent |
| 53 | + public init( |
| 54 | + magnitude: Double |
| 55 | + ) { |
| 56 | + self.magnitude = magnitude |
| 57 | + } |
| 58 | + |
| 59 | + func percent(of measure: Measurement) -> Measurement { |
| 60 | + .init(value: magnitude * measure.value, unit: measure.unit) |
| 61 | + } |
| 62 | + |
| 63 | + func percent(of other: Double) -> Double { |
| 64 | + magnitude * other |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +extension Measurement { |
| 69 | + public var isPercent: Bool { |
| 70 | + self.unit == Percent.unit |
| 71 | + } |
| 72 | + |
| 73 | + public var asPercent: Percent? { |
| 74 | + isPercent ? Percent(magnitude: self.value/100) : nil |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// MARK: Percent as Unit |
| 79 | +extension Percent { |
| 80 | + public var unit: Unit { Self.unit } |
| 81 | + |
| 82 | + public static let unit = Unit( |
| 83 | + definedBy: DefaultUnits.percent) |
| 84 | +} |
| 85 | + |
| 86 | +// MARK: Numeric Conformance |
| 87 | +public extension Percent { |
| 88 | + init(integerLiteral value: Double) { |
| 89 | + magnitude = value |
| 90 | + } |
| 91 | + |
| 92 | + static func *= (lhs: inout Percent, rhs: Percent) { |
| 93 | + lhs.magnitude *= rhs.magnitude |
| 94 | + } |
| 95 | + |
| 96 | + static func - (lhs: Percent, rhs: Percent) -> Percent { |
| 97 | + Percent(magnitude: lhs.magnitude - rhs.magnitude) |
| 98 | + } |
| 99 | + |
| 100 | + init?<T>(exactly source: T) where T : BinaryInteger { |
| 101 | + magnitude = Double(source) |
| 102 | + } |
| 103 | + |
| 104 | + static func * (lhs: Percent, rhs: Percent) -> Percent { |
| 105 | + Percent(magnitude: lhs.magnitude * rhs.magnitude) |
| 106 | + } |
| 107 | + |
| 108 | + static func + (lhs: Percent, rhs: Percent) -> Percent { |
| 109 | + Percent(magnitude: lhs.magnitude + rhs.magnitude) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +postfix operator % |
| 114 | + |
| 115 | +extension BinaryInteger { |
| 116 | + public static postfix func % (value: Self) -> Percent { |
| 117 | + Percent(magnitude: Double(value)/100) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +extension BinaryFloatingPoint { |
| 122 | + public static postfix func % (value: Self) -> Percent { |
| 123 | + Percent(magnitude: Double(value)/100) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// AdditiveArithmetic operations `*` and `/` |
| 128 | + |
| 129 | +public extension Measurement { |
| 130 | + /// Adds a percentage to a measurement by increasing its value by the given percent. |
| 131 | + /// - Parameters: |
| 132 | + /// - lhs: The base measurement. |
| 133 | + /// - rhs: The percentage to add. |
| 134 | + /// - Returns: A new `Measurement` with its value increased by the given percentage. |
| 135 | + @_disfavoredOverload |
| 136 | + static func + (lhs: Measurement, rhs: Percent) -> Measurement { |
| 137 | + return Measurement( |
| 138 | + value: lhs.value + rhs.percent(of: lhs.value), |
| 139 | + unit: lhs.unit |
| 140 | + ) |
| 141 | + } |
| 142 | + |
| 143 | + /// Subtracts a percentage from a measurement by decreasing its value by the given percent. |
| 144 | + /// - Parameters: |
| 145 | + /// - lhs: The base measurement. |
| 146 | + /// - rhs: The percentage to subtract. |
| 147 | + /// - Returns: A new `Measurement` with its value decreased by the given percentage. |
| 148 | + @_disfavoredOverload |
| 149 | + static func - (lhs: Measurement, rhs: Percent) -> Measurement { |
| 150 | + return Measurement( |
| 151 | + value: lhs.value - rhs.percent(of: lhs.value), |
| 152 | + unit: lhs.unit |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + /// Increases a measurement in place by the given percentage. |
| 157 | + /// - Parameters: |
| 158 | + /// - lhs: The measurement to modify. |
| 159 | + /// - rhs: The percentage to add. |
| 160 | + @_disfavoredOverload |
| 161 | + static func += (lhs: inout Measurement, rhs: Percent) { |
| 162 | + lhs = lhs + rhs |
| 163 | + } |
| 164 | + |
| 165 | + /// Decreases a measurement in place by the given percentage. |
| 166 | + /// - Parameters: |
| 167 | + /// - lhs: The measurement to modify. |
| 168 | + /// - rhs: The percentage to subtract. |
| 169 | + @_disfavoredOverload |
| 170 | + static func -= (lhs: inout Measurement, rhs: Percent) { |
| 171 | + lhs = lhs - rhs |
| 172 | + } |
| 173 | + |
| 174 | +} |
| 175 | + |
| 176 | +// Scalar operations `*` and `/` |
| 177 | +public extension Measurement { |
| 178 | + /// Multiplies a measurement by a percentage, treating the percent as a scalar (e.g., 25% = 0.25). |
| 179 | + /// - Parameters: |
| 180 | + /// - lhs: The base measurement to scale. |
| 181 | + /// - rhs: The percentage factor. |
| 182 | + /// - Returns: A new `Measurement` whose value is `lhs.value * rhs.magnitude` with the same unit. |
| 183 | + @_disfavoredOverload |
| 184 | + static func * (lhs: Measurement, rhs: Percent) -> Measurement { |
| 185 | + return Measurement( |
| 186 | + value: lhs.value * rhs.magnitude, |
| 187 | + unit: lhs.unit |
| 188 | + ) |
| 189 | + } |
| 190 | + |
| 191 | + /// Divides a measurement by a percentage, treating the percent as a scalar (e.g., 25% = 0.25). |
| 192 | + /// - Parameters: |
| 193 | + /// - lhs: The base measurement to scale. |
| 194 | + /// - rhs: The percentage divisor. |
| 195 | + /// - Returns: A new `Measurement` whose value is `lhs.value / rhs.magnitude` with the same unit. |
| 196 | + @_disfavoredOverload |
| 197 | + static func / (lhs: Measurement, rhs: Percent) -> Measurement { |
| 198 | + return Measurement( |
| 199 | + value: lhs.value / rhs.magnitude, |
| 200 | + unit: lhs.unit |
| 201 | + ) |
| 202 | + } |
| 203 | +} |
0 commit comments