JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.63k stars 5.48k forks source link

Define the degree symbol (°) as the DEG_TO_RAD constant #1683

Closed simleb closed 11 years ago

simleb commented 11 years ago

Mathematica defines ° as pi/180 so you can write degrees naturally, like in Sin[90°].

In julia, it's as easy as typing ° = pi / 180:

julia> 180°
3.141592653589793

julia> sin(90°)
1.0

I don't see any other clever use of ° so what about making it a constant?

wlbksy commented 11 years ago

Isn't hard to type this symbol. Even I do have a Input Method, I have to turn to Soft Keyboard to type it. If one really needs this, a simpler alternative would be better. Otherwise, pi / 180 remains a clear way of maintaining codes.

simleb commented 11 years ago

Oh, I didn't realize that. I have a french keyboard (azerty) and ° is just a Shift + ) away!

No one really needs this. It's a detail but it doesn't hurt and is even quite pleasant to use (for french keyboard users at least!).

Keno commented 11 years ago

Same on German keyboards

carlobaldassi commented 11 years ago

International version: ComposeKey+o+o -> °

pao commented 11 years ago

Windows version: Alt + NumPad 0, 1, 7, 6

wlbksy commented 11 years ago

Personally, I like this note which I have used since primary school on paper. Seems it helps people a lot, I fully approve this. Chinese Input Method even has a quite simple workaround for typing this symbol (type du which is Chinese pronunciation of "degree"), just curious about people using laptop with EN_US keyboards and no NumPad

wlbksy commented 11 years ago

http://en.wikipedia.org/wiki/%C2%B0#Keyboard_entry find this...pathetic for laptops

simonbyrne commented 11 years ago

Mathematica defines ° as pi/180 so you can write degrees naturally, like in Sin[90°].

That's not quite true: typing 180° returns 180°, it just acts like pi (at least it does in version 8).

It could be possible to recreate similar behaviour in Julia defining a new type, say AngleDegree, and have ° be a constant unit of this type, similar to the im constant for the ComplexPair type: https://github.com/JuliaLang/julia/blob/master/base/complex.jl

The downside of this approach is that it would require defining new methods for the mathematical operators (both trigonometric cos, sin, as well as +, *, etc.)

Keno commented 11 years ago

Well yes, but that's because Mathematica is inherently symbolical, while Julia is not (consider FullSimplify[°/Pi]). I can't see any case in which one would need a separate AngleDegree type.

ViralBShah commented 11 years ago

I prefer to not do this for a different reason. It imposes a cost on others modifying codes that use such features.

simonbyrne commented 11 years ago

I agree it's probably not worth implementing in Base, but it could be useful to incorporate into a package that allowed for units to be attached to numeric values, similar to F#'s support for units of measure: I've heard from someone in finance that they find it really useful, as they often end up working in weird non-SI units, and that it can help catch conversion and other errors.

JeffBezanson commented 11 years ago

Ok. Adding const ° = pi / 180 to your file/module is easy, so we don't need this in base.