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

Fix reductions in Statistics #183

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name = "FixedPointNumbers"
uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
version = "0.8.1"

[deps]
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
julia = "1"

Expand Down
3 changes: 3 additions & 0 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
div, fld, rem, mod, mod1, fld1, min, max, minmax,
rand, length

import Statistics: _mean_promote
timholy marked this conversation as resolved.
Show resolved Hide resolved

using Base.Checked: checked_add, checked_sub, checked_div

using Base: @pure
Expand Down Expand Up @@ -225,6 +227,7 @@ Base.mul_prod(x::FixedPoint, y::FixedPoint) = Treduce(x) * Treduce(y)
Base.reduce_empty(::typeof(Base.mul_prod), ::Type{F}) where {F<:FixedPoint} = one(Treduce)
Base.reduce_first(::typeof(Base.mul_prod), x::FixedPoint) = Treduce(x)

_mean_promote(x::Real, y::FixedPoint) = Treduce(y)

"""
sd, ad = scaledual(s::Number, a)
Expand Down
13 changes: 12 additions & 1 deletion test/fixed.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FixedPointNumbers, Test
using FixedPointNumbers, Statistics, Test
using FixedPointNumbers: bitwidth

function test_op(fun::F, ::Type{T}, fx, fy, fxf, fyf, tol) where {F,T}
Expand Down Expand Up @@ -242,6 +242,17 @@ end
@test prod(a, dims=1) == [acmp]
end

@testset "reductions, Statistics" begin
a = Q1f6[0.75, 0.5]
af = FixedPointNumbers.Treduce.(a)
@test mean(a) === mean(af)
@test std(a) === std(af)
@test var(a) === var(af)
m = mean(a)
@test stdm(a, m) === stdm(af, m)
@test varm(a, m) === varm(af, m)
end

@testset "convert result type" begin
x = Fixed{Int8,7}(0.75)
for T in (Float16, Float32, Float64, BigFloat)
Expand Down
13 changes: 12 additions & 1 deletion test/normed.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FixedPointNumbers, Test
using FixedPointNumbers, Statistics, Test
using FixedPointNumbers: bitwidth

@testset "domain of f" begin
Expand Down Expand Up @@ -449,6 +449,17 @@ end
@test prod(a, dims=1) == [acmp]
end

@testset "reductions, Statistics" begin
a = N0f8[reinterpret(N0f8, 0x80), reinterpret(N0f8, 0x40)]
af = FixedPointNumbers.Treduce.(a)
@test mean(a) === mean(af)
@test std(a) === std(af)
@test var(a) === var(af)
m = mean(a)
@test stdm(a, m) === stdm(af, m)
@test varm(a, m) === varm(af, m)
end

@testset "rand" begin
for T in (Normed{UInt8,8}, Normed{UInt8,6},
Normed{UInt16,16}, Normed{UInt16,14},
Expand Down