[BRL.Math] Sign() function

BlitzMax Forums/BlitzMax Module Tweaks/[BRL.Math] Sign() function

Kryzon(Posted 2011) [#1]
Sign or Signum is a function that returns '1' for positive values, '0' for zero and '-1' for negative values.
It's useful when dealing with several algebraic operations such as Dot products etc.
It is not implemented by default in the system library, but it's very easy to reproduce.

Disclaimer: The way it will be implemented assumes the compiler treats boolean 'True' values as any positive number, and 'False' values as zero. This allows for a fast version of it.
GCC agrees with this behavior, so there shouldn't be any problem (maybe when compiling in other platforms; I wouldn't know).

Instructions:
• Open BRL.mod\Math.mod\Math.C.
• Add this to the end of the code:
double bbSign( double x ){
	return -(x<0) + (x>0);
}
This should be below "bbCeil" or whatever function one might have added there last.
• Save and close this file.

• Open BRL.mod\Math.mod\Math.BMX.
• Add this to the end of the code, but inside the "End Extern" statement:
Rem
bbdoc: Signum of @x
returns: 1 for positive values, 0 for zero, -1 for negative values.
End Rem
Function Sign:Double( x:Double )="bbSign"

End Extern
Note the original "End Extern" that was already there. It needs to enclose this new Sign() declaration just like all the other declarations that were there.
• Save and close this file.

• You need to rebuild the math module. Two ways to do this: select 'Build Modules' (CTRL+D) in your IDE (for Windows, Mac or Linux), or follow the instructions below (just for Windows):
- Open a Command Prompt (MS-DOS).
- Head over to "[...]\blitzmax\bin"
- Type the following (without quotes): "bmk makemods brl.math"
- It will build both Release and Debug versions of the module.

• To rebuild the documentation so this Sign() function is added to the help files and is highlighted when you are programming in BMax, select 'Rebuild Documentation' in your IDE and wait for the process to complete.


GfK(Posted 2011) [#2]
It is not implemented by default in the system library, but it's very easy to reproduce.

Sgn() ?


Kryzon(Posted 2011) [#3]
Can you find a system header where it's declared? from what I've read there's no standard for C or C++, just for other higher-level languages like VB.


GfK(Posted 2011) [#4]
Can you find a system header where it's declared? from what I've read there's no standard for C or C++
No idea what you're talking about with your system headers - that stuff's beyond me. Just wondering why you've gone to the trouble of adding a function which Blitzmax already has.


Kryzon(Posted 2011) [#5]
How about that, you're right!

Still, if someone wants a faster way EDIT: implementing your own Sign() is not faster... after some tests Sgn() is much, much faster and I think due to the fact it's from BRL.Blitz, the core module that is always imported to apps.
Back to the sketch board *sigh*...

Last edited 2011