# Evalpoly without coefficients: ugly errors, zero polynomial?

**URL:** <https://discourse.julialang.org/t/evalpoly-without-coefficients-ugly-errors-zero-polynomial/97666>\
**Category:** Internals & Design\
**Tags:** numerics, polynomials\
**Created:** [April 19, 2023, 5:05pm UTC](https://discourse.julialang.org/t/evalpoly-without-coefficients-ugly-errors-zero-polynomial/97666 "2023-04-19T17:05:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [April 19, 2023, 5:05pm UTC](https://discourse.julialang.org/t/evalpoly-without-coefficients-ugly-errors-zero-polynomial/97666/1 "2023-04-19T17:05:36Z")

</div>

Consider calls such as these:

```julia
julia> evalpoly(3, ())
ERROR: BoundsError: attempt to access Tuple{} at index [0]
Stacktrace:
 [1] getindex(t::Tuple, i::Int64)
   @ Base ./tuple.jl:31
 [2] macro expansion
   @ ./math.jl:182 [inlined]
 [3] evalpoly(x::Int64, p::Tuple{})
   @ Base.Math ./math.jl:181
 [4] top-level scope
   @ REPL[1]:1

julia> evalpoly(0.3, Float64[])
ERROR: BoundsError: attempt to access 0-element Vector{Float64} at index [0]
Stacktrace:
 [1] getindex
   @ ./essentials.jl:13 [inlined]
 [2] _evalpoly
   @ ./math.jl:199 [inlined]
 [3] evalpoly(x::Float64, p::Vector{Float64})
   @ Base.Math ./math.jl:194
 [4] top-level scope
   @ REPL[2]:1

```

Some issues:

1. The errors are not friendly/decriptive.

2. In the case when the coefficients are an empty tuple, `Tuple{}`, it might make sense to fail early, that is, make this a `MethodError` by preventing dispatch to the method for empty tuples in the first place. [This](https://discourse.julialang.org/t/is-this-test-detect-unbound-args-result-valid-or-a-bug/96987/2) trick should work.

3. Arguably, a polynomial with no coefficients should be interpreted as the zero polynomial. So it might make sense to special-case this instead of erroring. For tuples this might be addressed with a method like so: `Base.evalpoly(::T, ::Tuple{}) where {T} = zero(T)`. For empty vectors an additional check would need to be included at the beginning of the relevant method.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [November 28, 2024, 11:56am UTC](https://discourse.julialang.org/t/evalpoly-without-coefficients-ugly-errors-zero-polynomial/97666/2 "2024-11-28T11:56:30Z")

</div>

Finally getting addressed 😄:

> <https://github.com/JuliaLang/julia/issues/56699>
>
> \`evalpoly(x, ())\` should either return \`zero(x)\` or give a helpful error message…. Currently it
> 
> \`\`\`julia-repl
> julia\> evalpoly(1, ())
> ERROR: BoundsError: attempt to access Tuple{} at index \[0\]
> Stacktrace:
> \[1\] getindex(t::Tuple, i::Int64)
> @ Base ./tuple.jl:31
> \[2\] macro expansion
> @ ./math.jl:95 \[inlined\]
> \[3\] evalpoly(x::Int64, p::Tuple{})
> @ Base.Math ./math.jl:94
> \[4\] top-level scope
> @ REPL\[13\]:1
> \`\`\`
