Skip to content

convert sometimes throws InexactError, usually rounds #37

Description

@ssfrr

convert(Fixed{Int8, 7}, x) seems to happily round to the nearest representable value for most values of x, but throws an InexactError for x=0.999. AFAICT it throws the error when the value would get rounded up to 1.0, but 1.0 can't be represented. Is this expected behavior? I would expect that in this case it would act the same as convert(Fixed{Int8, 7}, 1.0), which overflows to -1.0.

julia> convert(Fixed{Int8, 7}, 0.8)
FixedPointNumbers.Fixed{Int8,7}(0.797)

julia> convert(Fixed{Int8, 7}, 0.9)
FixedPointNumbers.Fixed{Int8,7}(0.898)

julia> convert(Fixed{Int8, 7}, 0.999)
ERROR: InexactError()
 in trunc at float.jl:357
 in convert at /Users/srussell/.julia/v0.4/FixedPointNumbers/src/fixed.jl:34

julia> convert(Fixed{Int8, 7}, 1.0)
FixedPointNumbers.Fixed{Int8,7}(-1.0)

should convert accept values that can't be exactly represented? My understanding was that normally convert(T, x) is supposed to preserve information and thrown an error if the value can't be represented, and round(T, x) is supposed to be used to round to the nearest representable number, but I don't think that's explicitly said, so I could be wrong there.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions