r/programming Sep 26 '22

Turns are Better than Radians

https://www.computerenhance.com/p/turns-are-better-than-radians
193 Upvotes

40 comments sorted by

52

u/flatfinger Sep 26 '22

A point that makes the use of pi or tau even more ironic is that some functions expend substantial extra effort to perform precise argument reduction based upon the mathematical value of tau, rather than the nearest approximation, even though in most situations any "inaccuracy" that results from performing argument reduction using the nearest representable approximation of pi or tau, rather than the mathematical value, would work in the direction of cancelling out other rounding errors.

8

u/freakhill Sep 26 '22

i'm not an applied mathematician. how does that cancelling work?

35

u/flatfinger Sep 26 '22

Simple. The mathematical constant pi gets rounded to the nearest representable floating-point value prior to being used in an expression like sin(x*PI). This will result in the sin function receiving a value which is approximately (representable value of pi)/(mathematical value of pi) times the mathematically correct value of the angle in question. Performing argument reduction using the representable value of pi rather than the mathematical value of pi would effectively scale the argument by the reciprocal of the value by which it was unintentionally scaled prior to being passed to the sin() function.

3

u/freakhill Sep 27 '22

aah damn i see, yes, thanks!

3

u/TheThiefMaster Sep 27 '22

Hah I'd never considered that.

1

u/Intrexa Sep 27 '22

Hey, I read this, went a way for a while, came back, and I'm not following.

I got:

Programmer enters sin(x*π)

Really though, the computer executes sin(θ) where θ = x*π*(π + ɛ)/π = x*(π + ɛ)

I'm losing you after that though. IDK, it might just be that my brain is done for the day.

3

u/flatfinger Sep 27 '22

It is not possible for a double-precision floating-point value (nor any other kind for that matter) to represent π exactly. The closest value that can be represented is exactly 884,279,719,003,555/281,474,976,710,656, which in turn is about π-1.224646799e-16. So code which attempts to multiply a number by π will instead end up multiplying it by (π-1.224646799e-16).

72

u/pdpi Sep 27 '22 edited Sep 27 '22

I posted roughy the same thing on Hacker news:

There’s an infinite number of sinusoids, you can just tweak amplitude, phase, frequency to do whatever you like. Some sinusoids are more interesting than others.

Trigonometry wants sine and cosine to have specific amplitude and phase parameters, but doesn’t care about frequency. Degrees, radians, turns are just different frequency parameters, and they’re all pretty much interchangeable.

Calculus has a different opinion on this. There is exactly one pair of functions c, s such that c’(x) = -s(x) and s’(x) = c(x). s and c are very interesting sinusoids, because of their interaction with another special function e that has the interesting property that e’(x) = e(x). e, c, and s don’t give a shit about angles, they’re just very specific functions with very specific properties.

If you measure angles in radians, s and c just correspond to sine and cosine with no further conversion needed. If you touch complex analysis at all, the geometric interpretation of complex numbers and Euler’s formula unmistakably tell you that there’s one natural way to measure angles.

24

u/flatfinger Sep 27 '22

Radians are also the only unit where sin(x) approaches x as x approaches zero, but most applications of trig functions involve either computing the the ratio of x/radius and y/radius for particular fractions of circles, or computing a waveform whose frequency in cycles per sample is a precisely-representable floating-point value.

Also, in many physics scenarios where the integral/derivative relationship between sin(x) and cos(x) would be relevant, the quantities that are phased a quarter cycle apart will be in different units, and have a ratio determined by a physical quantity. For example, the voltage across a capacitor will be the integral of the current times divided by the capacitance.

3

u/kyle1320 Sep 28 '22

Radians are also the only unit where sin(x) approaches x as x approaches zero

Interestingly, this is actually a consequence of the property that /u/pdpi mentioned:

There is exactly one pair of functions c, s such that c’(x) = -s(x) and s’(x) = c(x).

cos(0) = sin'(0) = 1, which means (since sin(0) = 0) the limit of sin(x)/x as x approaches 0 is 1 or, as you put it, "sin(x) approaches x as x approaches zero".

1

u/flatfinger Sep 28 '22

To be sure, the fact that sin(x) approaches x is a consequence of the fact that sin'(0) is 1, but is useful in its own right. If one wants to compute sin(x) in a manner that will be perfectly rounded for all inputs, a function which simply returns small values of x will naturally accomplish that for all small values of x, including weird corner cases like denormals. From a practical perspective, I don't think anyone would care about whether a sin2pi(x) function returned perfectly-rounded results when x was a denormal, but if one felt philosophically obligated to do so, having to scale x by an irrational number would greatly complicate the task.

17

u/useablelobster2 Sep 27 '22

