Math Functions
Table 9-8. Math Functions
Function Name | Description |
---|---|
Abs(number) | Return the absolute value. |
Fix(number, decimals) | Return a number with a specified number of significant digits. |
Int(number), numerator \ denominator | Return the integer portion of a fractional number. |
Pi | 3.14… |
Remainder(numerator, denominator), | Return the remainder of dividing the numerator by the denominator. |
numerator Mod denominator | Return the remainder of dividing the numerator by the denominator. |
Round(number, decimals) | Round up a number with a specified number of significant digits. |
Sgn(number) | Return number’s sign. |
Sqr(number), Exp(number), Log(number) | The standard arithmetic functions. |
Cos(number),Sin(number),Tan(number), Atn(number) | The standard scientific functions. |
The math functions listed in Table 9-8 are similar to the corresponding functions in .NET. There are only a couple of interesting points to notice.
Working with whole numbers and decimals is done in numerous ways. The Fix() and Round() functions take a fractional number and truncate it to a specified number of digits. The Round() function will round up to the nearest decimal. The number of decimals to display is optional and the default is zero. The Int() function is similar to the Round() function except that it only returns the whole number and will round down to the nearest whole number. Table 9-9 shows how the three functions will return a different number depending upon the decimal portion and whether the number is positive or negative.
Table 9-9. Examples of Truncating Decimals
Function | 1.9 | -1.9 |
---|---|---|
Fix() | 1 | -1 |
Round() | 2 | -1 |
Int( | 1 | -2 |
If you want to get the whole number and you have the numerator and denominator available, you can use the \ operator to perform integer division. This does the division and only returns the integer portion of the result. The Mod operator and Remainder() function return the remainder after doing the division.
‘Demonstrate the integer division and the Mod operator
Formula = 10 \ 3 ‘Returns 3
Formula = 10 mod 3 ‘Returns 1
Formula = Remainder(10, 3) ‘Returns 1