News for hackers. Stuff that matters.

joelgibson

Re:Turns are better than radians on Monday September 26, 2022 @10:19AM (#32983434)
There are a lot of comments here saying that radians are the only true way to deal with angles, however I agree with the author of the original article that turns are a legitimate alternative - I just wouldn't use the same language. Instead I would say that the new function I'm calculating is sin(2π t), and maybe also say that t is measured in turns, where (1 turn) = (2π rad).

It still has a nice small angle approximation: sin(2π t) ≈ 2π t for small t (arguably this is easier to interpret than sin(x) ≈ x), and its derivative is slightly more complicated: d/dt sin(2π t) = 2π cos(2π t). But everything is still perfectly workable and makes sense. I don't think you would find a mathematician or engineer surprised to come across functions such as these. (They may prefer to make the standard [1] substitution ω = 2πt if there is going to be a lot of differentiation involved, but this is a choice, not a requirement).

Turns can also be helpful as an intermediate unit which is to be translated both to an angle, and something else (colour, pitch, etc). I used turns internally for a pitch pipe application [2], where a turn became both an angle around a circle (t ↦ (cos(2πt), sin(2πt)), and a pitch moving up and down in equal temperament (t ↦ C4_FREQUENCY * 2^t). That way t=1.5 means either 1.5 octaves higher, or 1.5 full turns around the circle.

What mathematicians or engineers would be unhappy with is finding a redefinition of sin(t) to sin(2π t). Instead, lean into the fact that algebra can be a compact and unambiguous method of communication, and make a new library function called sin2π or something, and document that it calculates sin(2π t). Everyone will know what you mean.

[1]: https://en.wikipedia.org/wiki/Angular_frequency

[2]: https://www.jgibson.id.au/blog/pitch-pipe/

Re:Speedup from switch to += on Monday September 12, 2022 @02:31AM (#32806734)
This change isn't a matter of Python being slower than a compiled language, it's changing the meaning of the code. The line

  x = x + y
creates a copy of the array x, adds y to it, and then sets the variable x to that new array. In contrast, the line

  x += y
adds the array y in-place into the array x (and so hopefully no other piece of code is relying on x being immutable). This kind of trade-off occurs in pretty much all programming, for instance you see it whenever big-integer libraries are used in C++ or Rust.