Math library question Most math libraries I've run into solely use radians for arguments. The problem is that nobody in the real world uses radians. How come Math libraries never seem to implement interfaces that use degrees?
Philo
I suppose they figure it's easier for you to write "degrees * Math.PI / 180", than it is for them to write all the wrappers...
Joe
I heard an explaination for this once but I dont recall the details. It has something to do with the relation to Pi and the infinite string of decimals.
Eric Debois
I probably lost touch with the real world some time ago, but I always work in radians. Mathematicians and physicists use them because it makes the equations simpler - eg if you differentiate sin(x) where x is expressed in radians you get cos(x). If you're working with complex numbers, then you can write exp(i.x) = cos(x) + i.sin(x), which simplifies the handling of some differential equations. If you try and use degrees instead, you end up with factors of 180/pi cropping up all over the place. After a while radians just come to seem the natural way to do things - just as working with logs to the base e instead of base 10 does.
as
Oh, I'm well aware of the mathematical reasons for using Pi. The only problem is that I think most developers are generally coding against the real world, where bearings are in degrees, offsets are in degrees, torque angles are in degrees...
Philo
"In fact, I cannot think of a single *practical* angle measurement in anything but degrees."
Danil
Philo, welcome to GIS.
Philo,
Ian
"You sound like someone raised on English units complaining that they cannot think of a single practical length measurement in anything but feet."
Philo
Philo, get back to work.
Bill
Yo, Philo. Radians is based off of PI...case you didnt know. So, in a matter of speaking, radians isnt all that arbitary.
Anon-y-mous Cow-ard
"? Where did you get this?"
Tom H
The x87 FPU seems to use radians instead of degrees; a math library that does the same is thinner (more efficient).
Christopher Wells
Sigh.
Matt
Should a programmer deliver a program that elegantly accomplishes the problem domain in the way that makes sense to the programmer, or...
Philo
Matt gives a great answer.
DaveF
Philo
as
Philo is making a very simple point that some people seem to be ignoring: the interface should allow for a person to pass degrees as an argument, because when it comes to angles, most people *think* in degrees. Whatever happens inside the function is irrelevant. If calculators can do it, math libraries should be able to as well.
However, now that everyone expects computer trig functions to accept radian arguments, changing them to degrees would be confusing.
Julian
Damit Philo, take the five minutes out of your busy posting schedule to write a freaking wrapper class that takes degrees or philos or whatever you want.
Dennis Atkins
As many have pointed out by now, it's a user interface problem. You should handle the conversion in the user interface, not in the underlying code that does the work.
Jan Oosting
Interesting question.
Tayssir John Gabbour
"As many have pointed out by now, it's a user interface problem"
Philo
Hmm, come to think of it, if you had optional parameters, you could flag degrees. Like in lisp,
Tayssir John Gabbour
Yeah Philo, why you using C anyway? Lisp is the way to go.
Dennis Atkins
Philo, all mathematical calculations must be done using standard units. For trigonometry this means radians. While for a novice, not using degrees, might seem unintuitive, any experienced mathematical coder can tell you that not using standard units will lead to spaghetti code and loads of bugs. Ask NASA what happens if you don't use standard units. Just accept it and life will be a thousand times easier.
Jan Derk
Okay Jan, do me a favor would you? Tell the entire global aviation community that they're using the wrong units when they measure flight courses.
Philo
I happen to have flown aircraft, hold a Master of Science in Aeronautics and have programmed helicopter simulation and performance software, which is all about transformations and trigonometry, for several years. So that might provide me with some authority. To answer you question: All those things you mention are human interfaces using non-standard units on historic grounds. Any software driving those interfaces uses standard units *under the hood*. Well, at least it should.
Jan Derk
Hmmm. That sounds awfully arrogant.
Jan Derk
IIRC grads were introduced by the french during the revolution. Sometimes used by surveyors to avoid degree's minutes' s seconds problems (i.e. it's a decimal degree)
Peter Ibbotson
Yep, as pointed out above, any radian/degree conversions should be done in the UI layer, the code that does the actual work should store intermediate results in radians. Firstly because it'd be a pointless waste of CPU cycles converting them from degrees and back again however many times whenever you need to do a calculation, and secondly because doing that will likely worsen any rounding errors in the floating point representation.
Matt
Just a quickie...
e^x is natural in the sense that it's what you get if you look for a function whose derivative is its own value (with the constraint that f(0) = 1) and then evaluate that function at x=1. It can also be defined as the limit in a compound-interest calculation as the compounding interval drops to zero.
Dan Maas
// macro to convert degrees to radians
Name
Yes, any FPU hardware will take radians. But this isn't an arbitrary choice either, as I said, the power series (or any other method for calculating sin) takes argument in radians, so the hardware needs to work with radians anyway, and would have to convert to them if it wasn't given radians.
Matt
"is e natural? it looks pretty thought up to me (for dealing with differentiation, that is)"
Danil
The reason we use degrees instead of radians goes back to ancient Egypt. They were trying to work out how time and geometry fit together. Their numbering system didn't work with fractions very well, but it serves as the basis of navigation and cartography to this day:
Tom H
Might also help to note that e, pi, and the sin and cos functions, are intimately connected, as you see when you start looking at complex numbers, and these connections all depend upon using radians for the sin and cos functions, and base e for the exponential:
Matt
Philo, you make a good point. It might be useful for the math functions to provide a degree interface for trigonometric functions. After all, most math libraries provide a function that takes a logarithm to the base 10, so there is certainly good precedent for doing this, and it would avoid some programming errors.
Dan Brown
Interesting points... but radians needn't have any connection to anything in the physical universe to define or make sense of them - sure, units with dimension can be defined so as to make certain physical constants into nice numbers, but radians are dimensionless and are a purely mathematical construct. So the analogy doesn't quite carry over...
Matt
or as people have mentioned in this thread, the math library can have a simple conversion function "float radians(float degrees)". then you say sin(radians(degrees_entered))
mb
Phile, override the damn Sin function.
Alex
Can't override a static method.
Philo
I'm embarssed to say I spent about 2 hours trying to figure out why my drawing function was producing such bizarre output. It never crossed my mind that sin() took radians until I googled a little bit. I never claimed to be brilliant :)
Lee
<quote>
Jan Derk
The oil industry is probably one of the worst offenders, they have units of measure that you simply wouldn't think possible in an international industry. They also have 'standard' calculations that only work for some weird, perverse set of inputs because who ever wrote them (IADC, SPE) rolled three or four conversion factors and maybe a constant of proportionality into a single figure. To cap the lot they also have so called engineers who insist on implementing calculations that are dimensionally incorrect.
Gaius
"The fact that all trigonometry libraries work in radians should have been a big hint, but apparently they are all wrong."
Philo
>> "1) Accuracy - sin(67 + 90) is going to be more accurate than sin(60 * Pi/180 + Pi/2) when you're working on a digital computer. (hint: Math.Pi != Pi; 60 * Math.Pi/180 != Pi/3)"
Name
Philo, in scientific and engineering applications there are rarely such concepts as "when the UI is in degrees." A component spec from one manufacturer, for example, may express something as RPM, and another express the same thing as radians per second, and another as degrees per hour. Or users in one country may always use "feet per minute," while others use "meters per second."
Ron
Philo,
hoser
WOE, I'm not sure a full defrocking is called for, just for daring to suggest the parameter to the trig functions be in degrees rather than radians.
Ron
It goes to everything wrong with the software industry. I remember talking to a guy who wrote software at a diesel engine company. He was frustrated that the mechanical engineers would not model their software requirements using Booch methodology.
hoser
"Act as the world is; do not act as you wish it was." - Philo
Dennis Atkins
My head hurts.
OffMyMeds
"Philo, in scientific and engineering applications there are rarely such concepts as "when the UI is in degrees." "
Philo
I think the difference is human -> human and human -> computer and computer -> computer communication.
Name
I think a couple things are getting conflated here.
Danil
Oops - let me clarify one thing. I *never* meant to say or imply that Math library trig functions should *only* take degrees - I was asking about either overloading the operator:
Philo
namespace Philo {
hoser
Of course I'm missing the point but for what it's worth...
Alex
Matt, I have to say, the e^iv way to express a complex number is pure beauty. Esp. e^i(pi) + 1 = 0. Most beautiful.
Marc
Fog Creek Home |