-
Notifications
You must be signed in to change notification settings - Fork 4
CS2 Discussion: Features: integer cast/truncation operator(s) #39
Comments
@Inve1951 what would be your preference for what |
I think the most elegant version is compiling It seems ES6 got Switching @GeoffreyBooth what are your thoughts on this topic? |
I’ve never used that operator so I have no thoughts 😄 In #36 we’re discussing a breaking-change version of CoffeeScript that outputs ESNext, so |
What variant you put in
|
This sounds like a bugfix for You’ll need to explain what exactly |
I have used Just as a note: Python seems to do the same thing as CoffeeScript here:
Might be worth reading through jashkenas/coffeescript#2887 (and the issues linked to from there) to see if this has been discussed before. |
Just ran a speed test on both in chrome:
Output:
|
I don’t think it’s worth discussing performance before the semantics are worked out. |
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 ( |
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. |
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... |
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 |
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 😄 |
But why? to save a couple of keystrokes every so often? There's nothing coffeescript about it, jash would kill these suggestions without mercy. |
@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: |
Closing as the consensus above on the original proposal, which suggested redefining the |
docs still say |
Is it as simple as find and replace one phrase for the other? |
yes |
* Changelog for 2.1.0; remove text from objects section that is no longer valid for CS2/ES2015. * Update packages * 2.1.0 build * Update output * Correct reference to `//` division, per coffeescript6/discuss#39 (comment)
Migrated to jashkenas/coffeescript#4935 |
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: