JeffreySarnoff / SaferIntegers.jl

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

The ^ power function does not seem to work #38

Closed Brinkhuis closed 1 year ago

Brinkhuis commented 1 year ago

This code seems to work just fine:

@saferintegers begin
    x = Int8(20)
    y = x * x
    end

Result:

OverflowError: 20 * 20 overflowed for type Int8

Stacktrace:
 [1] throw_overflowerr_binaryop(op::Symbol, x::Int8, y::Int8)
   @ Base.Checked [./checked.jl:155](https://file+.vscode-resource.vscode-cdn.net/Users/rene/Documents/Projects/Julia/JuliaBasics/checked.jl:155)
 [2] checked_mul
   @ [./checked.jl:289](https://file+.vscode-resource.vscode-cdn.net/Users/rene/Documents/Projects/Julia/JuliaBasics/checked.jl:289) [inlined]
 [3] *(x::SafeInt8, y::SafeInt8)
   @ SaferIntegers [~/.julia/packages/SaferIntegers/UJFmn/src/arith_ops.jl:35](https://file+.vscode-resource.vscode-cdn.net/Users/rene/Documents/Projects/Julia/JuliaBasics/~/.julia/packages/SaferIntegers/UJFmn/src/arith_ops.jl:35)
 [4] top-level scope
   @ [~/Documents/Projects/Julia/JuliaBasics/types.ipynb:3](https://file+.vscode-resource.vscode-cdn.net/Users/rene/Documents/Projects/Julia/JuliaBasics/~/Documents/Projects/Julia/JuliaBasics/types.ipynb:3)

When I change the code to:

@saferintegers begin
    x = Int8(20)
    y = x ^ 2
    end

It gives no OverflowError.

JeffreySarnoff commented 1 year ago

x * x multiplies two Int8 values under as saferintegers, the result must fit into an Int8 -- which it does not and so errors. x ^ 2 raises an Int8 value to a Int64 value as saferintegers, the result must fit into an Int64 -- which it does and so does not error.

JeffreySarnoff commented 1 year ago

closing this -- let me know if something is unexplained or overlooked. thank you.