-
Notifications
You must be signed in to change notification settings - Fork 2k
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
CS2 Discussion: Features: integer cast/truncation operator(s) #4935
Comments
From @GeoffreyBooth on 2016-09-18 15:27 @Inve1951 what would be your preference for what |
From @Inve1951 on 2016-09-18 17:33 I think the most elegant version is compiling It seems ES6 got Switching @GeoffreyBooth what are your thoughts on this topic? |
From @GeoffreyBooth on 2016-09-18 18:12 I’ve never used that operator so I have no thoughts 😄 In coffeescript6/discuss#36 we’re discussing a breaking-change version of CoffeeScript that outputs ESNext, so |
From @Inve1951 on 2016-09-18 18:36
What variant you put in
|
From @GeoffreyBooth on 2016-09-18 18:46 This sounds like a bugfix for You’ll need to explain what exactly |
From @lydell on 2016-09-18 18:50 I have used Just as a note: Python seems to do the same thing as CoffeeScript here:
Might be worth reading through #2887 (and the issues linked to from there) to see if this has been discussed before. |
From @Inve1951 on 2016-09-18 18:57 Just ran a speed test on both in chrome:
Output:
|
From @lydell on 2016-09-18 19:09 I don’t think it’s worth discussing performance before the semantics are worked out. |
From @alangpierce on 2016-09-18 19:12 I don't think this is obviously a bug. Floor division and truncation division are both valid operations, and different languages make different decisions about what to use by default. In Python, Guido intentionally chose the floor behavior. Here's a post explaining why: Floor division and mod that returns a non-negative value ( |
From @Inve1951 on 2016-09-18 19:12 Sorta coming from C there which makes me expect dividing integers simply not resulting in floating point numbers. The float result (if there were one) is truncated at the decimal point (towards zero).
I'd also blame python for that behavior. |
From @Inve1951 on 2016-09-18 19:49 As mentioned in a comment from @alangpierce's link this doesn't work: But I see now that there is a different way to think of "integer division". I still find the implemented behavior really odd. Edit: To me it seems that both implementations are reasonable and I'm clearly in favor of truncation towards zero since there is no int cast in js. (obviously one could just write a function Since it's not a bug please treat this as a proposal for changing the implementation. That's quite brazen, I know... |
From @GeoffreyBooth on 2016-09-18 22:14 I think renaming it to ”floor division operator” in the docs is reasonable. I’m less convinced that we should change the behavior of an operator when the current implementation is reasonable and might be preferred by people. Perhaps rather than changing |
From @Inve1951 on 2016-09-19 17:42 I did some thinking and some experimenting and came up with the following: New operators: I wouldn't add Parentheses What actually happens here is that before any binary operator (like our Bitwise OR I do see one drawback though: So above example One could argue that an int-cast operator is dispensable since you can just write Also one could argue that the int-cast operator is all you need and the other operators are dispensable since And of course the operator could look different to those described here in order to not bring a breaking change along (e.g. Furthermore if those are implemented one could go crazy and demand versions of those which do the int-cast on the operands before calculation but that's just nonsense IMO. I hope this sounds better to you guys 😄 |
From @DomVinyard on 2016-09-21 16:08
But why? to save a couple of keystrokes every so often? There's nothing coffeescript about it, jash would kill these suggestions without mercy. |
From @Inve1951 on 2016-09-23 23:24 @DomVinyard, thanks for your input.
I had that exact same thought, which is what brought me to :
I still do think that there should be the int-cast operator because there is no integer primitive in JS (and no float-to-int cast) and there's situations where you need ints.
It's reasonable to have that operator:
@lydell @GeoffreyBooth For clarification: |
From @GeoffreyBooth on 2017-11-26 02:14 Closing as the consensus above on the original proposal, which suggested redefining the |
From @Inve1951 on 2017-11-27 10:44 docs still say |
From @GeoffreyBooth on 2017-11-27 16:11 Is it as simple as find and replace one phrase for the other? |
From @Inve1951 on 2017-11-30 11:17 yes |
From @Inve1951 on 2016-09-18 11:44
I'm following your progress in this project and want to throw in another topic:
Using
Math.floor()
on negative numbers gives non-straight-forward results.For example right now
-5 // 3
compiles toMath.floor(-5 / 3)
which results in-2
when-1
would be the result of proper "integer division".I came across this stackoverflow question which displays some nice alterantives which even seem to perform better.
Keep up that good work of yours. 👍
The text was updated successfully, but these errors were encountered: