this sent me down a rabbit hole -- I asked a few models to solve that same problem, then followed up with a request to optimize it so it runs more efficiently.
chatgpt & gemini's solutions were buggy, but claude solved it, and actually found a solution that is even more efficient. It only needs to compute sqrt once per iteration. It's more complex however.
yours claude
------------------------------
Time (ns/call) 40.5 38.3
sqrt per iter 3 1
Accuracy 4.8e-7 4.8e-7
Claude's trick: instead of calling sin/cos each iteration, it rotates the existing (cos,sin) pair by the small Newton step and renormalizes: // Rotate (c,s) by angle dt, then renormalize to unit circle
float nc = c + dt*s, ns = s - dt*c;
float len = sqrt(nc*nc + ns*ns);
c = nc/len; s = ns/len;
See: https://gist.github.com/achille/d1eadf82aa54056b9ded7706e8f5...p.s: it seems like Gemini has disabled the ability to share chats can anyone else confirm this?