Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GAP.julia_to_gap(big(2)^62) versus GAP.julia_to_gap(2^62) #688

Open
fingolfin opened this issue Sep 6, 2021 · 1 comment
Open

GAP.julia_to_gap(big(2)^62) versus GAP.julia_to_gap(2^62) #688

fingolfin opened this issue Sep 6, 2021 · 1 comment
Labels
topic: conversion Issue related to conversion

Comments

@fingolfin
Copy link
Member

I just run into this:

julia> GAP.julia_to_gap(big(2)^62)
GAP: 4611686018427387904

julia> GAP.julia_to_gap(2^62)
4611686018427387904

Without reflecting it deeper, I would expect the two to return the same thing. Maybe I am wrong, need to think more about it.

See also PR #434 and thinks that links to, and probably a bunch of other issues and PRs sigh

@fingolfin fingolfin added the topic: conversion Issue related to conversion label Sep 6, 2021
@fingolfin
Copy link
Member Author

Looking at the code, we have

julia_to_gap(x::Int64) = x

but at the same time also this:

function julia_to_gap(x::Integer)
    # if it fits into a GAP immediate integer, convert x to Int64
    x in -1<<60:(1<<60-1) && return Int64(x)
    # for the general case, fall back to BigInt
    return julia_to_gap(BigInt(x))
end

...

function julia_to_gap(x::UInt)
    x < (1<<60) && return Int64(x)
    return ccall((:ObjInt_UInt, libgap), GapObj, (UInt64, ), x)
end

## BigInts are converted via a ccall
function julia_to_gap(x::BigInt)
    x in -1<<60:(1<<60-1) && return Int64(x)
    return GC.@preserve x ccall((:MakeObjInt, libgap), GapObj, (Ptr{UInt64}, Cint), x.d, x.size)
end

Thus once the value 2^62 is converted to an Int and once to a GapObj.

This can be fixed by replace the checks involving 1<<60 to compare against typemin(Int) resp. typemax(Int)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: conversion Issue related to conversion
Projects
None yet
Development

No branches or pull requests

1 participant