Floor Division

Get help with using AutoHotkey and its commands and hotkeys
1100++
Posts: 78
Joined: 10 Feb 2018, 19:05

Floor Division

09 Jul 2018, 12:00

Try this:

Code: Select all

MsgBox % "-3//2 = " -3//2 "`n-3.0//2 = " -3.0//2
gregster
Posts: 5840
Joined: 30 Sep 2013, 06:48

Re: Floor Division

09 Jul 2018, 12:07

Read this:
Floor divide (//): The double-slash operator uses high-performance integer division if the two inputs are integers. For example, 5//3 is 1 and 5//-3 is -1. If either of the inputs is in floating point format, floating point division is performed and the result is truncated to the nearest integer to the left. For example, 5//3.0 is 1.0 and 5.0//-3 is -2.0.
1100++
Posts: 78
Joined: 10 Feb 2018, 19:05

Re: Floor Division

09 Jul 2018, 15:03

That seems inconsistent.
gregster
Posts: 5840
Joined: 30 Sep 2013, 06:48

Re: Floor Division

09 Jul 2018, 15:38

Like this, you have options - you can round a floating point number to an integer, if you need it, or format an integer as floating point (I think adding 0.0 to an integer will already do this, try msgbox % 4 + 0.0 ), before applying floor division.

I think the floating point version is what is usually considered as floor division while the other case is what is indeed often called integer division (a certain type of it, at least), especially the specific kind you will encounter in some programming languages like C, which will return only integers.

But afaik, integer division is not clearly defined in mathematics, it is not closed in a mathematical sense and hence, not handled unambiguously. So, consistency might not really be a relevant category here... it is rather a matter of definition.

Since AHK's source code is written in C++, it can probably make direct use of some "high performance" integer division of the language. Perhaps some of our C++ or AHK source code gurus can chime in on this. (So far, I have succesfully suppressed my long-ago experiences with C :D )

But in the end, it is documented like this, not really uncommon in programming and it can be handled. So I wouldn't expect it to change (which might even break a number of older scripts).

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Dsalomon, Google [Bot] and 37 guests