Skip to content

can not convert float to the same or more number of bit fixed point  #102

Closed

Description

@xiuliren

this works in Julia 0.6.2:

Float32(1.0) |> N0f8
Float32(1.0) |> N0f16

this will not work

Float32(1.0) |> N0f32
Float32(1.0) |> N0f64
Float64(1.0) |> N0f64

It looks like when the number of bits is the same or larger, it won’t work.
error message:

ArgumentError: FixedPointNumbers.Normed{UInt64,64} is a 64-bit type representing 0 values from 0.0 to 1.0; cannot represent 1.0

Activity

  1. giordano commented on Mar 15, 2018

    @giordano
    Member

    I just run into the same error. The problem is in _convert(::Type{U}, ::Type{T}, x) where {U <: Normed,T} function:

    function _convert(::Type{U}, ::Type{T}, x) where {U <: Normed,T}
    y = round(widen1(rawone(U))*x)
    (0 <= y) & (y <= typemax(T)) || throw_converterror(U, x)
    U(_unsafe_trunc(T, y), 0)

    E.g., in

    FixedPointNumbers._convert(N0f64, FixedPointNumbers.rawtype(N0f64), 1.0)

    the y variable is equal to

    julia> y = round(FixedPointNumbers.widen1(FixedPointNumbers.rawone(N0f64)) * 1.0)
    1.8446744073709552e19

    which is larger than the maximum number that can be represented with UInt64:

    julia> big(typemax(UInt64))
    18446744073709551615

    Maybe it's worth checking if the x argument is one?

  2. kimikage commented on Oct 26, 2019

    @kimikage
    Collaborator

    Maybe it's worth checking if the x argument is one?

    FYI, since the length of the mantissa part of Float32 is 23 (+1) bits, this is not a problem just when x argument is one.

    julia> for f=1:32; Float32(exp2(32-f)) |> Normed{UInt32,f};end
    ERROR: ArgumentError: Normed{UInt32,25} is a 32-bit type representing 4294967296 values from 0.0 to 128.0; cannot represent 128.0

    Similarly, the length of the mantissa part of Float64 is 52 (+1) bits.

    julia> for f=1:64; Float64(exp2(64-f)) |> Normed{UInt64,f};end
    ERROR: ArgumentError: Normed{UInt64,54} is a 64-bit type representing 0 values from 0.0 to 1024.0; cannot represent 1024.0

    Fixing this problem may be useful for the round trip testing to check the conversions.

  3. kimikage commented on Nov 2, 2019

    @kimikage
    Collaborator

    PR #131 does not change the rem.

    Therefore, this does not fix the problems such as:

    julia> using Colors
    
    julia> white = parse(RGB{Float32}, "white")
    RGB{Float32}(1.0f0,1.0f0,1.0f0)
    
    julia> convert(RGB{N0f32}, white)
    RGB{N0f32}(0.0,0.0,0.0)

    Originally posted by @kimikage in #131 (comment)

    When you are faced by a problem with rem, please report it as a new issue.

  4. added a commit that references this issue on Dec 28, 2024
    70ae1d6
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