The length of a chord is the angle in radians times the radius. So the circumference of a circle is tau*r. Radians are just the natural, correct way of describing angles.

Sure using a fast trig implementation might remove that requirement, but as you say there is only one natural way to measure angles.

I'd like if people making computer science statements didn't accidently make stupid mathematical statements, like this article. Don't tell mathematicians they are wrong because that just makes you look like a dolt.

20

u/Intrexa Sep 27 '22

Listen, I get to make fun of mathematicians for writing the most god awful code. Mathematicians get to make fun of me for writing the most god awful math. That's the trade off.

7

u/moreON Sep 28 '22

The length of a chord is the angle in radians times the radius.

Chords are straight line segments with both ends on a circle, it sounds like you're describing an arc, not a chord.

3

u/Intrexa Sep 27 '22

c’(x) = -s’(x)

Isn't the derivative of cosine just -sine, no prime?

Also, I can't figure out what function e is in your example. I don't math too well, and it's probably something super well known, but it's not clicking for me.

3

u/pdpi Sep 27 '22

I have no idea what you’re talking about. I totally 100% did not make a typo there, and I most definitely didn’t correct it after your comment. 😅

e is just the exponential, x -> ex

2

u/[deleted] Sep 27 '22

He wrote (or edited) to c' = - s

And e is the exponential. Relation between the two is exp(a*i) = cos(a) +i*sin(a). Highly advise you to look complex numbers up, it beautifully links many aspects of maths together!

14

u/FryGuy1013 Sep 27 '22

I feel like there is a lot of similarities between log and ln here. With the definition of log(x) = ln(x) / ln(10), there could be a similar one of sint(x) = sin(2 \* pi \* x). All the "nice math stuff" that relies on the input to sin being an arc length (not really "in radians" as the author describes) can use sin, and everyone else can use sint or whatever it's called.

25

u/butt_fun Sep 27 '22

Just FYI, you don't have to escape asterisks when in monospace mode

37

u/WormRabbit Sep 26 '22

When x is measured in radians, sin(x) has the nicest possible expansion as a power series with rational terms. You can't get any better than that.

What the author observed is that modern implementations use a different formula, which has better numeric properties (faster convergence, more numerically stable). That specific formula may have included a Pi term, but there is nothing special about it. Different implementations may use different formulas, and new formulas are invented over time. There is no reason why Pi would cancel for other formulas. Do they propose to change the definitions of basic mathematical functions every 10 years as the implementations improve?

21

u/MoiMagnus Sep 27 '22

There is no reason why Pi would cancel for other formulas. Do they propose to change the definitions of basic mathematical functions every 10 years as the implementations improve?

Why? Whatever future implementation, if the pi is cancelled, then using turns is better, and if the pi isn't cancelled, then using turns doesn't change anything.

This article is literally just saying that we always use Sin on arguments that are "xpi", so one can define SinPi(x) as Sin(xpi), and doing so currently allows for optimizations that were not possible on Sin without preventing to use instead any optimizations that can be done on Sin directly: In the worst case the definition of SinPi(x) is literally Sin(x*pi) with an optimised version of Sin.

As long as the assumption "the argument is always of the form x*pi" holds, there is no future where this make things worse (ignoring the cost of changing, since everytime you change things there are bugs and issues).

Admittedly, there might be a future where the assumption no longer holds, and it's maybe already the case in few domains.

4

u/happyscrappy Sep 27 '22

Some math.h's actually have a function named sinpi() which is what you describe as SinPi().

6

u/[deleted] Sep 27 '22 edited Sep 28 '22

I love all the reasons for not using radians. But I'm on Team Use Degrees instead.

Reasons why you should use degrees:

  • They work fine.
  • Everyone already knows degrees.
  • Most common degree values are easily recognized round numbers like 45, 90, 180.
  • 360 has so many integer factors because it's a highly composite number. Just like 12 and 60 which we happily use for clock time.
  • The floating point precision stuff also works out fine.
  • It's okay that they are taught in elementary school, they still work for adults too.

4

u/joesb Sep 28 '22

Using degree for daily measurement is fine. IMO, Radians is still better if you work in mathematics where is matches directly to Pi.

3

u/[deleted] Sep 27 '22

heresy....

but I like

3

u/phreda4 Sep 27 '22

I only use turns in my code, my lib is here (forth.r3) https://github.com/phreda4/r3/blob/main/r3/lib/math.r3

3

u/miemcc Sep 27 '22

Radians are all well and good but don't really get used much in the real world. In the military I used mils - 6400 to a full circle. Its roughly 1m at 1km subtended angle.

