Page 1 of 1

[Feature Request] Add "Sign" function

Posted: 14 Aug 2018, 12:53
by AndreVallestero
A "Sign" function would be very useful in many cases where only a max distance of 1 is allowed which is a case I have encountered many times with directional mouse movement. Sign() would take any number and would return -1 for a negative value, 0 for a 0 value and 1 for a positive value.

Here's a sample implementation for C++:

return -1 + (int)(number > -1) + (int)(number > 0);

Re: [Feature Request] Add "Sign" function

Posted: 14 Aug 2018, 14:07
by nnnik
Why not write it yourself? e.g.:

Code: Select all

sign(nr) {
	return (nr>0)-(nr<0)
}

Re: [Feature Request] Add "Sign" function

Posted: 14 Aug 2018, 15:40
by jeeswg
- I know it's relatively minor but I would agree that this should be added.
- There's a certain selection of basic functions that should be built into all programming languages and I have been writing an article on this that I intend to publish, comparing 4 programming languages.
- I would want AutoHotkey to be a model language in this regard. Cheers.

- New functions I have in mind:
ATan2(vNumY, vNumX)
Pow(vNum1, vNum2)
Sign(vNum)
- Amendments I have in mind (additional parameters):
Ceil(vNum, vDP:=0)
Floor(vNum, vDP:=0)
Log(vNum, vBase:=10)
- Possibly (names could be different):
BaseToDec
Between
DecToBase
MCeil(vNum, vMult:=1)
MFloor(vNum, vMult:=1)
MRound(vNum, vMult:=1) ;round to nearest multiple

Code: Select all

JEE_Sign(vNum)
{
	;return (vNum = 0) ? 0 : Round(vNum/Abs(vNum)) ;logically more secure
	;return Round(vNum/Abs(vNum)) ;also works
	return (vNum > 0) ? 1 : (vNum < 0) ? -1 : 0
}
- Btw @nnnik, nice implementation.

Re: [Feature Request] Add "Sign" function

Posted: 22 Aug 2018, 10:23
by jeeswg
- Something else that could be worth considering is a mod operator. This is very useful for when converting between AutoHotkey and other programming languages.

- I've written some potential code for some of these functions, here:
C++: AHK source code: potential functions - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 23&t=54392