JeffreySarnoff / SaferIntegers.jl

These integer types use checked arithmetic, otherwise they are as system types.
MIT License
59 stars 9 forks source link

Promotion in comparison fails in exponentiation #37

Closed jishnub closed 1 year ago

jishnub commented 1 year ago
julia> SafeInt8(5)^SafeInt8(2)
ERROR: InexactError: trunc(Int8, 129)
Stacktrace:
  [1] throw_inexacterror(f::Symbol, #unused#::Type{Int8}, val::Int64)
    @ Core ./boot.jl:634
  [2] checked_trunc_sint
    @ ./boot.jl:656 [inlined]
  [3] toInt8
    @ ./boot.jl:671 [inlined]
  [4] Int8
    @ ./boot.jl:781 [inlined]
  [5] SafeInt8
    @ ~/.julia/packages/SaferIntegers/UJFmn/src/construct.jl:73 [inlined]
  [6] convert(#unused#::Type{SafeInt8}, x::Int64)
    @ Base ./number.jl:7
  [7] _promote
    @ ./promotion.jl:358 [inlined]
  [8] promote
    @ ./promotion.jl:381 [inlined]
  [9] <
    @ ~/.julia/packages/SaferIntegers/UJFmn/src/binary_ops.jl:42 [inlined]
 [10] ^(x::SafeInt8, y::SafeInt8)
    @ SaferIntegers ~/.julia/packages/SaferIntegers/UJFmn/src/pow.jl:22
 [11] top-level scope
    @ REPL[12]:1

The error seems to arise from https://github.com/JeffreySarnoff/SaferIntegers.jl/blob/4653835e462c79ad1d3fdbb85bf0068c7982a294/src/pow.jl#L22 because

julia> SafeInt8(5) < 129
ERROR: InexactError: trunc(Int8, 129)
Stacktrace:
  [1] throw_inexacterror(f::Symbol, #unused#::Type{Int8}, val::Int64)
    @ Core ./boot.jl:634
  [2] checked_trunc_sint
    @ ./boot.jl:656 [inlined]
  [3] toInt8
    @ ./boot.jl:671 [inlined]
  [4] Int8
    @ ./boot.jl:781 [inlined]
  [5] SafeInt8
    @ ~/.julia/packages/SaferIntegers/UJFmn/src/construct.jl:73 [inlined]
  [6] convert
    @ ./number.jl:7 [inlined]
  [7] _promote
    @ ./promotion.jl:358 [inlined]
  [8] promote
    @ ./promotion.jl:381 [inlined]
  [9] <(x::SafeInt8, y::Int64)
    @ SaferIntegers ~/.julia/packages/SaferIntegers/UJFmn/src/binary_ops.jl:42
 [10] top-level scope
    @ REPL[14]:1
JeffreySarnoff commented 1 year ago

Thank you. I believe this is now fixed in main, pending merge. Let me know if you find any other issues.