FixedPointNumbers.Fixed
— TypeFixed{T <: Signed, f} <: FixedPoint{T, f}
Fixed{T,f}
maps Signed
integers from -2^f
to 2^f
to the range [-1.0, 1.0]. For example, Fixed{Int8,7}
maps -128
to -1.0
and 127
to 127/128 ≈ 0.992
.
There are the typealiases for Fixed
in the QXfY
notation, where Y
is the number of fractional bits (i.e. f
), and X+Y+1
equals the number of underlying bits used (+1
means the sign bit). For example, Q0f7
is aliased to Fixed{Int8,7}
and Q3f12
is aliased to Fixed{Int16,12}
.
FixedPointNumbers.FixedPoint
— TypeFixedPoint{T <: Integer, f} <: Real
Supertype of the two fixed-point number types: Fixed{T, f}
and Normed{T, f}
.
The parameter T
is the underlying machine representation and f
is the number of fraction bits.
FixedPointNumbers.Normed
— TypeNormed{T <: Unsigned, f} <: FixedPoint{T, f}
Normed{T,f}
maps Unsigned
integers from 0
to 2^f-1
to the range [0.0, 1.0]. For example, Normed{UInt8,8}
maps 0x00
to 0.0
and 0xff
to 1.0
.
There are the typealiases for Normed
in the NXfY
notation, where Y
is the number of fractional bits (i.e. f
), and X+Y
equals the number of underlying bits used. For example, N0f8
is aliased to Normed{UInt8,8}
and N4f12
is aliased to Normed{UInt16,12}
.
Base.isapprox
— Methodisapprox(x::FixedPoint, y::FixedPoint; rtol=0, atol=max(eps(x), eps(y)))
For FixedPoint numbers, the default criterion is that x
and y
differ by no more than eps
, the separation between adjacent fixed-point numbers.
FixedPointNumbers.floattype
— Methodfloattype(::Type{T})::Type{<:AbstractFloat}
Return a minimal type suitable for performing computations with instances of type T
without integer overflow.
The fallback definition of floattype(T)
applies only to T<:AbstractFloat
. However, it is permissible to extend floattype
to return types that are not subtypes of AbstractFloat
; the key characteristic is that the return type should support computation without integer overflow.
In general the returned type should have the minimum bitwidth needed to encode the full precision of the input type. however, a priority should be placed on computational efficiency; consequently, types like Float16
should be avoided except in scenarios where they are guaranteed to have hardware support.
Examples
A classic usage is to avoid overflow behavior by promoting FixedPoint
to AbstractFloat
julia> x = N0f8(1.0)
1.0N0f8
julia> x + x # overflow
0.996N0f8
julia> T = floattype(x)
Float32
julia> T(x) + T(x)
2.0f0
The following represents a valid extension of floattype
to non-AbstractFloats:
julia> using FixedPointNumbers, ColorTypes
julia> floattype(RGB{N0f8})
RGB{Float32}
RGB
itself is not a subtype of AbstractFloat
, but unlike RGB{N0f8}
operations with RGB{Float32}
are not subject to integer overflow.
FixedPointNumbers.scaledual
— Methodsd, ad = scaledual(s::Number, a)
Return sd
and ad
such that sd * ad == s * a
. When a
is an array of FixedPoint numbers, sd*ad
might be faster to compute than s*a
.