FloorDiv / FloorMod (and an 'always positive' Mod function)

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 5939
Joined: 19 Dec 2016, 01:58
Location: UK

FloorDiv / FloorMod (and an 'always positive' Mod function)

14 Sep 2018, 16:12

- 99% of the time (possibly 100%) when using Mod, I want a positive (or 0) answer. Not a negative answer.
- I had considered popularising the concept of a 'ModPsv' function, and recommending it for different programming languages.
- However, it turns out that Java has something not quite the same, but sufficient, a FloorMod function.
- For FloorMod(Dividend, Divisor), if the divisor is positive, the result is positive/zero. That is sufficient for my needs. (AFAIK I've never needed a negative divisor.)
- Without such a FloorMod or ModPsv function you have to add in fiddly/less readable code for handling or avoiding the possibility of a negative result.
- Here are 'FloorMod' and 'ModPsv'. (I also throw in a FloorDiv function.)

Code: Select all

JEE_FloorDiv(vNum1, vNum2)
{
	;return vNum1 // vNum2 ;can give an incorrect answer
	return Floor(vNum1 / vNum2)
}

;if vNum2 is positive, result is positive(/zero)
JEE_FloorMod(vNum1, vNum2)
{
	;return vNum1 - ((vNum1 // vNum2) * vNum2) ;can give an incorrect answer
	return vNum1 - (Floor(vNum1 / vNum2) * vNum2)
}

;modulo function but always positive
;for AHK's Mod(Dividend, Divisor), if dividend is negative, result is negative
JEE_ModPsv(vDividend, vDivisor)
{
	vRet := Mod(vDividend, vDivisor)
	return (vRet >= 0) ? vRet : vRet + Abs(vDivisor)
}
- There are notes on Java's floorDiv and floorMod functions here. I quote one curio.
Math (Java Platform SE 8 )
https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
For example, floorDiv(-4, 3) == -2, whereas (-4 / 3) == -1.
- Similarly in AutoHotkey v1: Floor(-4 / 3) = -2, whereas (-4 // 3) = -1.

- In Python, the mod operator (%) returns a positive number if the divisor is positive.
- I.e. the kind of behaviour I would like is built-in. (But with the drawback of reduced compatibility with other programming languages)

Code: Select all

[AutoHotkey/C++/Java: the result uses the sign of the 1st number (the dividend)]

[AutoHotkey]
vOutput := ""
vOutput .= Mod(16,10) "," ;6
vOutput .= Mod(16,-10) "," ;6
vOutput .= Mod(-16,10) "," ;-6
vOutput .= Mod(-16,-10) "," ;-6
MsgBox, % RTrim(vOutput, ",")

[C++]
std::cout << 16 % 10 << std::endl; //6
std::cout << 16 % -10 << std::endl; //6
std::cout << -16 % 10 << std::endl; //-6
std::cout << -16 % -10 << std::endl; //-6

[Java]
System.out.println(16 % 10); //6
System.out.println(16 % -10); //6
System.out.println(-16 % 10); //-6
System.out.println(-16 % -10); //-6

[Python: the result uses the sign of the 2nd number (the divisor)]

[Python]
print(16 % 10) #6
print(16 % -10) #-4
print(-16 % 10) #4
print(-16 % -10) #-6
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions”

Who is online

Users browsing this forum: AviationGuy, blue83, DuyMinh and 55 guests