Mapping degrees excluding 360.0

I’ve been working on a control that behaves like an encoder wheel that is turned with the mouse and outputs the current angle of rotation.

I’m having trouble figuring out how to get it to output 0 through 360 but forcing 360 to output 0.

When I try to use the output value, it gets messed up in the small space where 359 changes to 360, and then to 0. Using a map().

I want a float value in the range of 0-360 excluding 360. Is there a way to do that?

The code I have is really long including the encoder class and all the mouse testing etc. and it’s a bit of a mess so I’m not including it, hoping someone would know how to solve the problem conceptually.

// Assumes that all angles are positive and 
// values > 360 wrap round and start again
float angle;
angle = 3.56; println(angle, angle % 360);
angle = 356.8; println(angle, angle % 360);
angle = 359.999; println(angle, angle % 360);
angle = 360.0; println(angle, angle % 360);
angle = 360.01; println(angle, angle % 360);

produces the output

3.56     3.56
356.8    356.8
359.999  359.999
360.0    0.0
360.01   0.010009766

Notice the floating point rounding error on the last one. This is so small it will not cause a problem.

1 Like

Thank you, I did try using %360, but it didn’t work. I probably have to clean up my mess and try it again. I can see how it does actually work here in your example