wrap()

Restricts a value to an interval [low, high) by wrapping it around.

Code

Result

wrap(1.0, 1.0, 5.0)

1.0

wrap(3.0, 1.0, 5.0)

3.0

wrap(4.9999, 1.0, 5.0)

4.9999

wrap(5.0, 1.0, 5.0)

1.0

wrap(10.0, 1.0, 5.0)

1.0

wrap(13.0, 1.0, 5.0)

3.0

Two alternative versions are provided:

Version

Equivalent to

wrap(x, high)

wrap(x, 0.0, high)

wrap01(x)

wrap(x, 0.0, 1.0)

Example

Ramp LED up and then back to zero once every 10 second:

#include <Plaquette.h>

AnalogOut led(9);

void begin() {
}

void step() {
   wrap(seconds(), 10.0) >> led;
}

Reference

float pq::wrap(double x, double low, double high)

Restricts value to the interval [low, high) by wrapping it around.

Parameters:
  • x – the value to wrap

  • low – the lower boundary

  • high – the higher boundary

Returns:

the value wrapped around [low, high) or [high, low) if high < low

float pq::wrap(double x, double high)

Restricts value to the interval [0, high) by wrapping it around.

Parameters:
  • x – the value to wrap

  • high – the higher bound

Returns:

the value wrapped around [0, high) or [high, 0) if high is negative

float pq::wrap01(double x)

Restricts value to the interval [0, 1) by wrapping it around.

Parameters:

x – the value to wrap

Returns:

the value wrapped around [0, 1).

See Also