What's New

1.2 – Wrapping

2022-08-30T18:42:51Z

So you want your value to be within a particular range, but instead of clamping when it gets outside that range, you want it to repeat back to the beginning of the range?

Gotcha Covered

Now that's as simple as calling .wrapped(within:) on any number!

value.wrapped(within: range)

This loops the number within the range, like this:

A chart depicting a sawtooth-style wrapping of numbers, 3.0 through 4.9, repeating left to right

Here's a code example of how this works:

print(0.wrapped(within: 3..<6)) // 3
print(1.wrapped(within: 3..<6)) // 4
print(2.wrapped(within: 3..<6)) // 5
print(3.wrapped(within: 3..<6)) // 3
print(4.wrapped(within: 3..<6)) // 4
print(5.wrapped(within: 3..<6)) // 5
print(6.wrapped(within: 3..<6)) // 3
print(7.wrapped(within: 3..<6)) // 4
print(8.wrapped(within: 3..<6)) // 5

Swift Basic Math Tools

Some basic tools for doing math in Swift

Wrapping

This allows any number to be wrapped within a range of numbers.

For example, with a range of floats from 3..<5, here's how the numbers from -10 to 10 would wrap:

A chart depicting a sawtooth-style wrapping of numbers, 3.0 through 4.9, repeating left to right

The syntax is really simple:

value.wrapped(within: range)

So for the above example, you might do:

value.wrapped(within: 3..<5)

You can also use the global function if that makes more sense for your software:

wrap(min: 3, value: value, max: 5)

Tolerable Equality

This allows you to compare two values for equality, within a certain tolerance!

This is done with a protocol that is already applied to all the language's built-in signed numbers, but which can be applied to anything.

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

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Jan 24 2024 22:27:31 GMT-1000 (Hawaii-Aleutian Standard Time)