Julia: Infinite recursion in div for Real

Created on 24 Oct 2019  Â·  4Comments  Â·  Source: JuliaLang/julia

https://github.com/JuliaLang/julia/pull/33040 caused this

The fallback div definition used to apply to T <: Real but is now for T <: AbstractFloat, which leaves out types such as FixedPointDecimal. This causes an infinite promote recursion.

julia> using FixedPointDecimals

julia> a = FixedDecimal{Int,1}(1.3)
FixedDecimal{Int64,1}(1.3)

julia> div(a, a, RoundToZero)
ERROR: StackOverflowError:
Stacktrace:
 [1] div(::FixedDecimal{Int64,1}, ::FixedDecimal{Int64,1}, ::RoundingMode{:ToZero}) at ./div.jl:234 (repeats 79985 times)
bug

Most helpful comment

Yeah, I saw that after making that comment and testing my fixed so I fixed it right along the original issue.

All 4 comments

Yes, the removal of the fallback was deliberate. It was widely inaccurate for various types we had. FixedPointDecimals should provide this method for its particular type. Of course we should fix this to give a better error.

Also, I don't think this is a regression, at least not as stated. Three argument div is new as of that commit.

Ah, @keno, it turns out that there _was_ actually a regression, since the default implementation of ÷ had changed in #33040 to call 3-argument div. So this was now failing, where it didn't fail before:

julia> let x = FixedDecimal{Int,2}(1)
           x ÷ 2
       end
ERROR: StackOverflowError:
Stacktrace:
 [1] div(::FixedDecimal{Int64,2}, ::FixedDecimal{Int64,2}, ::RoundingMode{:ToZero}) at ./div.jl:234 (repeats 79998 times)

But your new fallback definition in #33679 does indeed fix the regression, so thanks! :)

julia> let x = FixedDecimal{Int,2}(1)
           x ÷ 2
       end
FixedDecimal{Int64,2}(0.00)

✅

Yeah, I saw that after making that comment and testing my fixed so I fixed it right along the original issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StefanKarpinski picture StefanKarpinski  Â·  3Comments

i-apellaniz picture i-apellaniz  Â·  3Comments

sbromberger picture sbromberger  Â·  3Comments

felixrehren picture felixrehren  Â·  3Comments

dpsanders picture dpsanders  Â·  3Comments