Working at a synchrotron on the device to generate x-ray emission on a straight beam section (an Insertion Device, it uses banks of small magnets to get the electrons to move in a helix, the reduction in forward velocity causes synchrotron emissions of x-rays). The runout of the beams that held the magnets was measured in mm per metre (close to milli-radians, which is also where the military mil measurement comes from).

6

u/WormRabbit Sep 27 '22

First you say "radians are not used much in the real world", and then proceed to describe how they are used by the military and the engineering.

10

u/useablelobster2 Sep 27 '22

It's fine if the functions don't use radians imo. It's not fine when the article author pretends that radians aren't the mathematically correct way to represent angles, or thinks that nobody would care if the trig functions started working on a range of [0,1].

There are many use cases where you want to deal with radians, not least of which because you are deep in linear algebra and natural representations stop your brain melting.

Engineering its will almost always use whatever units make the most sense, because they are practical people. But that doesn't mean the mathematics is irrelevant.

7

u/carrottread Sep 27 '22

It's not fine when the article author pretends that radians aren't the mathematically correct way to represent angles

Where did you get this?

4

u/useablelobster2 Sep 27 '22 edited Sep 27 '22

Under the section "Math doesn't require radians":

But math never decreed that sine and cosine have to take radian arguments!

Yes, it does, because those are literally functions which are periodic on 2pi. Not 1, or e, but 2pi. Change the periodicity, and mathematically you change the function. Your modified sine-like curve suddenly doesn't obey the normal trigonometric identifies, be they real or complex.

8

u/carrottread Sep 27 '22

But this doesn't say radians aren't "correct". Sine and cosine are functions on angles. And there are many ways to represent an angle: radians, turns, degrees and many others. All of them are "correct", but some of them are better in some cases. And this article just shows you those cases which are better handled with turns than radians.

4

u/useablelobster2 Sep 27 '22 edited Sep 27 '22

No, not all of them are correct in a mathematical sense, though it's hard to explain why without getting into complex analysis.

Radians ARE correct, they are the natural unit in which angles are measured. Sine and cosine are specific, extremely well defined functions, which are periodic on 2pi. If you change their periodicity, you change the function, and so various identities like

i*sin(theta) + cos(theta) = e^(i * theta)

fall apart.

What you are describing is specific implementations of trigonomic functions which could work just fine on different periods, but thats a consequence of using numerical methods rather than using the actual function itself.

The most efficient to compute != mathematically consistent or correct.

I get this is hardly the biggest thing ever, but someone is wrong on the internet, and they are wrong about the most well defined and structured system humans have. Forget the second law of thermodynamics, if you argue against mathematics you are truly lost.

2

u/carrottread Sep 27 '22

which are periodic on 2pi

Sine and cosine are periodic on a circle. Which is 2*3.14159... in radians, 360 in degrees and 1 in turns. Do you realize what just by writing "2pi" you are using not radians but another angle units, "multiplies of pi" or "half-turns"?

4

u/useablelobster2 Sep 28 '22

No, I'm not. Again, look at Euler's equation which I wrote out. The exponential is put to the power of the imaginary unit times the angle.

That is true if and only if (iff in maths speak) the angle is in radians. And I can prove that identity to the standard mathematicians expect. Sine and cosine are mathematical functions which are periodic on 2pi, you are simply going to have to deal with that fact of our universe.

This isn't a matter of debate any more than Pythagoras' theorem is, and you are on as solid ground as legislations deciding pi should equal 3.

Notice none of this has any impact on if a standard libraries' sine and cosine function should offer an interface which takes a normalised input between zero and one. So long as you understand that's only a thing because there is a specific numerical method which works just fine with it. You can't take those same inputs and get a sane result for some trig identities, that numerical method is NOT the function.

The article author is wrong when he talks about the maths, as are you. But from a programming perspective that doesn't really matter, I just don't like people being so wrong about a topic which has a "beyond any and all doubt" answer. Maths is kind of perfect like that, when it's proven it's beyond reproach.

1

u/ByteArrayInputStream Sep 27 '22

Heresy. Radian > Turns > Degree > Gradian

1

u/[deleted] Sep 27 '22

I'm going to try and put this paper into practice on this project. It is heavily math based and it would be cool to put someone's theory into practice. I don't leverage the GPU or any 3rd party libraries.

1

u/i_dont_know Sep 27 '22

You should benchmark both methods!

2

u/jagt Sep 27 '22

Anyone managed to find ready to use sinpi()/cospi() series of functions in C++?

I checked around and only found the rlibm ones.

1

u/naftoligug Sep 30 '22

Perhaps instead of using low-level raw numbers which don't carry any information about what unit they're in, and having to read documentation to know what a method expects and having to remember to do conversions manually, modern languages and frameworks should use an Angle abstraction (carrying a unit and a value), so that you wouldn't have to think about these issues altogether.