|
The INT_ and FRAC_ Functions
These functions return the integer and the fraction parts of a variable value, respectively.
It is important to note the relationship between these two functions and the special GPP2 variable formats. INT_ and FRAC_ work on the actual value of numeric expressions, ignoring any variable format information. This may lead to confusion in some cases, if variable formats are ignored.
For instance, assume the variable VAR has the value 2.9999, but its format only allows 2 decimal digits for the fraction part. Sending VAR to output will print the string "3.00". When compared to other variables, VAR will behave as if it has the value 3.00 (see discussion on numeric expression comparison later in this document).
However, INT_ and FRAC_ ignore the variable format. INT_(VAR) will return 2, and FRAC_(VAR) will return 0.9999. In most cases, this is indeed the desired result.
If the GPP2 developer needs to integer and fractional values of a variable while taking into account the variable format, then the following methods can be used:
-
Use of the FORMATTED_NUM function. INT_(FORMATTED_NUM(VAR)) will return 3, while FRAC_(FORMATTED_NUM(VAR)) will return 0.0. This method does not change the internal value of VAR - it will remain 2.9999.
-
Actually change the value of VAR to its formatted value before using the INT_ and FRAC_ functions. This can be achieved with the FORMATTED_NUM function (see above). This method changes the internal value of VAR to 3.0000.
Examples
VAR = 2.9999;
IPART = INT_(VAR); Set IPART to 2
FPART = FRAC_(VAR); Set FPART to 0.9999
IPART = INT_(FORMATTED_NUM(VAR)); Set IPART to 3, VAR stays 2.9999
FPART = FRAC_(FORMATTED_NUM(VAR)); Set FPART to 0, VAR stays 2.9999
VAR = FORMATTED_NUM (VAR); Change VAR to 3.0000
IPART = INT_(VAR); Set IPART to 3
FPART = FRAC_(VAR); Set FPART to 0
|