-
Notifications
You must be signed in to change notification settings - Fork 2
added Percent (not as Measurement) #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Units Justfile | ||
| # Command line support | ||
|
|
||
|
|
||
| # About Units Commands | ||
| _default: | ||
| @echo '{{ style("warning") }}Units Script Commands{{ NORMAL }}' | ||
| @echo @{{source_file()}} | ||
| @echo "" | ||
| @just -f {{source_file()}} --list | ||
|
|
||
| # Install 'units' in /usr/local/bin | ||
| install: | ||
| swift build -c release | ||
| cp .build/release/unit /usr/local/bin/ | ||
|
|
||
| # rm 'units' from /usr/local/bin | ||
| uninstall: | ||
| rm /usr/local/bin/unit | ||
|
|
||
| # Add swiftformat to git/pre-commit | ||
| dev_setup: | ||
| echo "./Scripts/git_commit_hook.sh" > .git/hooks/pre-commit | ||
|
|
||
| # Open Documentation | ||
| docs: | ||
| open https://swiftpackageindex.com/NeedleInAJayStack/Units/v1.0.0/documentation/units | ||
|
|
||
| git_origin := `git remote get-url origin` | ||
|
|
||
| # repo/origin | ||
| repo: | ||
| @echo {{git_origin}} | ||
|
|
||
| # open repo/origin | ||
| open-repo: | ||
| open {{git_origin}} | ||
|
|
||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| import Foundation | ||
| /* | ||
| NOTE: Should consider introducing `protocol Scalar` | ||
| based on `VectorArithmetic` | ||
| */ | ||
|
|
||
| /** | ||
| Math operators with percentages treat the percent as its decimal equivalent (e.g., 25% = 0.25) | ||
| but in the case of `+` and `-` the calculation is less direct. | ||
|
|
||
| Here’s how math operators work with percentages in typical calculations: | ||
|
|
||
| Multiplication (100 * 25%) | ||
|
|
||
| When you multiply a number by a percentage, you’re finding that percent of the number. | ||
| • 25% is the same as 0.25. | ||
| • So, 100 * 25% = 100 * 0.25 = 25. | ||
|
|
||
| Division (100 / 30%) | ||
|
|
||
| Dividing by a percentage means dividing by its decimal form. | ||
| • 30% is 0.3. | ||
| • So, 100 / 30% = 100 / 0.3 ≈ 333.33. | ||
|
|
||
| Addition (100 + 10%) | ||
|
|
||
| Adding a percentage to a number is less direct, but usually means increasing the number by that percent. | ||
| • 10% of 100 is 10. | ||
| • So, 100 + 10% = 100 + (100 * 0.10) = 110. | ||
|
|
||
| General Rule | ||
| • Percent means “per hundred,” so 25% = 25/100 = 0.25. | ||
| • Replace the percent with its decimal equivalent before performing the operation. | ||
|
|
||
| Example Table | ||
| =========== | ||
| Expression Decimal Form Result | ||
| ------------------------------ | ||
| 100 * 25% 100 * 0.25 25 | ||
| 100 / 30% 100 / 0.3 333.33 | ||
| 100 + 10% 100 + (100*0.10) 110 | ||
|
|
||
|
|
||
| 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! | ||
| */ | ||
| public struct Percent: Numeric, Equatable, Codable { | ||
|
|
||
| public private(set) var magnitude: Double | ||
|
|
||
| /// Create a new Percent | ||
| /// - Parameters: | ||
| /// - value: The magnitude of the percent | ||
| public init( | ||
| magnitude: Double | ||
| ) { | ||
| self.magnitude = magnitude | ||
| } | ||
|
|
||
| func percent(of measure: Measurement) -> Measurement { | ||
| .init(value: magnitude * measure.value, unit: measure.unit) | ||
| } | ||
|
|
||
| func percent(of other: Double) -> Double { | ||
| magnitude * other | ||
| } | ||
| } | ||
|
|
||
| extension Measurement { | ||
| public var isPercent: Bool { | ||
| self.unit == Percent.unit | ||
| } | ||
|
|
||
| public var asPercent: Percent? { | ||
| isPercent ? Percent(magnitude: self.value/100) : nil | ||
| } | ||
| } | ||
|
|
||
| // MARK: Percent as Unit | ||
| extension Percent { | ||
| public var unit: Unit { Self.unit } | ||
|
|
||
| public static let unit = Unit( | ||
| definedBy: DefaultUnits.percent) | ||
| } | ||
|
|
||
| // MARK: Numeric Conformance | ||
| public extension Percent { | ||
| init(integerLiteral value: Double) { | ||
| magnitude = value | ||
| } | ||
|
|
||
| static func *= (lhs: inout Percent, rhs: Percent) { | ||
| lhs.magnitude *= rhs.magnitude | ||
| } | ||
|
|
||
| static func - (lhs: Percent, rhs: Percent) -> Percent { | ||
| Percent(magnitude: lhs.magnitude - rhs.magnitude) | ||
| } | ||
|
|
||
| init?<T>(exactly source: T) where T : BinaryInteger { | ||
| magnitude = Double(source) | ||
| } | ||
|
|
||
| static func * (lhs: Percent, rhs: Percent) -> Percent { | ||
| Percent(magnitude: lhs.magnitude * rhs.magnitude) | ||
| } | ||
|
|
||
| static func + (lhs: Percent, rhs: Percent) -> Percent { | ||
| Percent(magnitude: lhs.magnitude + rhs.magnitude) | ||
| } | ||
| } | ||
|
|
||
| postfix operator % | ||
|
|
||
| extension BinaryInteger { | ||
| public static postfix func % (value: Self) -> Percent { | ||
| Percent(magnitude: Double(value)/100) | ||
| } | ||
| } | ||
|
|
||
| extension BinaryFloatingPoint { | ||
| public static postfix func % (value: Self) -> Percent { | ||
| Percent(magnitude: Double(value)/100) | ||
| } | ||
| } | ||
|
|
||
| // AdditiveArithmetic operations `*` and `/` | ||
|
|
||
| public extension Measurement { | ||
| /// Adds a percentage to a measurement by increasing its value by the given percent. | ||
| /// - Parameters: | ||
| /// - lhs: The base measurement. | ||
| /// - rhs: The percentage to add. | ||
| /// - Returns: A new `Measurement` with its value increased by the given percentage. | ||
| @_disfavoredOverload | ||
| static func + (lhs: Measurement, rhs: Percent) -> Measurement { | ||
| return Measurement( | ||
| value: lhs.value + rhs.percent(of: lhs.value), | ||
| unit: lhs.unit | ||
| ) | ||
| } | ||
|
|
||
| /// Subtracts a percentage from a measurement by decreasing its value by the given percent. | ||
| /// - Parameters: | ||
| /// - lhs: The base measurement. | ||
| /// - rhs: The percentage to subtract. | ||
| /// - Returns: A new `Measurement` with its value decreased by the given percentage. | ||
| @_disfavoredOverload | ||
| static func - (lhs: Measurement, rhs: Percent) -> Measurement { | ||
| return Measurement( | ||
| value: lhs.value - rhs.percent(of: lhs.value), | ||
| unit: lhs.unit | ||
| ) | ||
| } | ||
|
|
||
| /// Increases a measurement in place by the given percentage. | ||
| /// - Parameters: | ||
| /// - lhs: The measurement to modify. | ||
| /// - rhs: The percentage to add. | ||
| @_disfavoredOverload | ||
| static func += (lhs: inout Measurement, rhs: Percent) { | ||
| lhs = lhs + rhs | ||
| } | ||
|
|
||
| /// Decreases a measurement in place by the given percentage. | ||
| /// - Parameters: | ||
| /// - lhs: The measurement to modify. | ||
| /// - rhs: The percentage to subtract. | ||
| @_disfavoredOverload | ||
| static func -= (lhs: inout Measurement, rhs: Percent) { | ||
| lhs = lhs - rhs | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // Scalar operations `*` and `/` | ||
| public extension Measurement { | ||
| /// Multiplies a measurement by a percentage, treating the percent as a scalar (e.g., 25% = 0.25). | ||
| /// - Parameters: | ||
| /// - lhs: The base measurement to scale. | ||
| /// - rhs: The percentage factor. | ||
| /// - Returns: A new `Measurement` whose value is `lhs.value * rhs.magnitude` with the same unit. | ||
| @_disfavoredOverload | ||
| static func * (lhs: Measurement, rhs: Percent) -> Measurement { | ||
| return Measurement( | ||
| value: lhs.value * rhs.magnitude, | ||
| unit: lhs.unit | ||
| ) | ||
| } | ||
|
|
||
| /// Divides a measurement by a percentage, treating the percent as a scalar (e.g., 25% = 0.25). | ||
| /// - Parameters: | ||
| /// - lhs: The base measurement to scale. | ||
| /// - rhs: The percentage divisor. | ||
| /// - Returns: A new `Measurement` whose value is `lhs.value / rhs.magnitude` with the same unit. | ||
| @_disfavoredOverload | ||
| static func / (lhs: Measurement, rhs: Percent) -> Measurement { | ||
| return Measurement( | ||
| value: lhs.value / rhs.magnitude, | ||
| unit: lhs.unit | ||
| ) | ||
| } | ||
| } |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.