changing operators // and //= to only do integer or floor division

Discuss the future of the AutoHotkey language
User avatar
jeeswg
Posts: 5136
Joined: 19 Dec 2016, 01:58
Location: UK

changing operators // and //= to only do integer or floor division

Post by jeeswg » 22 Sep 2018, 04:19

- Like <>, this pull request would likely prompt a lot of discussion:
Changing operators // and //= to only do integer division. by HelgeffegleH · Pull Request #116 · Lexikos/AutoHotkey_L · GitHub
https://github.com/Lexikos/AutoHotkey_L/pull/116
- Here's a summary of the current behaviour:

Code: Select all

q:: ;test // in AHK v1
MsgBox, % (3 // 2) ;1 ;integer division: round towards 0
MsgBox, % (3.0 // 2) ;1.000000 ;floor division: round down

MsgBox, % (-3 // 2) ;-1 ;integer division: round towards 0
MsgBox, % (-3.0 // 2) ;-2.000000 ;floor division: round down

MsgBox, % (3 // -2) ;-1 ;integer division: round towards 0
MsgBox, % (3.0 // -2) ;-2.000000 ;floor division: round down

MsgBox, % (-3 // -2) ;1 ;integer division: round towards 0
MsgBox, % (-3.0 // -2) ;1.000000 ;floor division: round down
return
- Link:
Variables and Expressions - Definition & Usage | AutoHotkey
https://autohotkey.com/docs/Variables.htm#MulDiv

- There's a logic to making // consistent for floats and integers. They handle positive results in the same way, but handle negative results differently (rounding them towards 0 (up), or down).
- I would consider how other programming languages do it. Python always rounds down, it uses floor division: e.g. -3 // 2 = -2 and -3.0 // 2 = -2.0. (C++ and Java use // for comments.)
- Which would break more scripts, the round towards 0 or the round down behaviour?
- (I'd be tempted to use the round down behaviour always, this would maintain the 'floor division' name and seems more useful. The round towards 0 behaviour doesn't seem useful to me, but maybe there are arguments for it.)
- (The pull request proposal would make // be 'integer division', which seems to me the wrong way to go.)
- Potentially, if people wanted, an IntDiv (or other name) function could be added.
guest3456
Posts: 2419
Joined: 09 Oct 2013, 10:31

Re: changing operators // and //= to only do integer or floor division

Post by guest3456 » 22 Sep 2018, 08:18

Helgef already addressed your point in the PR:
floor divide is already possible with (more readable) floor(x/y)
you ignored mentioning this function in your post. you are basically suggesting that Floor() and // be exactly the same (round down), yes?

User avatar
jeeswg
Posts: 5136
Joined: 19 Dec 2016, 01:58
Location: UK

Re: changing operators // and //= to only do integer or floor division

Post by jeeswg » 22 Sep 2018, 09:34

- (So first my posts are too long for you, now they're too short!?)

- Floor(x/y) and x // y would be identical under my proposal, and currently they are identical when using positive integers or floats.
- The key question is should x // y be floor division or integer division. IMO it is clear that it should be floor division. (I'm surprised that the idea of // being integer division was even suggested, but I prefer to state things softly.)
- IMO also, the only reason integer division is being suggested is because you wouldn't have to introduce any new syntax. But with the severe drawback that // would be inconsistent with all other programming languages AFAIK.

- Making (a != b) consistent with !(a = b), good idea. Introducing !==, good idea. Removing <>, bad idea (it does not logically follow).
- Making // consistent for integers and floats, good idea. Making // become integer division, bad idea (it does not logically follow).
- In both cases, coming up with the ideas and writing the C++ code for it, admirable.
User avatar
tank
Posts: 2373
Joined: 28 Sep 2013, 22:15
Facebook: charlie.simmons.7334
Google: ttnnkkrr
GitHub: ttnnkkrr
Location: Louisville KY
Contact:

Re: changing operators // and //= to only do integer or floor division

Post by tank » 22 Sep 2018, 09:58

jeeswg wrote:(So first my posts are too long for you, now they're too short!?)
what precisely is your point here your entire post could be reduced to the word yes.
Many of your points are off-topic or rambling
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
If you have forum suggestions please submit a pull request
User avatar
jeeswg
Posts: 5136
Joined: 19 Dec 2016, 01:58
Location: UK

Re: changing operators // and //= to only do integer or floor division

Post by jeeswg » 22 Sep 2018, 10:23

- @tank: guest3456 is implying certain points. Every indication suggests that if I don't respond to those points now, he will ask them in follow-up questions. I'm just saving time. I know some of the users on this forum pretty well by now. (He packs a lot into a few sentences.)
- (And in case you hadn't noticed, some users try to get you into a position where you criticise other users, and thus cause drama or damage friendships, I try to avoid this by praising other users where I can. Some of us, when we invest in code changes, we really care about this.)

- @tank: I like all the detail, well-organised and written succinctly and clearly. Also, not everyone will be fully clued up on all topics before reading.
- When I asked you to point to one post of mine that could be improved, it was already quite short with simple sentences, and yet you edited it to the point where you'd removed most of the useful information!?
- I've only had less than a handful of people complain about my thorough posts, but these people tend to be a bit complacent in several respects, and I've noticed other trends, but I'd rather not go into it.
iseahound
Posts: 307
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: changing operators // and //= to only do integer or floor division

Post by iseahound » 24 Sep 2018, 19:22

Hmm... this is really interesting.

In OCaml, there are different operators for floats and integers.
Image
Image

Typically the operators are overridden into:
Image

I'm against // because how would we do integer add, subtract, and multiply?
Finally, for most people // just means "discard the remainder".

Source: https://medium.com/arena-tech-blog/ocam ... a33bb39072
User avatar
jeeswg
Posts: 5136
Joined: 19 Dec 2016, 01:58
Location: UK

Re: changing operators // and //= to only do integer or floor division

Post by jeeswg » 25 Sep 2018, 05:08

- Thanks iseahound, that is an interesting source. How common is OCaml?

- The PR mentioned that if you wanted to do floor division you could use Floor. Well likewise, if you wanted to do integer division you could use Integer.
- And so the question becomes: which is more standard, // for floor division or integer division, Python for example uses floor division.

Code: Select all

MsgBox(-3 / 2) ;-1.5
MsgBox(-3.0 / 2.0) ;-1.5

MsgBox(-3 // 2) ;-1 ;I would make this -2 like Python (// would then be consistent)
MsgBox(-3.0 // 2.0) ;-2.0

MsgBox(Integer(-3 / 2)) ;-1
MsgBox(Integer(-3.0 / 2.0)) ;-1

MsgBox(Floor(-3 / 2)) ;-2
MsgBox(Floor(-3.0 / 2.0)) ;-2
iseahound
Posts: 307
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: changing operators // and //= to only do integer or floor division

Post by iseahound » 25 Sep 2018, 10:00

Guido explains why he uses floor division over integer division here: http://python-history.blogspot.com/2010 ... loors.html

Also, Ocaml is common and rare, depending on ones needs. It's a dialect of Caml, which is descended from ML (Meta Language). OCaml is notable for implementing Coq which is a theorem prover.
User avatar
jeeswg
Posts: 5136
Joined: 19 Dec 2016, 01:58
Location: UK

Re: changing operators // and //= to only do integer or floor division

Post by jeeswg » 09 Oct 2018, 15:53

- @iseahound: Great link thanks. It has some good reasons for why floor division (divide and round towards negative infinity) is more useful than integer division (divide and round towards zero).

- I was thinking that although // would be best for floor division, and is generally more useful. An operator for integer division could be useful also, e.g. to safely and succinctly convert a / b, which is integer division in many languages, when a and b are of integer type.
- Replacing operators with functions e.g. a%b to Mod(a,b) or / to Integer(a/b) can overcomplicate/lose the structure of a formula and can introduce new bugs. And you can get formulas worse than the example below.

Code: Select all

;from the AutoHotkey source code (util.cpp)
;C++:
(yday - (yday - wday + ISO_WEEK1_WDAY + ((366 / 7 + 2) * 7)) % 7 + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY);

;AHK:
(yday - Mod((yday - wday + ISO_WEEK1_WDAY + ((366 // 7 + 2) * 7)), 7) + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY)

;AHK (if we replace / with Integer() and /):
(yday - Mod((yday - wday + ISO_WEEK1_WDAY + ((Integer(366 / 7) + 2) * 7)), 7) + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY)
- Choosing an integer division symbol would be difficult. a idiv b if there isn't a better one.
Post Reply

Return to “AutoHotkey v2 Development”