by flowerbreeze · 4y ago · 0 comments
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.
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;"
index = angle1 + angle2 | 0
Will keep things an integer. The | requires casting to int, the 0 makes it a no-op after the cast.This is from asm.js which had to emulate integers so they looked through what it would take
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.
> a = new Uint16Array(1)
Uint16Array(1) [ 0 ]
> a[0]
0
> a[0] = a[0] + 65537
65537
> a[0]
1