# Defining pow :^ does not have a fallback for irrationals (method ambiguous)

**URL:** <https://discourse.julialang.org/t/defining-pow-does-not-have-a-fallback-for-irrationals-method-ambiguous/4751>\
**Category:** General Usage\
**Created:** [July 9, 2017, 5:32pm UTC](https://discourse.julialang.org/t/defining-pow-does-not-have-a-fallback-for-irrationals-method-ambiguous/4751 "2017-07-09T17:32:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)\
**Post date:** [July 9, 2017, 5:32pm UTC](https://discourse.julialang.org/t/defining-pow-does-not-have-a-fallback-for-irrationals-method-ambiguous/4751/1 "2017-07-09T17:32:51Z")

</div>

```julia
julia> struct Test <: AbstractFloat
       x
       end
julia> Base.:^(x::Number, y::Test) = x^(y.x)

# does not work
julia> e^Test(2.0)
ERROR: MethodError: ^(::Irrational{:e}, ::Test) is ambiguous. Candidates:
  ^(x::Number, y::Test) in Main at REPL[3]:1
  ^(::Irrational{:e}, x::Number) in Base at irrationals.jl:219
Possible fix, define
  ^(::Irrational{:e}, ::Test)

```

That should just work since `Irrational <: Number`; I would think that there should be an appropriate float conversion fallback.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 9, 2017, 5:56pm UTC](https://discourse.julialang.org/t/defining-pow-does-not-have-a-fallback-for-irrationals-method-ambiguous/4751/2 "2017-07-09T17:56:40Z")

</div>

```julia
struct Test{T <: AbstractFloat}
    x::T
end

Base.:^(x::T, y::Test) where {T<:Number} = x^(y.x)

```

```julia
julia> e^Test(2.0)
7.38905609893065

```

---

<div class="post-metadata">

**Author:** ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)\
**Post date:** [July 9, 2017, 6:35pm UTC](https://discourse.julialang.org/t/defining-pow-does-not-have-a-fallback-for-irrationals-method-ambiguous/4751/4 "2017-07-09T18:35:19Z")

</div>

> [@Tamas\_Papp](#):
>
> struct Test{T \<: AbstractFloat}  
> x::T  
> end

Actually the real issue here is that what you suggest doesn’t work if you add

```julia
struct Test{T <: AbstractFloat} <: AbstractFloat
    x::T
end
Base.:^(x::T, y::Test) where {T<:Number} = x^(y.x)

julia> e^Test(2.0)
ERROR: MethodError: ^(::Irrational{:e}, ::Test{Float64}) is ambiguous. Candidates:
  ^(x::Number, y::Test) in Main at REPL[1]:1
  ^(::Irrational{:e}, x::Number) in Base at irrationals.jl:219
Possible fix, define
  ^(::Irrational{:e}, ::Test)

```

In other words I need that `Test <: AbstractFloat`

---

<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:** [July 9, 2017, 7:32pm UTC](https://discourse.julialang.org/t/defining-pow-does-not-have-a-fallback-for-irrationals-method-ambiguous/4751/5 "2017-07-09T19:32:49Z")

</div>

Looks like a peculiarity of `e`, because it falls back on `exp`:

```julia
julia> struct Test{T<:AbstractFloat} <: AbstractFloat
       x::T
       end

julia> Base.:^(x::T, y::S) where {T<:Number, S<:Test} = x ^ (y.x)

julia> pi ^ Test(2.0)
9.869604401089358

julia> im ^ Test(2.0)
-1.0 + 0.0im

julia> 5.7 ^ Test(2.0)
32.49

julia> e ^ Test(2.0)
ERROR: not implemented for Test{Float64}
Stacktrace:
 [1] ^(::Irrational{:e}, ::Test{Float64}) at ./irrationals.jl:223

julia> exp(Test(2.0))
ERROR: not implemented for Test{Float64}
Stacktrace:
 [1] exp(::Test{Float64}) at ./math.jl:275

julia> Base.:^(x::Irrational{:e}, y::S) where {S<:Test} = e ^ (y.x)

julia> e ^ Test(2.0)
7.38905609893065

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [July 9, 2017, 10:50pm UTC](https://discourse.julialang.org/t/defining-pow-does-not-have-a-fallback-for-irrationals-method-ambiguous/4751/6 "2017-07-09T22:50:35Z")

</div>

It does seem the error message is meaningful.  
There does indeed seem to be an ambiguity.

Which method is more specific?  
`^(x::Number, y::Test)`  
`^(::Irrational{:e}, x::Number)`

Have you tried following the suggested fix?

```julia
Possible fix, define
  ^(::Irrational{:e}, ::Test)

```

```julia
struct Test{T<:AbstractFloat} <: AbstractFloat
    x::T
end

Base.:^(x::T, y::Test) where {T<:Number} = x^(y.x)
Base.:^(::Irrational{:e}, y::Test) = exp(y.x)

```

```julia
Julia-0.6.0> e^Test(2.0)
7.38905609893065

```

---

<div class="post-metadata">

**Author:** ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)\
**Post date:** [July 10, 2017, 2:21am UTC](https://discourse.julialang.org/t/defining-pow-does-not-have-a-fallback-for-irrationals-method-ambiguous/4751/7 "2017-07-10T02:21:07Z")

</div>

Right, I was trying to better understand whether the current fallbacks make the most sense.

I feel like `Base.exp(x::AbstractFloat) = float(e)^x` would make sense to define in base e.g.:

```julia
struct Test{T<:AbstractFloat} <: AbstractFloat
    x::T
end
Base.convert(::Type{T}, x::Test{T}) where T<:AbstractFloat = x.x
Base.promote_rule(::Type{T},::Type{Test{T}}) where {T<:AbstractFloat} = T
Base.:^(x::T, y::Test{T}) where {T<:AbstractFloat} = x^(y.x)

x = Test(2.0)

julia> γ^x # works!
0.33317792380771866

julia> e^x # nope
ERROR: not implemented for Test{Float64}
Stacktrace:
 [1] ^(::Irrational{:e}, ::Test{Float64}) at .\irrationals.jl:219

```

Perhaps base julia needs to define

```julia
Base.exp(x::AbstractFloat) = float(e)^x
julia> e^x # now works 
7.3890560989306495

```

---

<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:** [July 10, 2017, 7:59am UTC](https://discourse.julialang.org/t/defining-pow-does-not-have-a-fallback-for-irrationals-method-ambiguous/4751/8 "2017-07-10T07:59:52Z")

</div>

> [@musm](#):
>
> Perhaps base julia needs to define
> 
> ```julia
> Base.exp(x::AbstractFloat) = float(e)^x
> julia> e^x # now works 
> 7.3890560989306495
> 
> ```

I don’t think this is a good idea:

```julia
julia> struct Test{T<:AbstractFloat} <: AbstractFloat
       x::T
       end

julia> Base.exp(x::Test) = float(e) ^ (x.x)

julia> e ^ Test(big"2.0")
7.389056098930649441295292229390736727411671930796332859195060284440703668451533

julia> exp(big"2.0")
7.389056098930650227230427460575007813180315570551847324087127822522573796079054

```
