wrap

Wraps a numeric value between 0 and a max value.

T
wrap
(
string bound = ")"
T
)
()
if (
isNumeric!T &&
(
bound == ")" ||
bound == "]"
)
)

Parameters

bound

a string that indicates if the max is excluded (")), the default or if the max is included ("]").

value
Type: T

the value to wrap. max the maximal value.

Examples

1 import std.math: approxEqual;
2 import std.math: modf;
3 // wrap from max
4 assert(wrap(1,1) == 1);
5 assert(wrap(3,2) == 1);
6 assert(wrap(-1,3) == 2);
7 assert(wrap(1.5,1).approxEqual(0.5));
8 assert(wrap(1.01,1).approxEqual(0.01));
9 assert(wrap(-0.5,2).approxEqual(1.5));
10 // wrap past max
11 assert(wrap!"]"(1,1) == 0);
12 assert(wrap!"]"(3,2) == 1);
13 assert(wrap!"]"(3,3) == 0);
14 assert(wrap!"]"(1.0,1.0) == 0.0);
15 assert(wrap!"]"(-0.5,1.0) == 0.5);
16 // two phases in sync
17 double incr = 0.0125;
18 double sync = 0.25;
19 double phase1 = 0, phase2 = 0;
20 foreach(i; 0 .. 100000)
21 {
22     phase1 = wrap(phase1 + incr, 1.0);
23     phase2 = wrap(phase1 - sync, 1.0);
24     assert(phase1 < 1.0 && phase2 < 1.0);
25 }

Meta