Skip to content

Commit

Permalink
parse sqrt symbol (√) as a unary operator
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed May 10, 2014
1 parent fe994bb commit 56e04cb
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ New language features
| == | ≥ (>=) ≤ (<=) ≡ (===) ≠ (!=) ≢ (!==) .≥ (.>=) .≤ (.<=) .!= (.≠) ∈ (`in`) ∉ (`(x,y)->!in(x, y)`) ∋ (`(x,y)->in(y, x)`) ∌ (`(x,y)->!in(y, x)`) ⊆ (`issubset`) ⊈ (`(x,y)->!issubset(x, y)`) ⊊ (`(x,y)->x⊆y && x!=y`) ⊂ ⊄ |
| + | ⊕ ⊖ ⊞ ⊟ ∪ (`union`) ∨ ⊔ |
| * | ÷ (`div`) ⋅ (`dot`) ∘ × (`cross`) ∩ (`intersect`) ∧ ⊓ ⊗ ⊘ ⊙ ⊚ ⊛ ⊠ ⊡ |
| unary ||

Library improvements
--------------------
Expand Down
3 changes: 1 addition & 2 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export sin, cos, tan, sinh, cosh, tanh, asin, acos, atan,
besselj0, besselj1, besselj, bessely0, bessely1, bessely,
hankelh1, hankelh2, besseli, besselk, besselh,
beta, lbeta, eta, zeta, polygamma, invdigamma, digamma, trigamma,
erfinv, erfcinv,
erfinv, erfcinv

import Base: log, exp, sin, cos, tan, sinh, cosh, tanh, asin,
acos, atan, asinh, acosh, atanh, sqrt, log2, log10,
Expand Down Expand Up @@ -284,7 +284,6 @@ sqrt(x::Float64) = box(Float64,sqrt_llvm(unbox(Float64,x)))
sqrt(x::Float32) = box(Float32,sqrt_llvm(unbox(Float32,x)))
sqrt(x::Real) = sqrt(float(x))
@vectorize_1arg Number sqrt
const =sqrt

for f in (:ceil, :trunc, :significand) # :rint, :nearbyint
@eval begin
Expand Down
5 changes: 3 additions & 2 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ function ifelse(c::AbstractArray{Bool}, x, y::AbstractArray)
end

# some operators not defined yet
global //, .>>, .<<, >:, <|, |>, hcat, hvcat, , ×, , , , , , , , ,
global //, .>>, .<<, >:, <|, |>, hcat, hvcat, , ×, , , , , , , , , ,

module Operators

Expand Down Expand Up @@ -428,6 +428,7 @@ export
,
,
,
,
colon,
hcat,
vcat,
Expand All @@ -441,6 +442,6 @@ import Base: !, !=, $, %, .%, &, *, +, -, .!=, .+, .-, .*, ./, .<, .<=, .==, .>,
.>=, .\, .^, /, //, <, <:, <<, <=, ==, >, >=, >>, .>>, .<<, >>>,
<|, |>, \, ^, |, ~, !==, >:, colon, hcat, vcat, hvcat, getindex, setindex!,
transpose, ctranspose,
, , , .≥, .≤, .≠, ÷, , ×, , , , , , , , ,
, , , .≥, .≤, .≠, ÷, , ×, , , , , , , , , ,

end
1 change: 1 addition & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ include("methodshow.jl")
include("floatfuncs.jl")
include("math.jl")
importall .Math
const ()=sqrt
include("float16.jl")

# multidimensional arrays
Expand Down
4 changes: 2 additions & 2 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
(define (kwarg? e)
(and (pair? e) (eq? (car e) 'kw)))

(define unary-ops '(+ - ! ~ |<:| |>:|))
(define unary-ops '(+ - ! ~ |<:| |>:|))

; operators that are both unary and binary
(define unary-and-binary-ops '(+ - $ & ~))
Expand All @@ -98,7 +98,7 @@
(define ctrans-op (string->symbol "'"))
(define vararg-op (string->symbol "..."))

(define operators (list* '~ '! '-> ctrans-op trans-op vararg-op
(define operators (list* '~ '! '-> '√ ctrans-op trans-op vararg-op
(delete-duplicates
(apply append (vector->list ops-by-prec)))))

Expand Down

8 comments on commit 56e04cb

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the binary version of √ where 3√2 means 2^(1/3)?

@kmsquire
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't 3√2 also be interpreted as 3*√2?

@staticfloat
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^ Indeed, that's what I interpreted it as. I would think you'd need to do 2√(1/3) to get what you're talking about, Stefan.

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, fair point. That interpretation actually makes more sense anyway. Which raises the question of whether juxtaposition with the sqrt symbol works of not. (I can't check – I'm on a phone at the airport. Even though I have my laptop. The airport in San Juan has no wifi – why do I pay for Boingo?)

@carlobaldassi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also would instinctively interpret 3√2 as 3*√2. For the record, this is what one gets now:

julia> 3√2
ERROR: syntax: extra token "√" after end of expression

As an aside, it would make sense to have ∛ = cbrt.

@staticfloat
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

julia> √2
1.4142135623730951

julia> 3√2
ERROR: syntax: extra token "√" after end of expression

julia> 3 √2
ERROR: syntax: extra token "√" after end of expression

julia> 3*√2
4.242640687119286

@staticfloat
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, weird. Didn't see Carlo's post 'till now. Also, @carlobaldassi github doesn't seem to want to print your cuberoot symbol properly in the code formatting font. ;)

@carlobaldassi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cuberoot symbol displays just fine for me. Maybe the font your browser uses for code misses that symbol?

Please sign in to comment.