Sgn Function |
Named Arguments
No
Syntax
Sgn(number)
number
Use: Required
Data Type: Numeric
A numeric expression.
Return Value
A Variant of subtype Integer.
Description
Determines the sign of a number.
Rules at a Glance
The return value of the Sgn function is determined by the sign of number:
If number is... | Sgn Returns |
---|---|
Positive | 1 |
Zero | 0 |
Negative | –1 |
Programming Tips and Gotchas
I suppose that someone, somewhere, has found a really good use for the Sgn function. However, its usefulness escapes me, because you need to carry out a test on the return value of the function identical to that which you could use on the number to find its sign.
If you're planning on using the Sgn function to evaluate a result to False (0) or True (any nonzero value), you could also use the CBool function.
The major use for Sgn—and a fairly trivial one—is to determine the sign of an expression. It's equivalent to the following code:
Public Function Sgn(varNumber as Variant) as Integer If varNumber > 0 Then Sgn = 1 ElseIf varNumber = 0 Then Sgn = 0 Else Sgn = -1 End If
Sgn is useful in cases in which the sign of a quantity defines the sign of an expression. For example:
lngResult = lngQty * Sgn(lngValue)
See Also
- If...Then Statement
Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.