Skip to content

Commit

Permalink
OscarNumber: allow a few more fallbacks, add a few type conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
benlorenz committed Jan 31, 2024
1 parent 92e81ed commit b06e500
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/oscarnumber.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ end
end
end

_on_cmp_int(e1::T, e2::T) where T = Clong(Base.cmp(e1, e2))

Check warning on line 119 in src/oscarnumber.jl

View check run for this annotation

Codecov / codecov/patch

src/oscarnumber.jl#L119

Added line #L119 was not covered by tests
@generated _on_gen_cmp(::Type{ArgT}) where ArgT =
quote
@cfunction(Base.cmp, Clong, (Ref{ArgT}, Ref{ArgT}))
@cfunction(_on_cmp_int, Clong, (Ref{ArgT}, Ref{ArgT}))

Check warning on line 122 in src/oscarnumber.jl

View check run for this annotation

Codecov / codecov/patch

src/oscarnumber.jl#L122

Added line #L122 was not covered by tests
end

@generated _on_gen_is_zero(::Type{ArgT}) where ArgT =
Expand All @@ -130,16 +131,13 @@ end
@cfunction(Base.isone, Bool, (Ref{ArgT},))
end

_on_sign_int(e::T) where T = Base.cmp(e,0)::Int
_on_sign_int(e::T) where T = Clong(Base.cmp(e,0))

Check warning on line 134 in src/oscarnumber.jl

View check run for this annotation

Codecov / codecov/patch

src/oscarnumber.jl#L134

Added line #L134 was not covered by tests
@generated _on_gen_sign_int(::Type{ArgT}) where ArgT =
quote
@cfunction(_on_sign_int, Clong, (Ref{ArgT},))
end

function _fieldelem_to_float(e::T) where T
if !hasmethod(Float64, Tuple{T})
error("OscarNumber: cannot coerce to Float64, please define 'Polymake._fieldelem_to_float(e::$T)::Float64'")
end
return Float64(e)
end

Expand All @@ -153,9 +151,6 @@ end
end

function _fieldelem_to_rational(e::T) where T
if !hasmethod(Rational{BigInt}, Tuple{T})
error("OscarNumber: cannot check is_rational, please define 'Polymake._fieldelem_to_rational(e::$T)::Rational{BigInt}'")
end
Polymake._fieldelem_is_rational(e) || error("not a rational number")
return Base.Rational{BigInt}(e)
end
Expand Down Expand Up @@ -204,10 +199,14 @@ end
@cfunction(_on_init, Ref{ArgT}, (Clong, Ptr{ArgT}, Clong))
end

function _fieldelem_from_rational(f::Any, r::Base.Rational{BigInt})
return f(r)

Check warning on line 203 in src/oscarnumber.jl

View check run for this annotation

Codecov / codecov/patch

src/oscarnumber.jl#L202-L203

Added lines #L202 - L203 were not covered by tests
end

function _on_init_frac(id::Clong, ::Ptr{ArgT}, np::Ptr{BigInt}, dp::Ptr{BigInt})::ArgT where ArgT
n = unsafe_load(np)::BigInt
d = unsafe_load(dp)::BigInt
return _on_parent_by_id[id](Base.Rational{BigInt}(n, d))
return _fieldelem_from_rational(_on_parent_by_id[id], Base.Rational{BigInt}(n, d))::ArgT

Check warning on line 209 in src/oscarnumber.jl

View check run for this annotation

Codecov / codecov/patch

src/oscarnumber.jl#L209

Added line #L209 was not covered by tests
end
@generated _on_gen_init_frac(::Type{ArgT}) where ArgT =
quote
Expand Down Expand Up @@ -277,9 +276,7 @@ function register_julia_element(e, p, t::Type)
error("OscarNumber: immutable julia types not supported")
end

for type in (Int64, Base.Rational{BigInt})
hasmethod(p, (type,)) || error("OscarNumber: no constructor ($p)($type)")
end
hasmethod(p, (Int64,)) || error("OscarNumber: no constructor ($p)(Int64)")

Check warning on line 279 in src/oscarnumber.jl

View check run for this annotation

Codecov / codecov/patch

src/oscarnumber.jl#L279

Added line #L279 was not covered by tests

dispatch = oscar_number_dispatch_helper()
dispatch.index = newid
Expand Down

0 comments on commit b06e500

Please sign in to comment.