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

Inconsistent Integer results #154

Closed
kimikage opened this issue Dec 25, 2019 · 2 comments · Fixed by #168
Closed

Inconsistent Integer results #154

kimikage opened this issue Dec 25, 2019 · 2 comments · Fixed by #168
Milestone

Comments

@kimikage
Copy link
Collaborator

kimikage commented Dec 25, 2019

The return types of Integer(::Fixed) and Integer(::Normed) are based on different manners.

Fixed{T}: T

julia> typeof(Integer(1Q1f6))
Int8

julia> typeof(Integer(1Q1f14))
Int16

Normed: Int with (non-intuitive) promotions

julia> typeof(Integer(1N0f8))
Int64

julia> typeof(Integer(1N0f16))
Int64

julia> typeof(Integer(1N0f32))
Int64

julia> typeof(Integer(1N0f64)) # !?
BigInt

Float: Int w/o promotions

julia> typeof(Integer(Float16(1)))
Int64

julia> typeof(Integer(1.0f0))
Int64

julia> typeof(Integer(1.0))
Int64

julia> typeof(Integer(0x1p63))
ERROR: InexactError: Int64(9.223372036854776e18)

I think it is better to unify the manners, even though it will be a breaking change.
I consider it appropriate for FixedPoint to return numbers in the rawtype T as Integer(::Fixed) returns.

FYI, with fix for issue #153, we can use floor(Integer, x) etc.

@kimikage kimikage added this to the 1.0 milestone Dec 25, 2019
@kimikage
Copy link
Collaborator Author

kimikage commented Dec 25, 2019

BTW, there is no longer a zero-argument InexactError().

> julia> Integer(1.5Q1f6)
ERROR: MethodError: no method matching InexactError()
Closest candidates are:
  InexactError(::Symbol, ::Any, ::Any) at boot.jl:280

function Base.Integer(x::Fixed{T,f}) where {T,f}
isinteger(x) || throw(InexactError())
Integer(x.i>>f)
end
function (::Type{TI})(x::Fixed{T,f}) where {TI <: Integer,T,f}
isinteger(x) || throw(InexactError())
TI(x.i>>f)
end

Also, I don't think this specialization makes much sense. (There is no ambiguity.)

@kimikage
Copy link
Collaborator Author

There is the problem of type inconsistency in Rational, too.

julia> typeof(Rational(0.5Q1f6))
Rational{Int64}

julia> typeof(Rational(0.5Q0f7))
Rational{Int64}

julia> typeof(Rational(0.5Q1f62))
Rational{Int128}

julia> typeof(Rational(0.5Q0f63))
Rational{Int128}
julia> typeof(Rational(0.5N0f8))
Rational{UInt8}

julia> typeof(Rational(0.5N0f64))
Rational{UInt64}

I also think it is better to use the rawtype T for Rational, but it should be noted that the rawone (i.e. the denominator) of Fixed{T,f} exceeds the range of T at f == 8sizeof(T)-1.

kimikage added a commit that referenced this issue Jan 16, 2020
Commonize further code between `Fixed` and` Normed` (Fixes #154)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant