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

mean of fixed-point numbers is a Float64 #306

Closed
jariji opened this issue Jun 13, 2024 · 5 comments
Closed

mean of fixed-point numbers is a Float64 #306

jariji opened this issue Jun 13, 2024 · 5 comments

Comments

@jariji
Copy link

jariji commented Jun 13, 2024

using Statistics, FixedPointNumbers

julia> typeof(mean([(N0f8(.5)), (N0f8(.5))]))
Float64

Is this the intended behavior? I expected it would round to the nearest fixed-point number.

@jariji jariji changed the title mean of fixed-point numbers mean of fixed-point numbers is a Float64 Jun 13, 2024
@kimikage
Copy link
Collaborator

kimikage commented Jun 13, 2024

At least that is the behavior @timholy intended.
cf. #183, JuliaStats/Statistics.jl#165 (comment)

This is consistent with the default behavior of integer types.

julia> typeof(mean([0x80, 0x80]))
Float64

Also, while N0f8 is used for image processing, Float32 is not always accurate enough.

@kimikage kimikage closed this as not planned Won't fix, can't repro, duplicate, stale Jun 13, 2024
@kimikage
Copy link
Collaborator

Note that it is not a good idea to use Float32 for the N0f8 value accumulations. N24f8 is superior in both speed and accuracy.

@jariji
Copy link
Author

jariji commented Jun 13, 2024

@timholy Can I ask the rationale for this behavior?

  • I thought the point of a fixed-point type was to preserve the type by rounding, but that's not what I see here.
  • I thought sum(_) and reduce(+, _) were supposed to return the same.
julia> N0f8(.5) + N0f8(.5) |> typeof
N0f8 (alias for Normed{UInt8, 8})

julia> reduce(+, [N0f8(.5), N0f8(.5)]) |> typeof
N0f8 (alias for Normed{UInt8, 8})

julia> sum([N0f8(.5), N0f8(.5)]) |> typeof
Float64

julia> N0f8(1)/2 |> typeof
Float32

@kimikage kimikage reopened this Jun 13, 2024
@kimikage
Copy link
Collaborator

As implied above, UInt8 is a counterexample to your expectation.

julia> 0x80 + 0x80 |> typeof
UInt8

julia> reduce(+, [0x80, 0x80]) |> typeof
UInt8

julia> sum([0x80, 0x80]) |> typeof
UInt64

julia> 0xff/2 |> typeof
Float64

Of course, since fixed-point numbers are different from both integers and floating-point numbers, this package can and shall design its own rules. And, there is already a design.
In other words, I think it is you who must provide the "objective" rationale.

@kimikage
Copy link
Collaborator

I am planning to change the design once Statistics.jl provides a richer public API.
However, I think the return types will remain as they are.

@kimikage kimikage closed this as not planned Won't fix, can't repro, duplicate, stale Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants