JeffreySarnoff / SaferIntegers.jl

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

`SafeUInt64(SafeUInt128(typemax(UInt64)))` throws #44

Closed LilithHafner closed 5 months ago

LilithHafner commented 5 months ago

Converting a value representable as a UInt64, but not as an Int64 to SafeUInt64 can throw. For example:

julia> SafeUInt64(SafeUInt128(typemax(UInt64)))
ERROR: InexactError: trunc(Int64, 0x0000000000000000ffffffffffffffff)
Stacktrace:
 [1] throw_inexacterror(::Symbol, ::Vararg{Any})
   @ Core ./boot.jl:748
 [2] checked_trunc_sint
   @ ./boot.jl:762 [inlined]
 [3] toInt64
   @ ./boot.jl:816 [inlined]
 [4] Int64
   @ ./boot.jl:890 [inlined]
 [5] Int64
   @ ~/.julia/packages/SaferIntegers/ksnrN/src/construct.jl:83 [inlined]
 [6] SafeUInt64(x::SafeUInt128)
   @ SaferIntegers ~/.julia/packages/SaferIntegers/ksnrN/src/construct.jl:80
 [7] top-level scope
   @ REPL[11]:1
JeffreySarnoff commented 5 months ago

thanks --

JeffreySarnoff commented 5 months ago

The current fix is overwriting the source with these:

SaferIntegers.SafeUInt8(x::SafeUInt16)=baseint(SafeUInt8)(x)  
SaferIntegers.SafeUInt16(x::SafeUInt32)=baseint(SafeUInt16)(x)  
SaferIntegers.SafeUInt32(x::SafeUInt64)=baseint(SafeUInt32)(x)  
SaferIntegers.SafeUInt64(x::SafeUInt128)=baseint(SafeUInt64)(x)  

I am having difficulty with an unrelated hiccup in the automerge. So the above should work meanwhile.

JeffreySarnoff commented 5 months ago

All is copacetic with the current version 3.4.3 afaik -- I am glad you found that bug.

LilithHafner commented 5 months ago

Yep. Can confirm. Thanks!