Math Functions
Table 6-8. Math Functions
Function Name | Description |
---|---|
Abs(number) | Return the absolute value. |
Ceiling(number, multiple) | Rounds up to the nearest multiple you specify. |
Truncate(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 (same as the Remainder function). |
Round(number, decimals),RoundUp(number, decimals) | Round up a number with a specified number of significant digits. |
Sgn(number) | Return a 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. |
Most of these functions perform basic mathematical functionality. There are only a couple of interesting points to notice. Working with whole numbers and decimals is done 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 6-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 6-9. Examples of Truncating Decimals
Function | 1.9 | -1.9 |
---|---|---|
Fix() | 1 | -1 |
Round() | 2 | -2 |
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.