Floor divide operator v. Floor function and divide

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 5425
Joined: 19 Dec 2016, 01:58
Location: UK

Floor divide operator v. Floor function and divide

10 Sep 2018, 11:40

- These give different results. Why?

Code: Select all

q::
MsgBox, % Floor(-4 / 3) ;-2
MsgBox, % Floor(-4.0 / 3.0) ;-2
MsgBox, % (-4 // 3) ;-1
MsgBox, % (-4.0 // 3.0) ;-2.000000
return
- I found a link, but it doesn't explain why there's a difference. Thanks.
Variables and Expressions - Definition & Usage | AutoHotkey
https://autohotkey.com/docs/Variables.htm
Floor divide (//): The double-slash operator uses high-performance integer division if the two inputs are integers.
wolf_II
Posts: 2242
Joined: 08 Feb 2015, 20:55

Re: Floor divide operator v. Floor function and divide

10 Sep 2018, 11:46

if the two inputs are integers: // will do high-performance integer division.
if the two inputs are not integers: // will not do high-performance integer division. I guess it's doing normal division.

PS: Floor division is the name given to the operator, it does not mean Floor() function is involved.
Helgef
Posts: 3303
Joined: 17 Jul 2016, 01:02
Contact:

Re: Floor divide operator v. Floor function and divide

10 Sep 2018, 11:50

Why did you stop reading at the first sentence in documentation you quoted?
Edit: hello wolf_II :wave:
User avatar
jeeswg
Posts: 5425
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Floor divide operator v. Floor function and divide

10 Sep 2018, 14:03

Thanks for your answers.

-4/3 = -1.333333
Floor(-1.333333) = -2
Why would something 'high-performance' give you the wrong answer?
MannyKSoSo
Posts: 278
Joined: 28 Apr 2018, 21:59

Re: Floor divide operator v. Floor function and divide

10 Sep 2018, 14:16

You can take a look at this and see the difference

Code: Select all

SetBatchLines -1
t1 := QPC()
vl := Floor(-4/3)
t2 := QPC()

t3 := QPC()
v2 := Floor(-4//3)
t4 := QPC()

t5 := QPC()
v3 := Floor(-1.333333)
t6 := QPC()
MsgBox % "Elapsed QPC time is " . (t2-t1) . " micro-seconds`r`nElapsed QPC time is " . (t4-t3) . " micro-seconds`r`nElapsed QPC time is " . (t6-t5) . " micro-seconds`r`n" vl ":" v2 ":" v3

;-------------------------------------------------------------------------------
QPC() { ; microseconds precision ; borrowed from wolf_II 
;-------------------------------------------------------------------------------
    static Freq, init := DllCall("QueryPerformanceFrequency", "Int64P", Freq)
	local Count
    DllCall("QueryPerformanceCounter", "Int64P", Count)
    Return (Count / Freq)*1000 
}
The high performance gives the correct answer, but the slower the conversion the worse it gets, its also only related to negative numbers from what I have seen. For some reason it decides that I don't know what I'm doing it rounds the other way by mistake.
User avatar
jeeswg
Posts: 5425
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Floor divide operator v. Floor function and divide

10 Sep 2018, 14:25

- Nicely done MannyKSoSo.
- It's possible there is some good reason for the behaviour, but it's come as a big surprise.
- I was working on a FloorMod function, and it matters greatly which type of 'floor divide' is used.
MannyKSoSo
Posts: 278
Joined: 28 Apr 2018, 21:59

Re: Floor divide operator v. Floor function and divide

10 Sep 2018, 14:27

If its confirmed that negative numbers is the cause you could use the // for anything that is negative and / for anything that is positive (just a simple work around).
lexikos
Posts: 6207
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Floor divide operator v. Floor function and divide

10 Sep 2018, 18:34

-4/3 = -1.333333
Floor(-1.333333) = -2
Why would something 'high-performance' give you the wrong answer?
It is the wrong answer for you because you are asking the wrong question.

High-performance integer division is just integer division, using the native CPU instruction for such. Floating-point division is slower, as with floating-point in general, hence calling integer division high-performance. As I understand it, integer division truncates (or simply does not compute) the fractional part. It does not round, floor or ceiling.

Unfortunately, // truly performs two different operations: "floor division" if either input is floating-point and "integer division" if both are integers.

The following all return true on v2:

Code: Select all

MsgBox Integer(-4/3) = -4//3
MsgBox Integer(+4/3) = +4//3
MsgBox Integer(-5/3) = -4//3
MsgBox Integer(+5/3) = +4//3
Replacing Integer with any other single function will not give the same results.
User avatar
jeeswg
Posts: 5425
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Floor divide operator v. Floor function and divide

14 Sep 2018, 16:23

- @lexikos: Thanks for the information. Appreciated.
- It was while working on these functions that I noticed the problem:
FloorDiv / FloorMod (and an 'always positive' Mod function) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=55751

- The problem exists in Java also:
Math (Java Platform SE 8 )
https://docs.oracle.com/javase/8/docs/a ... /Math.html
For example, floorDiv(-4, 3) == -2, whereas (-4 / 3) == -1.
- Similarly in AutoHotkey: Floor(-4 / 3) = -2, whereas (-4 // 3) = -1.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], DRocks, goksel, Google [Bot], kczx3, TAC109, vvhitevvizard and 99 guests