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

Equality between Gains is not transitive, prevents correct hashing #402

Open
sostock opened this issue Nov 26, 2020 · 1 comment
Open

Equality between Gains is not transitive, prevents correct hashing #402

sostock opened this issue Nov 26, 2020 · 1 comment
Labels
logarithmic logarithmic scales (decibels, nepers, …) v2.0

Comments

@sostock
Copy link
Collaborator

sostock commented Nov 26, 2020

When comparing a power gain Gain{L, :p} and a root-power gain Gain{L, :rp} with the same L, they get promoted to Gain{L, :?} without changing their numerical values. This breaks the transitivity of ==:

julia> a = Gain{Unitful.Decibel, :rp}(20) # 20 dB (root-power) == factor 10
20 dB

julia> b = Gain{Unitful.Decibel, :p}(20) # 20 dB (power) == factor 100
20 dB

julia> c = Gain{Unitful.Bel, :p}(2) # 2 B (power) == factor 100
2 B

julia> a == b == c
true

julia> a == c
false

Comparison involving numbers is intransitive as well:

julia> a = Gain{Unitful.Decibel, :rp}(20) # 20 dB (root-power) == factor 10
20 dB

julia> b = Gain{Unitful.Decibel, :p}(10) # 10 dB (power) == factor 10
10 dB

julia> c = Gain{Unitful.Decibel, :p}(20) # 20 dB (power) == factor 100
20 dB

julia> a == 10 == b
true

julia> a == b
false

julia> 10 == a == c
true

julia> 10 == c
false

To obtain a consistent behavior for ==, the promotion rules for Gain would have to be changed. It might be difficult to figure out a set of rules that is both practical and consistent, unless one gets rid of Gain{L, :?} altogether.

@sostock
Copy link
Collaborator Author

sostock commented Nov 30, 2020

One consequence of the intransitive isequal is that a correct hash implementation for Gain is impossible: We have

  • isequal(Gain{Decibel, :p}(20), Gain{Decibel,:rp}(20)),
  • isequal(Gain{Decibel, :p}(20), 100), and
  • isequal(Gain{Decibel,:rp}(20), 10),

but hash(10) != hash(100).

@sostock sostock changed the title Equality between Gains is not transitive Equality between Gains is not transitive, prevents correct hashing Nov 30, 2020
@sostock sostock added the logarithmic logarithmic scales (decibels, nepers, …) label Dec 6, 2022
@sostock sostock added the v2.0 label Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logarithmic logarithmic scales (decibels, nepers, …) v2.0
Projects
None yet
Development

No branches or pull requests

1 participant