Skip to content

Problems with rem for Fixed #219

Closed
@kimikage

Description

@kimikage

As mentioned in #211 (comment), the rem for Fixed has problems.

rem(x::Real, ::Type{Fixed{T,f}}) where {T,f} = Fixed{T,f}(rem(Integer(trunc(x)),T)<<f + rem(Integer(round(rem(x,1)*(one(widen1(T))<<f))),T),0)

I plan to specialize the multiplication for Fixed after the merging PR #213. As part of the specialization, I will improve the rem for Fixed, but since it is not the main topic of the specialization, I opened a separate issue for rem.

Speed performance

Simply put, rem for Fixed is too slow.

julia> x_f32 = rand(Float32, 1000, 1000);

julia> @btime $x_f32 .% Q0f7;
  8.012 ms (2 allocations: 976.70 KiB)

julia> @btime $x_f32 .% N0f8;
  123.300 μs (2 allocations: 976.70 KiB)

Throwing errors

Though this is one of the causes of the slowdown, the rem can throw an error.

julia> 2.0^63 % Q0f7 # Due to `Integer()`
ERROR: InexactError: Int64(9.223372036854776e18)

The rem for Normed prioritizes the speed and allows for inaccurate values to be returned in the overflow.

Unnecessary type conversion

We need the specialized method.

julia> typemax(Q0f63) % Q0f63
ERROR: ArgumentError: Fixed{Int64,63} is a 64-bit type representing 0 values from -1.0 to 1.0; cannot represent 1

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