# Negative Base and Negative Exponent

**URL:** <https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674>\
**Category:** General Usage\
**Created:** [May 12, 2017, 2:29pm UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674 "2017-05-12T14:29:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mboutros](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mboutros](https://discourse.julialang.org/u/mboutros)\
**Post date:** [May 12, 2017, 2:29pm UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/1 "2017-05-12T14:29:20Z")

</div>

Hello. I’ve spent some time reading about the reasons behind why Julia deals with negative exponents the way it does and I’m not interested in resurrecting that debate. I’m working on a script and I’ve run into a problem that boils down to the following:

```julia
-1.5 ^ -1.5 # Works!

b = -1.5
b ^ b # Doesn't work! :(

# Why? Because:
(-1.5) ^ (-1.5) # also doesn't work while
- (1.5) ^ (-1.5) # works, and is what the above is evaluated as.

```

How can I get Julia to accept taking a variable with a negative base to a negative exponent?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 12, 2017, 2:46pm UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/2 "2017-05-12T14:46:52Z")

</div>

It’s not the negative exponents, it’s the fractionals; which correspond to roots. You have to make it a complex number, then it works:

```julia
julia> (-1.5+0im) ^ (0.5)                                                                                                                             
7.499399432609231e-17 + 1.224744871391589im                    

julia> (-1.5+0im) ^ (-1.5)                                                                                                                            
-9.999199243478975e-17 + 0.5443310539518174im                                                                                                                                                                                         

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 12, 2017, 2:54pm UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/3 "2017-05-12T14:54:20Z")

</div>

Note that `-1.5 ^ -1.5` is not being parsed as you probably expect:

```julia
julia> parse("-1.5 ^ -1.5")
:(-(1.5 ^ -1.5))

```

When in doubt, check how your expression is parsed with `parse`, in any case use parentheses to avoid ambiguities.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 12, 2017, 3:34pm UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/4 "2017-05-12T15:34:21Z")

</div>

Also observe that this is standard mathematical parsing:  
When people write `-x^2` they mean `-(x^2)` not `(-x)^2`.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 13, 2017, 3:13am UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/5 "2017-05-13T03:13:44Z")

</div>

Are there any other programming languages that give `^` higher precedence than unary `-` though?  
It seems like something rather ripe for confusion and hard to find bugs.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 13, 2017, 7:37am UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/6 "2017-05-13T07:37:59Z")

</div>

Python and Ruby, just to name two:

```python
>>> -2**2
-4

```

```ruby
irb(main):001:0> -2^2
=> -4

```

```julia
julia> -2^2
-4

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 13, 2017, 8:01am UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/7 "2017-05-13T08:01:32Z")

</div>

R, Perl …

---

<div class="post-metadata">

**Author:** ![jpiitula](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpiitula/32/471_2.png) [@jpiitula](https://discourse.julialang.org/u/jpiitula)\
**Post date:** [May 13, 2017, 8:05am UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/8 "2017-05-13T08:05:27Z")

</div>

Prolog is tricky: the arithmetic expression `-2 ** 2` evaluates as `4.0` even though exponentiation does have precedence over negation. (I’m not sure why it goes to a float. It’s been a while since I knew these things.)

That’s because in Prolog the minus sign in `-2` is not the negation operator but a part of literal syntax.

In the arithmetic expression `-B **2`, where the base is a variable, the minus sign is an operator. That expression is parsed as `-(B** 2)` and maybe should not be spaced the way I did.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [May 13, 2017, 4:41pm UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/9 "2017-05-13T16:41:03Z")

</div>

In Matlab:

```julia
>> -2^2

ans =

    -4

```

I would be very surprised if `-2^2=4`.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 14, 2017, 9:23am UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/10 "2017-05-14T09:23:43Z")

</div>

I still find it very confusing that:

```julia
julia> dump(parse("-2.0 * u"))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol *
    2: Float64 -2.0
    3: Symbol u
  typ: Any

```

but with ^, it is parsed differently, i.e.:

```julia
julia> dump(parse("-2.0 ^ u"))
Expr
  head: Symbol call
  args: Array{Any}((2,))
    1: Symbol -
    2: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol ^
        2: Float64 2.0
        3: Symbol u
      typ: Any
  typ: Any

```

This sort of inconsistency, and having special handling in the parser for `^` vs. `*`, just seems wrong to me.

---

<div class="post-metadata">

**Author:** ![jpiitula](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpiitula/32/471_2.png) [@jpiitula](https://discourse.julialang.org/u/jpiitula)\
**Post date:** [May 14, 2017, 9:51am UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/11 "2017-05-14T09:51:34Z")

</div>

I think it’s just a peephole optimization built into the Julia parser.

The parses of `"-2 * 2"` and `"-u * w"`, resp. `"-2^2"` and `"-u^w"` are consistent with exponentiation having precedence over unary minus, and unary minus over multiplication.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 14, 2017, 10:00am UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/12 "2017-05-14T10:00:01Z")

</div>

That doesn’t fit well with the idea that one can have different meanings for the operators, depending on what types they are applied to (not that that’s a good thing - I’d prefer that Julia eliminated the type punning for `AbstractString` on `*` and `^`).

IIRC, didn’t Fortress have a way of giving a warning or error when the spacing indicated that the programmer intention was clearly at odds with what the compiler would do, in this sort of case?

---

<div class="post-metadata">

**Author:** ![jpiitula](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpiitula/32/471_2.png) [@jpiitula](https://discourse.julialang.org/u/jpiitula)\
**Post date:** [May 14, 2017, 1:47pm UTC](https://discourse.julialang.org/t/negative-base-and-negative-exponent/3674/13 "2017-05-14T13:47:46Z")

</div>

There seems to be an actual inconsistency (in the REPL in Julia 0.5.0) with what I guessed to be a peepholing in the parser if a program overrides the meaning of unary minus.

```julia
julia> import Base.-

julia> function (-)(x::Int) 3 end
- (generic function with 191 methods)

julia> -2, -(2), - 2
(-2,3,3)

julia> u = 2 ; -u, -(u), - u
(3,3,3)

```

But is it ever a good idea to override `Base` methods of unary minus?

By the way, while a macro could undo the parse-time analysis and reanalyze a negative number as a negation of a number, there is no way to recover certain occurrences of a unary _plus_ from the parse tree. Those are truly gone.

I wouldn’t worry about either of these. They affect some built-in literal types only, and those I think are best left to Julia.
