Fix an algoritm

Job ID: 34385720

Budget: €8 – €30 EUR

I have a simple function that takes a float parameter x in the range [-1.0, 2.0] and maps it to range [0.0, 1.0] such that values below 0.0 are mapped to 1.0 and values above 1.0 are mapped to 0.0:

float wrap_ternary(float x) {
// result is between [0.0, 1.0]
return x < 0.0 ? x + 1.0 : x > 1.0 ? x - 1.0 : x;
}

I want to convert that function to use math expressions instead of conditionals to avoid branching. I've come up with the following algorithm:

float wrap_mod(float x) {
return (((x + 1.0) % 2.0 + 1.0) % 1.0);
}

This seems close however the edge wrapping of this algorithm is inclusive i.e. 1.0/2.0 are mapped to 0.0 whereas the ternary version would map these values to 1.0 as seen by the results table attached to this post. (generated by stepping through the x-range [-1, 2] in increments of 0.5):

How would I modify my algorithm so that it produces the same results as the ternary version without using conditional logic? I've tried to subtract EPSILON from my modulo but that didn't really work... I just can't wrap my head around this right now. No pun intended.
Related categories: Algorithm Mathematics Graphics Programming