Swiftpack.co - RougeWare/Swift-Basic-Math-Tools as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by RougeWare.
RougeWare/Swift-Basic-Math-Tools 1.1.0
Some basic tools for doing math in Swift
⭐️ 0
🕓 6 weeks ago
.package(url: "https://github.com/RougeWare/Swift-Basic-Math-Tools.git", from: "1.1.0")

Swift Basic Math Tools

Some basic tools for doing math in Swift

TolerablyEqual

This is a protocol that is applied to all the language's built-in signed numbers, but which can be applied to anything, which lets you compare two values for equality, within a certain tolerance.

Let's look at this classic example:

let shouldBeOne = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1

print(shouldBeOne, shouldBeOne == 1)
// prints 0.9999999999999999 false

With this package, you can use the ~== operator to easily overcome this problem

print(shouldBeOne ~== 1)
// prints true

You can also customize this behavior as needed:

print(1_000_000.equals(1_000_100, tolerance: 1_000))
// prints true

Feel free to check out the tests for more examples!

Clamps

This includes a few functions which make it easy to clamp a value between two others. Like Swift's min and max, these work for any Comparable type.

Whichever you use simply depends on your preference or needs; they all act identically:

print(clamp(min: 2, value:  0, max: 7)) // Prints 2
print(clamp(min: 2, value:  5, max: 7)) // Prints 5
print(clamp(min: 2, value: 99, max: 7)) // Prints 7

print( 0.clamping(min: 2, max: 7)) // Prints 2
print( 5.clamping(min: 2, max: 7)) // Prints 5
print(99.clamping(min: 2, max: 7)) // Prints 7

print( 0.clamping(within: 2...7)) // Prints 2
print( 5.clamping(within: 2...7)) // Prints 5
print(99.clamping(within: 2...7)) // Prints 7

GitHub

link
Stars: 0
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

1.1 - Clamps
6 weeks ago

This update introduces some simple clamp functions! These make it easy to clamp a value between two others. Like Swift's min and max, these work for any Comparable type.

Whichever you use simply depends on your preference or needs; they all act identically:

print(clamp(min: 2, value:  0, max: 7)) // Prints 2
print(clamp(min: 2, value:  5, max: 7)) // Prints 5
print(clamp(min: 2, value: 99, max: 7)) // Prints 7

print( 0.clamping(min: 2, max: 7)) // Prints 2
print( 5.clamping(min: 2, max: 7)) // Prints 5
print(99.clamping(min: 2, max: 7)) // Prints 7

print( 0.clamping(within: 2...7)) // Prints 2
print( 5.clamping(within: 2...7)) // Prints 5
print(99.clamping(within: 2...7)) // Prints 7

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics