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

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

Closed
xiuliren opened this issue Jan 24, 2018 · 3 comments
Closed

Comments

@xiuliren
Copy link

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

@giordano
Copy link
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?

@kimikage
Copy link
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.

@kimikage
Copy link
Collaborator

kimikage commented Nov 2, 2019

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.

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

No branches or pull requests

3 participants