Skip to content

Commit

Permalink
Avoid undesired promotions in entropy calculations (#925)
Browse files Browse the repository at this point in the history
* Avoid undesired promotions in `entropy` calculations

* Clean file
  • Loading branch information
devmotion committed Apr 3, 2024
1 parent 4c9e559 commit f8a7da2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,13 @@ function entropy(p)
return -sum(xlogx, p)
end

entropy(p, b::Real) = entropy(p) / log(b)
function entropy(p, b::Real)
e = entropy(p)
# Promote explicitly before applying `log` to avoid undesired promotions
# with `log(b)::Float64` arising from `b::Int` (ref: #924)
_b = first(promote(b, e))
return e / log(_b)
end

"""
renyientropy(p, α)
Expand Down
4 changes: 4 additions & 0 deletions test/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ it = (xᵢ for xᵢ in x)
@test @inferred(entropy([1//2, 1//2], 2)) 1.0
@test @inferred(entropy([0.2, 0.3, 0.5], 2)) 1.4854752972273344

# issue #924
@test @inferred(entropy([0.5f0, 0.5f0], 2)) isa Float32
@test @inferred(entropy([0.5f0, 0.5f0], MathConstants.e)) isa Float32

@test_throws ArgumentError @inferred(entropy(Float64[]))
@test_throws ArgumentError @inferred(entropy(Int[]))

Expand Down

0 comments on commit f8a7da2

Please sign in to comment.