-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add signed(T)
, unsigned(T)
.
#30445
Add signed(T)
, unsigned(T)
.
#30445
Conversation
Converts Types into their corresponding signed/unsigned equivalents, maintaining storage size, but changing signed-ness.
5e7915e
to
1a5a012
Compare
(Still needs tests, etc, but just wanted to see what y'all think first! 😄) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use these extensions often. For the builtin types, I favor
unsigned(::Type{Int64}) = UInt64
unsigned(::Type{Float32}) = UInt32 # important for low level floating point work
unsigned(x::T) where {T<:Union{Int8..Int128}} = reinterpret(unsigned(T), x)
# etc and cover signed() and e.g. floating(::Type{UInt32}) = Float32 ..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I approve of adding this, once it has tests.
@JeffreySarnoff, I don't doubt being able to derive the "plain bits" type corresponding to a floating-point type is useful, but I don't think that operation can be called unsigned
. Maybe call that planbitstype
or something.
:) Thanks Tim! I've added Tests. This will probably also need a News change before it could be merged.
I have to agree with Tim about that. Converting a float to an int seems beyond the scope of |
Also, since I restored the original definitions, I also reordered the methods back to their original places.
Added unit tests, and also added the missing implementations for calling `signed` on already signed types and same for `unsigned`.
2c6b04f
to
6a34de1
Compare
@test_throws InexactError signed(UInt(-3)) | ||
@test signed(UInt(0) - 1) === -1 | ||
@test_throws InexactError signed(UInt(-3)) # Note that UInt() throws, not signed(). | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed all these tests to be ===
from ==
, because they would be equal even if signed()
did nothing. Lemme know if you don't like that! :)
Also, I was a bit confused by the @test_throws
here. I clarified its behavior with this # Note
, but maybe it's better to fix/clarify the test itself? I'm not reallly sure what it's testing though, unfortunately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand that test either. Does git blame
shed any light?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the test was added here: #11962, during v0.4.0
.
Maybe the behavior was different back then, or maybe it was a mistake? (It was David's first PR ever after all! 😋) I'm happy to either remove the whole line or just leave the comment; whatever you prefer!
@test_throws InexactError signed(UInt(-3)) | ||
@test signed(UInt(0) - 1) === -1 | ||
@test_throws InexactError signed(UInt(-3)) # Note that UInt() throws, not signed(). | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand that test either. Does git blame
shed any light?
Also: is this news-worthy? I'm ambivalent on that, but here's a draft for what i can put there: * There are new method overloads `unsigned(T::Type)` and `signed(T::Type)` that accept a `Type`,
and return its corresponding signed/unsigned equivalent type ([#30445]). |
Fixes docstrings for `unsigned(T::Type)` and `signed(T::Type)`. Co-Authored-By: NHDaly <[email protected]>
(K, i added a NEWS entry based on your thumbsup) |
Line 202 had trailing whitespace in the docstring which broke the tests. Fixed.
Now that I understand we're not supposed to generate these every time, i've removed the generated links to prevent conflicts.
I reran the tests, and they're passing now. :) Please take another look when you get a chance! :) |
Friendly ping, this should be good to merge. Please take another look? |
I am wondering what benefit there is to avoiding symmetry in these two functional definitions.
(I don't have v1.2 available to run these .. if the foregoing does not work, there are bigger issues) |
@JeffreySarnoff I completely agree. We discussed it above, here: #30445 (comment) -- the reason is to mirror the existing asymmetry in the definitions of I opened #30473 to discuss this further. I think we should probably make But that also seems orthogonal to this PR, which is why i separated it into #30473. |
I want to see this resolved as much as you do (modulo keypress as unit of effort). There is nothing in my experience that suggests spackle over a conceptual inelegance is a winning approach -- there are situations wherein it is the only approach available, fortunately this is not so for us. Rather than orthogonal concerns there is a dependance relationship. Get the underpinning correct and this PR is implemented with clarity, coherence and aplomb. Leaving the underpinning for resolution after the band-aid is applied ... well, who enjoys ripping off a bandage when the alternative is no bandage? I don't mind that there are split into two PR. It is my considered view that we need to come to an agreement on the other first. I am willing to help do that. |
I have a feeling that's not all. @timholy the first version of SaferIntegers was a conversion of your approach writing the pkg for Ints that round themselves -- and I put a premium on minimizing changes. I remember spending days on why you had to use discrepant approaches to the sogned and the unsigned types and wondering how to bring both under a shared umbrella Integer type that would support the expressiveness we like. |
Users can extend with their own types, just like for [un]signed(v).
Now that #33459 was merged, I've (finally) updated this PR to the simple version suggested by @JeffreySarnoff above. @JeffBezanson on the review of #33459, you noted that you're "really surprised we still have |
Though note that, the whole motivation for this PR was the observation that Base already includes an (undocumented) method for (Which, actually, this makes me wonder why i didn't see a method redefinition warning.. Ah, never mind yeah there is one. So I guess if we rename these, either we'd want to keep that weird undocumented asymmetry, or remove the undocumented method (which is "breaking")? |
fwiw I am happier with |
I can see the appeal of that, but as a counter-argument, Perhaps the best name for these would simply be the more explicit:
|
I think that would be my vote now:
|
This needs a rebase, but looks otherwise reasonable to me. |
If it's just about @JeffBezanson's comment regarding the value of version these functions, then I don't think it's a problem, because the type method doesn't quite have the same semantics. It's not really a value conversion, it's a type property query. So I'm fine with the PR as is. Did I miss another reason to use a different generic function? |
I also agree that these names would be more descriptive. I'm not sure on the policy on renaming exported function in 1.x releases, but someone should chime in before we merge this. |
BTW, PR #34864 was merged. So, at least #30444 could be closed. The discussion in this PR may be still worth continuing, but it seems to be a good idea to change the subject of this PR. cc: @NHDaly, @JeffreySarnoff |
what do you suggest? |
To be honest, I am satisfied with the current However, I think that adding functions such as |
That makes sense. Certainly we should move the direction of these two so we are following one more consistent approach. |
Marked for triage to decide whether to do this or not. |
Sounds good, triage should also considered this PR and the methods names in light of #36526 , as brought up by @kimikage and @JeffreySarnoff |
I believe we added this already. |
Converts Types into their corresponding signed/unsigned equivalents, maintaining storage size, but changing signed-ness.
Fixes #30444.