Skip to content

Commit 2f2a34a

Browse files
authored
Fix reductions in Statistics (#183)
1 parent bfb4f47 commit 2f2a34a

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name = "FixedPointNumbers"
22
uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
33
version = "0.8.1"
44

5+
[deps]
6+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
7+
58
[compat]
69
julia = "1"
710

src/FixedPointNumbers.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
88
div, fld, rem, mod, mod1, fld1, min, max, minmax,
99
rand, length
1010

11+
import Statistics # for _mean_promote
12+
1113
using Base.Checked: checked_add, checked_sub, checked_div
1214

1315
using Base: @pure
@@ -251,6 +253,9 @@ Base.mul_prod(x::FixedPoint, y::FixedPoint) = Treduce(x) * Treduce(y)
251253
Base.reduce_empty(::typeof(Base.mul_prod), ::Type{F}) where {F<:FixedPoint} = one(Treduce)
252254
Base.reduce_first(::typeof(Base.mul_prod), x::FixedPoint) = Treduce(x)
253255

256+
if isdefined(Statistics, :_mean_promote)
257+
Statistics._mean_promote(x::Real, y::FixedPoint) = Treduce(y)
258+
end
254259

255260
"""
256261
sd, ad = scaledual(s::Number, a)

test/fixed.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using FixedPointNumbers, Test
1+
using FixedPointNumbers, Statistics, Test
22
using FixedPointNumbers: bitwidth
33

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

245+
@testset "reductions, Statistics" begin
246+
a = Q1f6[0.75, 0.5]
247+
af = FixedPointNumbers.Treduce.(a)
248+
@test mean(a) === mean(af)
249+
@test std(a) === std(af)
250+
@test var(a) === var(af)
251+
m = mean(a)
252+
@test stdm(a, m) === stdm(af, m)
253+
@test varm(a, m) === varm(af, m)
254+
end
255+
245256
@testset "convert result type" begin
246257
x = Fixed{Int8,7}(0.75)
247258
for T in (Float16, Float32, Float64, BigFloat)

test/normed.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using FixedPointNumbers, Test
1+
using FixedPointNumbers, Statistics, Test
22
using FixedPointNumbers: bitwidth
33

44
@testset "domain of f" begin
@@ -449,6 +449,17 @@ end
449449
@test prod(a, dims=1) == [acmp]
450450
end
451451

452+
@testset "reductions, Statistics" begin
453+
a = N0f8[reinterpret(N0f8, 0x80), reinterpret(N0f8, 0x40)]
454+
af = FixedPointNumbers.Treduce.(a)
455+
@test mean(a) === mean(af)
456+
@test std(a) === std(af)
457+
@test var(a) === var(af)
458+
m = mean(a)
459+
@test stdm(a, m) === stdm(af, m)
460+
@test varm(a, m) === varm(af, m)
461+
end
462+
452463
@testset "rand" begin
453464
for T in (Normed{UInt8,8}, Normed{UInt8,6},
454465
Normed{UInt16,16}, Normed{UInt16,14},

0 commit comments

Comments
 (0)