My favourite way of handling angles was always with either unsigned char or 16bit unsigned int that was treated as 1/nth of turn. Usually in these cases cos/sin tables were pre-calculated for speed, although that need went away to an extent. As long as as the calculations wrap around on the underlying system, it makes angles much easier to manage, because angle1 + angle2 = angle3 is always within 0 to 255 or 0 to 65535. Unfortunately I mostly work with higher level languages now that have mostly dropped integer types.
If anybody knows how similar calculations can be easily achieved in JS for example, I'd love to hear about it. I'm sure there must be a better way than boundary checks and manual wrap-around.
> If anybody knows how similar calculations can be easily achieved in JS for example
Simply "a = (a + 0x1234) & 0xffffffff;". Or whatever width you require, 0xff or 0xffff. JIT is going to optimize that and-operation away (at least for 32-bit mask 0xffffffff) and keep the integer value internally.
You can also "cast" a var to int by "ORring" 0 with it, like "a |= 0;"
Thank you! This does exactly what I meant. I think this is the best solution for my use-cases. It even handles floating point operations to a correctly, something I didn't expect.
For the same number of bits, an integer representation of angle is always going to be more precise than a floating point one for angles away from 0. It's also going to be equally precise for the whole circle.
In JavaScript, I’d stay with floating-point (don’t fight the language if you don’t have to) and use something like x => x - Math.floor(x) to normalize.
If anybody knows how similar calculations can be easily achieved in JS for example, I'd love to hear about it. I'm sure there must be a better way than boundary checks and manual wrap-around.