-
-
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
itrunc for integer truncation is a bad pun #8646
Comments
Maybe time to use |
Possible, but it's a little vague. This really is a modulus operation. |
I agree with this issue, but I think modding something by a type is also a bit strange. |
The other option is that we could say that |
It looks like |
@StefanKarpinski That's not a bad idea; |
It is minimally disruptive and makes this no longer a pun. |
One downside is that |
It can probably be implemented with a couple of integer ops: decode the exponent, shift and mask. |
Hmm. Handling negative floats really makes this hard. |
I still think the So how about an even crazier idea: deprecate This would then leave |
I like the idea of folding |
In particular, I think the argument that we should have |
My inclination is that |
That's fine but what's the generic version? I also think it may be better to be explicit about how you are mapping one type to another – rounding, truncation, modulus, etc. |
I think the generic coercing version should be modulus (for converting a wider integer to a narrower integer) or bitwise copy (for signed to unsigned of the same width). i.e. "do an exact bitwise copy of as many bits as you can, starting from low-order bits", ala C. I think functions like |
This would seem to indicate that you support the behavior of my
I agree that one generally does not want both behaviors at the same time, but it is a single operation that has both kinds of truncation as limiting cases, thereby retroactively justifying the use of |
What is the use-case for truncated coercion that would not be better served by |
The problem with using "lowercase" functions for coercion is that sometimes you have the type; i.e. you know you want to truncate an integer down to type |
Spitballing slightly here, but what if we rolled this into an Base.Unsafe.Convert function (which defaults to calling Base.convert). In the user-defined bounds check PR I outlined this idea some. I can expand more when I get to a computer if this sounds good to someone. |
I brought this up immediately, even before the itruncopalypse change was merged, but using the
itrunc
function like this is a complete abuse and exactly the kind of operator punning that we've tried so hard to avoid in Julia. English happens to use the word "truncate" for both converting large integer types to smaller integer types by lopping off their high bits, and mapping floating-point values to integral values by lopping off the bits after the decimal point. However, these are completely different behaviors and cramming them into the same function is a bad pun. Unfortunately, this wasn't fixed immediately and now everyone's usingitrunc
for this everywhere. I'm working on trying to change this, but I'm afraid is going to cause almost as much trouble as the original change did since everyone will have to change the usages ofitrunc
they just added to something else. I've suggestedmod(n::Int, Int8)
for this.The text was updated successfully, but these errors were encountered: