SQL/MX 3.2 Programming Manual for C and COBOL (H06.25+, J06.14+)

Host Variables in C/C++ Programs
HP NonStop SQL/MX Release 3.2 Programming Manual for C and COBOL663854-002
3-25
Numeric Data
For example, if the numerator is NUMERIC (70, 10) and the denominator is
NUMERIC (50, 20), the precision and the scale of the result will be calculated
as follows:
Magnitude of the numerator is 70 - 10 = 60.
Scale of the numerator is 10.
Magnitude of the denominator is 50 - 20 = 30.
Scale of the denominator is 20.
Therefore, Scale of quotient = 10 + 30 = 40.
Therefore, Magnitude of quotient = 60 + 20 = 80.
Hence, precision of the quotient = 80 + 40 = 120.
GNU GMP library for BigNum
The user program uses the GNU GMP library for extensive number processing. You
can convert the new BigNum external binary format into the GMP format by using GMP
low-level library routines, mpz_import(). The following example shows the usage of
mpz_import()function:
// Convert the str which has the value "12345678901234567890"
from internal format to GMP.
//Declare the Bignum host variable
NUMERIC(50,5) s;
//Get the storageLength for the NUMERIC s
unsigned short storageLength =SQL_BignumSize(50);
//First, convert the str "12345678901234567890' to Internal
format
SQL_BignumFromStr(s, storageLength , str,
(unsignedshort)strlen(str));
//Declare the Bignum in GMP format
mpz_t z;
// Convert the Internal format s to GMP format z
mpz_import(z, // Output multi-precision in GMP format.
5, // Number of chunks in s.
-1, // Least significant chunk first.
2, // Size of each chunk, in bytes.
0, // Native endianness within the chunk.
0, // Nails.
s // Input Bignum in internal format.
);