IntervalArithmetic.jl icon indicating copy to clipboard operation
IntervalArithmetic.jl copied to clipboard

StackOverflowError when computing sincospi(2±1)

Open kvnoct opened this issue 2 years ago • 5 comments

I got StackOverflowError when computing sincospi(2±1). Here's the code to reproduce

using IntervalArithmetic
x = 1..3
sincospi(x)

kvnoct avatar Mar 29 '22 13:03 kvnoct

I don't think we implemented sincospi, so it's probably falling back to the generic version. (I didn't even know there was such a function.)

Try splitting that up into two pieces.

dpsanders avatar Mar 29 '22 13:03 dpsanders

Thanks for reporting @kvnoct!

Both, sincos and sincospi are computed using methods defined in Base:

julia> @which sincos(x)
sincos(x) in Base.Math at special/trig.jl:205

julia> sincos(x)  # OK
([0.14112, 1], [-0.989993, 0.540303])

julia> @which sincospi(x)
sincospi(x::Real) in Base.Math at special/trig.jl:940

julia> sincospi(x)
ERROR: StackOverflowError:
Stacktrace:
 [1] sincospi(x::Interval{Float64}) (repeats 79984 times)
   @ Base.Math ./special/trig.jl:940

The method used for sincos(x) finally corresponds to (sin(x), cos(x)), and thus is ok. For sincospi, this line calls itself again and again, which yields the StackOverflow. As @dpsanders suggests, you can solve it by "splitting that up into two pieces":

julia> import Base: sincospi

julia> sincospi(x::Interval) = (sinpi(x), cospi(x))
sincospi (generic function with 7 methods)

julia> sincospi(x)
([-1, 1], [-1, 1])

lbenet avatar Mar 29 '22 17:03 lbenet

Thanks @lbenet @dpsanders!

Actually I was using cispi, which calls the sincospi

cispi(theta::Real) = Complex(reverse(sincospi(theta))...)

I guess I will define a workaround to handle the intervals in the meantime

kvnoct avatar Mar 30 '22 01:03 kvnoct

Note that there is only partial support for complex intervals so far.

dpsanders avatar Mar 30 '22 01:03 dpsanders

It might be nice to define sincospi(x::Interval) so that it exploits "shared parts" of sin and cos computation and computes both at once faster than computing both separately

lucaferranti avatar Mar 31 '22 11:03 lucaferranti