3 ms·
Let's do a full circle. It all began with replacing frequent occurrence of 2π in calls of sin and cos functions with τ. This post suggests an optimisation by
by srean 2mo ago
Let's do a full circle.
It all began with replacing frequent occurrence of 2π in calls of sin and cos functions with τ.
This post suggests an optimisation by getting rid of τ by getting rid of radians. That way one can get rid of frequent and adjacent radians to degrees conversions and back.
I say, let's get rid of sin and cos itself !
Of course I am being over the top here. However, if you represent angle not as a scalar in degrees, radians or turns but as a tuple (sin, cos), one can usually dramatically reduce the number of calls to trigonometric functions. Rational polynomials and square root suffices. Recall rotation is a linear transformation with a matrix whose entries are in terms of sin and cosine.
As an API it might not be convenient but consider converting angles internally into a tuple of sin and cosine and keep it that way if your code frequently calls trigonometric functions.
(Aside: Sometimes I prefer keeping the tuple in terms of half angles. Tan half theta is nice to have. And I am embarrassed by the number of comments I have made on this post)
- kps 2mo agoHN has clumsy filtering, but you're allowed to write π and τ.
- srean 2mo agoThanks for the push. I was being lazy on my mobile phone. Note to readers who maybe confused by the parent comment. I was using \pi and \tau. I just noticed now that my default mobile keyboard has π.
- jacobolus 2mo agoIf you want a single number (e.g. you are trying to serialize a lot of data), in many contexts you can replace the coordinates (cos θ, sin θ) with the stereographic projection h = tan ½θ = sin θ / (1 + cos θ). Converting back and forth between these representations is cheap and easy: cos θ = (1 − h²) / (1 + h²) and sin θ = 2h / (1 + h²).
- srean 2mo agoDamn :) why did I not think of that. This would be useful for serialization deserialization. I have used half angles ½θ because there is no ambiguity about the full angle θ if I know it's sin value (and of course this holds for tan ½θ). Tan works better because one does not have to remember to take the correct branch of sqrt(1 - sin^2 θ). I learned two clever tricks in this discussion: (i) your tan ½θ and (ii) free modular arithmetic by embedding turns in signed integers.
- jacobolus 2mo agoFor more: https://en.wikipedia.org/wiki/User:Jacobolus/HalfTan https://en.wikipedia.org/wiki/User:Jacobolus/HalfTan
- srean 2mo agoOh! I did not know you were a contributing author to wikipedia. That is one fantastic page. It's a pity if it's sitting there as a draft. At the least have it on your blog. Half angles substitution is of course a common exercise trying out symbolic integration of many trigonometric integrands. That will s where I encountered it most frequently. The similarity between hyperbolic and trigonometric angle is also one of my favorite things. What I found interesting is that we think in terms of slightly different but equivalent pictures. Mine is https://upload.wikimedia.org/wikipedia/commons/1/18/Versin.png https://upload.wikimedia.org/wikipedia/commons/1/18/Versin.p... I think in terms of angle CAD and the angle subtended on the circumference on the opposite side of the center by the arc AB. Rotations tickle me just right. One thing I had thought of is turtle graphics motion on the sphere over great circles. Turns out it is not an original thought at all. https://news.ycombinator.com/item?id=48732233 https://news.ycombinator.com/item?id=48732233
- zardo 2mo agoThat will introduce an interesting new bit-bending avenue since almost all tuples of floats are not valid angle representations in that form.
- srean 2mo agoGood point. If you also incorporate scale then it is, otherwise they need to be unit normalised. The unnormalized case is essentially a complex number.
- alterom 2mo ago>As an API it might not be convenient but consider converting angles internally into a tuple of sin and cosine My friend, it's called a complex number. A unit complex number, to be specific. >Recall rotation is a linear transformation with a matrix whose entries are in terms of sin and cosine. That's what multiplication of complex numbers is. Can't wait till people here rediscover that a 3D rotation matrix has an axis-angle representation whose composition is given by quaternion multiplication. At that, children, is why learning mathematics is important.
- srean 2mo ago> Can't wait till You have to wait and inordinate amount of negative time then. Or maybe it's us who has to do the waiting for you to catch up on what HN folks in general are knowledgeable about. I have a suggestion. Scroll to the bottom of this page. You will find a search box. Plug in the names of concepts you were thinking of. Enjoy. To be honest, I too have come close to sounding a smug clueless smartass in my youth - lectured a guy on longest common subsequence not knowing that he was the original author of the file compression utility on AT&T Unix that used the same several decades ago.
- cosmojg 2mo ago[dead]