Repository navigation
Add widen for FixedPoint types - #348
PatrickHaecker wants to merge 4 commits into
Conversation
Widening the raw type and keeping the number of fractional bits makes `+` and `-` safe from overflow, as `Base.widen` requires, and makes the generic `widemul` work for fixed-point numbers. 128-bit raw types stay unsupported because `widen(Int128)` is `BigInt`, which is no valid raw type.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #348 +/- ##
==========================================
+ Coverage 96.83% 96.85% +0.01%
==========================================
Files 7 7
Lines 791 795 +4
==========================================
+ Hits 766 770 +4
Misses 25 25 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Personally, I think it is a reasonable proposal. In short, I think a docstring is necessary. And what is even more problematic is the fallback implementation of julia> widemul(1N0f8, 1N1f7)
1.0N24f8There is clearly no consensus on |
|
This should not be merged until the discussion regarding |
Co-authored-by: kimikage <kimikage.ceo@gmail.com>
Defining `widen` made the generic `widemul(x, y) = widen(x) * widen(y)` apply to fixed-point numbers. That rounds the exact product to the fractional bits of the operands (`widemul(eps(Q0f7), eps(Q0f7)) == 0`) and gives surprising types for mixed `f`. Until an exact `widemul` is designed, it throws a `MethodError` as before. `widen` and `widemul` are imported like the other functions this package extends for end users.
There is hardly any degree of freedom, because
So I agree that there are use cases where other definitions are more useful. However, they either need to use a different function (
I think that's clear. Let However, that's not the focus of this PR, so I made it throw following your suggestion. |
kimikage
left a comment
There was a problem hiding this comment.
The definition of widemul that you consider "clear" is not obvious to me. (This does not mean you are wrong; it simply means there is ambiguity there.)
So, I believe treating it as an error for the time being is the right thing.
Yes, definitely for this PR. However, the discussion about that might be useful to bring a potential future PR forward.
Where do you see the ambiguity? This is how multiplication of fractional values works in binary numbers:
So the only general, type-safe definition (independent of the values) which is aligned to "doubling the number of bits from |
I think it would be better to submit a separate issue. This PR will be merged before a conclusion is reached in the discussion on |
A generic `widen` lets integer types from other packages work without extra methods. A raw type whose `widen` is `BigInt` still throws, because `BigInt` is no valid raw type and would otherwise be returned silently. The docstring covers the value method as well, since end users mostly widen values.
|
Thanks for all the work you put into the package and all the reviews. Ready for the next review iteration. |
Done: #353 |
Widening the raw type and keeping the number of fractional bits makes
+and-safe from overflow, asBase.widenrequires, and makes the genericwidemulwork for fixed-point numbers. 128-bit raw types stay unsupported becausewiden(Int128)isBigInt, which is no valid raw type.The widening logic is the one which is closest to what
Basedoes. However, if users are not happy, they can still add more specific methods which override this behavior for their use cases.