### Example 1: Simple Addition with decNumber Source: https://speleotrove.com/decimal/dnusers.html Converts two command-line arguments to decNumber, adds them, and prints the result. Requires linking against the decNumber library and including decNumber.h. The example sets precision to 34 digits and disables traps. ```c // example1.c -- convert the first two argument words to decNumber, // add them together, and display the result #define DECNUMDIGITS 34 // work with up to 34 digits #include "decNumber.h" // base number library #include // for printf int main(int argc, char *argv[]) { decNumber a, b; // working numbers decContext set; // working context char string[DECNUMDIGITS+14]; // conversion buffer if (argc<3) { // not enough words printf("Please supply two numbers to add.\n"); return 1; } decContextDefault(&set, DEC_INIT_BASE); // initialize set.traps=0; // no traps, thank you set.digits=DECNUMDIGITS; // set precision decNumberFromString(&a, argv[1], &set); decNumberFromString(&b, argv[2], &set); decNumberAdd(&a, &a, &b, &set); // a=a+b decNumberToString(&a, string); printf("%s + %s => %s\n", argv[1], argv[2], string); return 0; } // main ``` -------------------------------- ### Example 6 – Packed Decimal numbers: result output Source: https://speleotrove.com/decimal/dnusers.html Sample output from the packed decimal example, showing the result as sixteen hexadecimal pairs and the scale. ```text Result: 00 00 00 00 00 00 00 00 00 00 00 03 52 36 45 1c (scale=2) ``` -------------------------------- ### decimal64 example output: 79 => 22 38 00 00 00 00 00 79 => 79 Source: https://speleotrove.com/decimal/dnusers.html Sample output from the decimal64 example when the input argument is '79' on a big-endian machine. Shows the decimal64 encoding as eight hexadecimal pairs and the resulting string conversion. ```text 79 => 22 38 00 00 00 00 00 79 => 79 ``` -------------------------------- ### Basic decNumber addition example Source: https://speleotrove.com/decimal/dnusers.html This example shows how to add two numbers using decNumber. It initializes a decContext, converts arguments to decNumbers, performs addition, and prints the result. Note that there is no error checking, so invalid arguments produce NaN. ```c ** 1.** decNumber one, mtwo, hundred; // constants ** 2.** decNumber start, rate, years; // parameters ** 3.** decNumber total; // result ** 4.** decContext set; // working context ** 5.** char string[DECNUMDIGITS+14]; // conversion buffer ** 6.** ** 7.** decContextDefault(&set, DEC_INIT_BASE); // initialize ** 8.** set.traps=0; // no traps ** 9.** set.digits=25; // precision 25 **10.** decNumberFromString(&one, "1", &set); // set constants **11.** decNumberFromString(&mtwo, "-2", &set); **12.** decNumberFromString(&hundred, "100", &set); **13.** **14.** decNumberFromString(&start, argv[1], &set); // parameter words **15.** decNumberFromString(&rate, argv[2], &set); **16.** decNumberFromString(&years, argv[3], &set); **17.** **18.** decNumberDivide(&rate, &rate, &hundred, &set); // rate=rate/100 **19.** decNumberAdd(&rate, &rate, &one, &set); // rate=rate+1 **20.** decNumberPower(&rate, &rate, &years, &set); // rate=rate**years **21.** decNumberMultiply(&total, &rate, &start, &set); // total=rate*start **22.** decNumberRescale(&total, &total, &mtwo, &set); // two digits please **23.** **24.** decNumberToString(&total, string); **25.** printf("%s at %s%% for %s years => %s\n", **26.** argv[1], argv[2], argv[3], string); **27.** return 0; ``` -------------------------------- ### Example 8 – Using decQuad with decNumber Source: https://speleotrove.com/decimal/dnusers.html Demonstrates using decQuad with the decNumber module to compute power(2*a, b). Requires decQuad, decNumber, and decimal128 libraries. Compile with example8.c, decContext.c, decQuad.c, decNumber.c, decimal128.c, and decimal64.c. ```c // example8.c -- using decQuad with the decNumber module #include "decQuad.h" // decQuad library #include "decimal128.h" // interface to decNumber #include // for printf int main(int argc, char *argv[]) { decQuad a; // working decQuad decNumber numa, numb; // working decNumbers decContext set; // working context char string[DECQUAD_String]; // number->string buffer if (argc<3) { // not enough words printf("Please supply two numbers for power(2*a, b).\n"); return 1; } decContextDefault(&set, DEC_INIT_DECQUAD); // initialize decQuadFromString(&a, argv[1], &set); // get a decQuadAdd(&a, &a, &a, &set); // double a decQuadToNumber(&a, &numa); // convert to decNumber decNumberFromString(&numb, argv[2], &set); decNumberPower(&numa, &numa, &numb, &set); // numa=numa**numb decQuadFromNumber(&a, &numa, &set); // back via a Quad decQuadToString(&a, string); // .. printf("power(2*%s, %s) => %s\n", argv[1], argv[2], string); return 0; } // main ``` -------------------------------- ### Example 7 – Using the decQuad module: example7.c Source: https://speleotrove.com/decimal/dnusers.html Complete C program using decQuad to add two numbers from command-line arguments. Initializes a decContext with DEC_INIT_DECQUAD, converts strings to decQuad, adds them, converts the result to a string, and prints it. ```c ** 1.** // example7.c -- using decQuad to add two numbers together ** 2.** ** 3.** #include "decQuad.h" // decQuad library ** 4.** #include // for printf ** 5.** ** 6.** int main(int argc, char *argv[]) { ** 7.** decQuad a, b; // working decQuads ** 8.** decContext set; // working context ** 9.** char string[DECQUAD String]; // number->string buffer **10.** **11.** if (argc<3) { // not enough words **12.** printf("Please supply two numbers to add.\.n"); **13.** return 1; **14.** } **15.** decContextDefault(&set, DEC INIT DECQUAD); // initialize **16.** **17.** decQuadFromString(&a, argv[1], &set); **18.** decQuadFromString(&b, argv[2], &set); **19.** decQuadAdd(&a, &a, &b, &set); // a=a+b **20.** decQuadToString(&a, string); **21.** **22.** printf("%s + %s => %s\n", argv[1], argv[2], string); **23.** return 0; **24.** } // main ``` -------------------------------- ### Compound interest calculation with decNumber Source: https://speleotrove.com/decimal/dnusers.html This example calculates compound interest using decNumber. It takes principal, rate, and years as arguments, computes the total, and prints the result. The output format is shown in the preceding example. ```c 100000 at 6.5% for 20 years => 352364.51 ``` -------------------------------- ### Example 5 – decimal64 conversions Source: https://speleotrove.com/decimal/dnusers.html This program demonstrates conversion between decimal64 and string representations using the decNumber library. It initializes a decimal64 context, converts a string argument to decimal64, prints its hexadecimal representation, and converts it back to a string. ```c ** 1.** // example5.c -- decimal64 conversions ** 2.** #include "decimal64.h" // decimal64 and decNumber library ** 3.** #include // for (s)printf ** 4.** ** 5.** int main(int argc, char *argv[]) { ** 6.** decimal64 a; // working decimal64 number ** 7.** decNumber d; // working number ** 8.** decContext set; // working context ** 9.** char string[DECIMAL64_String]; // number->string buffer **10.** char hexes[25]; // decimal64->hex buffer **11.** int i; // counter **12.** **13.** decContextDefault(&set, DEC_INIT_DECIMAL64); // initialize **14.** **15.** decimal64FromString(&a, argv[1], &set); **16.** // lay out the decimal64 as eight hexadecimal pairs **17.** for (i=0; i<8; i++) { **18.** sprintf(&hexes[i*3], "%02x ", a.bytes[i]); **19.** } **20.** decimal64ToNumber(&a, &d); **21.** decNumberToString(&d, string); **22.** printf("%s => %s=> %s\n", argv[1], hexes, string); **23.** return 0; **24.** } // main ``` -------------------------------- ### Example 6 – Packed Decimal numbers: include decPacked.h Source: https://speleotrove.com/decimal/dnusers.html Include directive for the decPacked.h header, required for using packed decimal functions. ```c ** 1.** #include "decPacked.h" ``` -------------------------------- ### Active error handling with signal handler in decNumber example Source: https://speleotrove.com/decimal/dnusers.html Insert this code after line 6 (before the main function) in Example 1, and remove line 18 (set.traps=0;) to enable traps. It sets up a signal handler for SIGFPE that uses longjmp to jump to a preserved point. ```c ** 1.** #include // signal handling ** 2.** #include // setjmp/longjmp ** 3.** ** 4.** jmp_buf preserve; // stack snapshot ** 5.** ** 6.** void signalHandler(int sig) { ** 7.** signal(SIGFPE, signalHandler); // re-enable ** 8.** longjmp(preserve, sig); // branch to preserved point ** 9.** } ``` -------------------------------- ### Set up SIGFPE signal handler and preserve environment with setjmp Source: https://speleotrove.com/decimal/dnusers.html Insert this code after line 11 of Example 1 to set up a signal handler for SIGFPE and preserve the execution environment using setjmp. The handler re-enables the signal and jumps back to the preserved point, allowing error recovery. ```c ** 1.** int value; // work variable ** 2.** ** 3.** signal(SIGFPE, signalHandler); // set up signal handler ** 4.** value=setjmp(preserve); // preserve and test environment ** 5.** if (value) { // (non-0 after longjmp) ** 6.** set.status &= DEC_Errors; // keep only errors ** 7.** printf("Signal trapped [%s].\n", decContextStatusToString(&set)); ** 8.** return 2; ** 9.** } ``` -------------------------------- ### Example 6 – Packed Decimal numbers: main variable declarations Source: https://speleotrove.com/decimal/dnusers.html Declarations for packed decimal input values (investment, rate, years), result buffer, scale variables, and a hex string buffer. Comments indicate the packed representations and their scales. ```c ** 1.** uint8_t startpack[]={0x01, 0x00, 0x00, 0x0C}; // investment=100000 ** 2.** int32_t startscale=0; ** 3.** uint8_t ratepack[]={0x06, 0x5C}; // rate=6.5% ** 4.** int32_t ratescale=1; ** 5.** uint8_t yearspack[]={0x02, 0x0C}; // years=20 ** 6.** int32_t yearsscale=0; ** 7.** uint8_t respack[16]; // result, packed ** 8.** int32_t resscale; // .. ** 9.** char hexes[49]; // for packed->hex **10.** int i; // counter ``` -------------------------------- ### Example 6 – Packed Decimal numbers: decPackedToNumber and arithmetic Source: https://speleotrove.com/decimal/dnusers.html Replacement for lines 14-26 of Example 2, using decPackedToNumber to convert packed inputs, performing division, addition, power, multiplication, and rescale operations, then converting the result back to packed format and printing it as hexadecimal pairs. ```c ** 1.** decPackedToNumber(startpack, sizeof(startpack), &startscale, &start); ** 2.** decPackedToNumber(ratepack, sizeof(ratepack), &ratescale, &rate); ** 3.** decPackedToNumber(yearspack, sizeof(yearspack), &yearsscale, &years); ** 4.** ** 5.** decNumberDivide(&rate, &rate, &hundred, &set); // rate=rate/100 ** 6.** decNumberAdd(&rate, &rate, &one, &set); // rate=rate+1 ** 7.** decNumberPower(&rate, &rate, &years, &set); // rate=rate**years ** 8.** decNumberMultiply(&total, &rate, &start, &set); // total=rate*start ** 9.** decNumberRescale(&total, &total, &mtwo, &set); // two digits please **10.** **11.** decPackedFromNumber(respack, sizeof(respack), &resscale, &total); **12.** **13.** // lay out the total as sixteen hexadecimal pairs **14.** for (i=0; i<16; i++) { **15.** sprintf(&hexes[i*3], "%02x ", respack[i]); **16.** } **17.** printf("Result: %s (scale=%ld)\n", hexes, (long int)resscale); ``` -------------------------------- ### Passive error handling in decNumber example Source: https://speleotrove.com/decimal/dnusers.html Replace lines 14 through 22 in Example 2 with this code to add passive error checking. It checks the status after input conversion and after calculations, printing error messages if invalid inputs or calculation errors occur. ```c ** 1.** decNumberFromString(&start, argv[1], &set); // parameter words ** 2.** decNumberFromString(&rate, argv[2], &set); ** 3.** decNumberFromString(&years, argv[3], &set); ** 4.** if (set.status) { ** 5.** printf("An input argument word was invalid [%s]\n", ** 6.** decContextStatusToString(&set)); ** 7.** return 1; ** 8.** } ** 9.** decNumberDivide(&rate, &rate, &hundred, &set); // rate=rate/100 **10.** decNumberAdd(&rate, &rate, &one, &set); // rate=rate+1 **11.** decNumberPower(&rate, &rate, &years, &set); // rate=rate**years **12.** decNumberMultiply(&total, &rate, &start, &set); // total=rate*start **13.** decNumberRescale(&total, &total, &mtwo, &set); // two digits please **14.** if (set.status & DEC_Errors) { **15.** set.status &= DEC_Errors; // keep only errors **16.** printf("Result could not be calculated [%s]\n", **17.** decContextStatusToString(&set)); **18.** return 1; **19.** } ``` -------------------------------- ### decNumberRemainderNear Source: https://speleotrove.com/decimal/dnnumb.html Computes the remainder of lhs divided by rhs, but returns the remainder closest to zero (the nearest multiple). For example, 10 divided by 6 gives -2 instead of 4. ```APIDOC ## decNumberRemainderNear(number, lhs, rhs, context) ### Description Computes the remainder of _lhs_ divided by _rhs_, but returns the remainder closest to zero (the nearest multiple). For example, if _lhs_ is 10 and _rhs_ is 6, the result is -2 (instead of 4) because the nearest multiple of 6 is 12. ### Parameters - **number** (decNumber*) - Output parameter receiving the remainder. - **lhs** (const decNumber*) - The dividend. - **rhs** (const decNumber*) - The divisor. - **context** (decContext*) - The context controlling precision. ``` -------------------------------- ### decNumberCopy(number, source) Source: https://speleotrove.com/decimal/dnnumb.html Copies a decimal number from source to number. All fields are copied, with the units containing the source's digits copied starting from the least significant unit. ```APIDOC ## decNumberCopy(number, source) ### Description Copies a decimal number from source to number. All fields are copied, with the units containing the source's digits copied starting from the least significant unit. ### Parameters - **number** (decNumber *) - Pointer to the structure to receive the copy. Must have space for source->digits digits. - **source** (decNumber *) - Pointer to the structure to be copied. The source structure is unchanged. ### Returns - **decNumber *** - Pointer to the result (same as number). No error is possible. ``` -------------------------------- ### decContextDefault(context, kind) Source: https://speleotrove.com/decimal/dncont.html Initializes a decContext structure with predefined defaults based on the kind parameter. Supports base, decimal32, decimal64, and decimal128 initialization modes. ```APIDOC ## decContextDefault(context, kind) ### Description Initializes a decContext structure with predefined defaults based on the kind parameter. Supports base, decimal32, decimal64, and decimal128 initialization modes. ### Parameters - **context** (`decContext *`) - Required - Pointer to the structure to be initialized. - **kind** (`int32_t`) - Required - The kind of initialization to perform. Only values defined in the decContext header file are permitted; any other value will initialize the structure to a valid condition but with the `DEC_Invalid_operation` status bit set. ### Behavior - `DEC_INIT_BASE`: Sets digits=9, emax=999999999, round=ROUND_HALF_UP, status cleared, traps set to all DEC_Errors bits except DEC_Rounded, DEC_Inexact, DEC_Lost_digits, and DEC_Subnormal, clamp=0, extended=0. - `DEC_INIT_DECIMAL32` or `DEC_INIT_DECSINGLE`: Sets digits=7, emax=96, emin=-95, round=DEC_ROUND_HALF_EVEN, status cleared, traps cleared, clamp=1, extended=1. - `DEC_INIT_DECIMAL64` or `DEC_INIT_DECDOUBLE`: Sets digits=16, emax=384, emin=-383, other fields as for DEC_INIT_DECIMAL32. - `DEC_INIT_DECIMAL128` or `DEC_INIT_DECQUAD`: Sets digits=34, emax=6144, emin=-6143, other fields as for DEC_INIT_DECIMAL32. ### Returns - **context** (`decContext *`) - The initialized context structure. ``` -------------------------------- ### decDoubleVersion(void) Source: https://speleotrove.com/decimal/dnfloat.html Returns the version string of the decDouble module. ```APIDOC ## decDoubleVersion(void) ### Description Returns the version string of the decDouble module. ### Returns - **const char *** - The version string. ``` -------------------------------- ### decDoubleFromPacked(r, exp, pack) Source: https://speleotrove.com/decimal/dnfloat.html Creates a decDouble from a packed decimal array. The arguments are not checked for speed; no status is set. ```APIDOC ## decDoubleFromPacked(r, exp, pack) ### Description Creates a decDouble number from a packed decimal array. The arguments are not checked for speed; no status is set. The content of r is undefined if the arguments are invalid or out of range. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **exp** (`int32_t`) - In-range unbiased exponent or a special value in the form returned by decDoubleGetExponent. - **pack** (`const uint8_t *`) - Array of `DECDOUBLE_Pmax` packed decimal digits (one digit per four-bit nibble) followed by a sign nibble, and (for decDouble and decQuad only) prefixed with an extra pad nibble (which is ignored); the sign nibble must be any of the six sign codes listed in `decQuad.h` and described for the decPacked module, and digit nibbles must be in the range 0–9. ### Return Value `decDouble *` - The result r. ### Notes This function is also available in the decSingle module. ``` -------------------------------- ### decimal64Canonical(decimal64, source) Source: https://speleotrove.com/decimal/dncomps.html Creates a copy of the source decimal64 with canonical encoding. The decimal64 structure will receive a copy of _source_ with canonical encoding. ```APIDOC ## decimal64Canonical(decimal64, source) ### Description Creates a copy of the source decimal64 with canonical encoding. The decimal64 structure will receive a copy of _source_ with canonical encoding. ### Method Function ### Signature `decimal64 *decimal64Canonical(decimal64 *decimal64, decimal64 *source)` ### Parameters - **decimal64** (`decimal64 *`) - Pointer to the structure to receive a copy of _source_, with canonical encoding. - **source** (`decimal64 *`) - Pointer to the structure to be converted to a canonical encoding. ### Returns Returns _decimal64_; no error is possible from this function. ``` -------------------------------- ### decimal64FromString(decimal64, string, context) Source: https://speleotrove.com/decimal/dncomps.html Converts a character string to a decimal64 value, following the rules for the decNumberFromString function except that precision and exponent range are fixed for the format. The string must be a valid numeric string as defined in the specification. ```APIDOC ## decimal64FromString(decimal64, string, context) ### Description Converts a character string to a decimal64 value, following the rules for the decNumberFromString function except that precision and exponent range are fixed for the format. The string must be a valid numeric string as defined in the specification. ### Method Function ### Signature `decimal64 *decimal64FromString(decimal64 *decimal64, char *string, decContext *context)` ### Parameters - **decimal64** (`decimal64 *`) - Pointer to the structure to be set from the character string. - **string** (`char *`) - Pointer to the input character string. Must be a valid numeric string. The string will not be altered. - **context** (`decContext *`) - Pointer to the context structure which controls the conversion. The precision and exponent range are fixed for each format; the values of _emax_, _emin_, and _digits_ are ignored. ### Returns Returns _decimal64_. ### Errors Possible errors are `DEC_Conversion_syntax` (the string does not have the syntax of a number), `DEC_Overflow` (the adjusted exponent of the number is positive and is greater than _emax_ for the format), or `DEC_Underflow` (the adjusted exponent of the number is negative and is less than _emin_ for the format and the conversion is not exact). If one of these conditions is set, the _decimal64_ structure will have the value `NaN`, ±`Infinity` or the largest possible finite number, or a finite (possibly subnormal) number respectively, with the same sign as the converted number after overflow or underflow. ``` -------------------------------- ### decNumberVersion() Source: https://speleotrove.com/decimal/dnnumb.html Returns a string containing the version of the decNumber library. ```APIDOC ## decNumberVersion() ### Description Returns a string containing the version of the decNumber library. ### Returns - **char *** - A string containing the version, e.g., "decNumber 3.40". ``` -------------------------------- ### decContextGetRounding(context) Source: https://speleotrove.com/decimal/dncont.html Returns the current rounding mode from a decContext structure. ```APIDOC ## decContextGetRounding(context) ### Description Returns the current rounding mode from a decContext structure. ### Parameters - **context** (`decContext *`) - Required - Pointer to the structure whose rounding mode is to be returned. ### Returns - **round** (`enum rounding`) - The current rounding mode. ``` -------------------------------- ### decDoubleFromInt32(r, i) Source: https://speleotrove.com/decimal/dnfloat.html Creates a decDouble from a 32-bit integer. ```APIDOC ## decDoubleFromInt32(r, i) ### Description Creates a decDouble number from the 32-bit integer i and stores the result in r. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **i** (`int32_t`) - Integer input. ### Return Value `decDouble *` - The result r. ``` -------------------------------- ### decContextStatusToString(context) Source: https://speleotrove.com/decimal/dncont.html Returns a pointer to a string describing the status of a decContext structure. ```APIDOC ## decContextStatusToString(context) ### Description Returns a pointer to a string describing the status of a decContext structure. If no bits are set in the status field, a pointer to the string 'No status' is returned. If more than one bit is set, a pointer to the string 'Multiple status' is returned. ### Method Function ### Signature char *decContextStatusToString(decContext *context) ### Parameters - **context** (decContext *) - Required - Pointer to the structure whose status is to be returned as a string. The bits set in the status field must comprise only bits defined in the header file. ### Return Value char * - Pointer to a string describing the status. The content of the string pointed to is a programming interface (it is understood by the decContextSetStatusFromString function) and is therefore not language- or locale-dependent. ``` -------------------------------- ### decDoubleFromBCD(r, exp, bcd, sign) Source: https://speleotrove.com/decimal/dnfloat.html Creates a decDouble from a BCD array. The arguments are not checked for speed; no status is set. ```APIDOC ## decDoubleFromBCD(r, exp, bcd, sign) ### Description Creates a decDouble number from a BCD array. The arguments are not checked for speed; no status is set. The content of r is undefined if the arguments are invalid or out of range. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **exp** (`int32_t`) - In-range unbiased exponent or a special value in the form returned by decDoubleGetExponent. - **bcd** (`const uint8_t *`) - Array of `DECDOUBLE_Pmax` elements, one digit in each byte (BCD8 encoding); the first (most significant) digit is ignored if the result will be a NaN; all are ignored if the result is infinite. All bytes must be in the range 0–9. - **sign** (`int32_t`) - Integer which must be `DECFLOAT_Sign` to set the sign bit of r to 1, or 0 to set it to 0. ### Return Value `decDouble *` - The result r. ### Notes This function is also available in the decSingle module. ``` -------------------------------- ### decDoubleCopySign(r, x, y) Source: https://speleotrove.com/decimal/dnfloat.html Copies the sign of y to the number x and stores the result in r. ```APIDOC ## decDoubleCopySign(r, x, y) ### Description Copies the sign of y to the number x and stores the result in r. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **x** (`const decDouble *`) - Input argument whose value is used. - **y** (`const decDouble *`) - Input argument whose sign is used. ### Return Value `decDouble *` - The result r. ``` -------------------------------- ### decDoubleFromNumber(r, dn, set) Source: https://speleotrove.com/decimal/dnfloat.html Converts a decNumber to a decDouble. Requires the decimal64.h header file for the macro. ```APIDOC ## decDoubleFromNumber(r, dn, set) ### Description Converts a decNumber to a decDouble and stores the result in r. To use this macro, the `decimal64.h` header file must be included. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **dn** (`const decNumber *`) - Input decNumber. - **set** (`decContext *`) - Context for rounding and status. ### Return Value `decDouble *` - The result r. ### Notes This function is also available in the decSingle module. ``` -------------------------------- ### decDoubleCopyNegate(r, x) Source: https://speleotrove.com/decimal/dnfloat.html Copies the negation of a decDouble number from x to r. ```APIDOC ## decDoubleCopyNegate(r, x) ### Description Copies the negation of the decDouble number x to r. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **x** (`const decDouble *`) - Input argument. ### Return Value `decDouble *` - The result r. ``` -------------------------------- ### decDoubleClassString(x) Source: https://speleotrove.com/decimal/dnfloat.html Returns a string describing the class of a decDouble number. ```APIDOC ## decDoubleClassString(x) ### Description Returns a string describing the class of the decDouble number x. ### Parameters - **x** (`const decDouble *`) - Input argument. ### Return Value `const char *` - A string describing the class. ``` -------------------------------- ### decimal64ToEngString(decimal64, string) Source: https://speleotrove.com/decimal/dncomps.html Converts a decimal64 value to an engineering string representation. The arguments and result are the same as for the decimal64ToString function, and similarly no error is possible from this function. ```APIDOC ## decimal64ToEngString(decimal64, string) ### Description Converts a decimal64 value to an engineering string representation. The arguments and result are the same as for the decimal64ToString function, and similarly no error is possible from this function. ### Method Function ### Signature `char *decimal64ToEngString(decimal64 *decimal64, char *string)` ### Parameters - **decimal64** (`decimal64 *`) - Pointer to the structure to be converted to a string. - **string** (`char *`) - Pointer to the character string buffer which will receive the converted number. It must be at least `DECIMAL64_String` (24) characters long. ### Returns Returns _string_; no error is possible from this function. ``` -------------------------------- ### decDoubleIsZero Source: https://speleotrove.com/decimal/dnfloat.html Tests whether a decDouble is zero. ```APIDOC ## decDoubleIsZero ### Description Tests whether a decDouble is zero. ### Function Signature `uint32_t decDoubleIsZero(const decDouble *x)` ### Parameters - **x** (const decDouble *) - Pointer to the decDouble value. ### Return Value - **uint32_t** - Non-zero if zero, zero otherwise. ``` -------------------------------- ### decDoubleSetExponent Source: https://speleotrove.com/decimal/dnfloat.html Sets the exponent of r to exp. exp is not checked for speed; underflow or overflow can result. Result undefined if exp is not a value that could have been produced by decDoubleGetExponent. ```APIDOC ## decDoubleSetExponent(r, set, exp) ### Description Sets the exponent of r to exp. exp is not checked for speed; underflow or overflow can result. Result undefined if exp is not a value that could have been produced by decDoubleGetExponent. ### Parameters - **r** - decDouble to modify. - **set** - Context settings. - **exp** (int32_t) - Exponent value. ### Notes - No status is set. ``` -------------------------------- ### decDoubleZero(r) Source: https://speleotrove.com/decimal/dnfloat.html Sets a decDouble value to zero. ```APIDOC ## decDoubleZero(r) ### Description Sets a decDouble value to zero. ### Parameters - **r** (decDouble *) - Required - The decDouble value to set to zero. ### Notes - This function is also available in the decSingle module. ``` -------------------------------- ### decDoubleCompareTotalMag(r, x, y) Source: https://speleotrove.com/decimal/dnfloat.html Compares the magnitudes of two decDouble numbers using total ordering and stores the result in r. ```APIDOC ## decDoubleCompareTotalMag(r, x, y) ### Description Compares the magnitudes of the decDouble numbers x and y using total ordering and stores the result in r. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **x** (`const decDouble *`) - First input argument. - **y** (`const decDouble *`) - Second input argument. ### Return Value `decDouble *` - The result r. ``` -------------------------------- ### decContextTestEndian(quiet) Source: https://speleotrove.com/decimal/dncont.html Tests the DECLITEND tuning parameter and optionally displays a warning if it is set incorrectly. ```APIDOC ## decContextTestEndian(quiet) ### Description Tests the DECLITEND tuning parameter. If quiet is 0, a warning message is displayed (using printf) if DECLITEND is set incorrectly. If quiet is 1, no message is displayed. ### Method Function ### Signature int decContextTestEndian(uint8_t quiet) ### Parameters - **quiet** (uint8_t) - Required - If 0, a warning message is displayed (using printf) if DECLITEND is set incorrectly. If 1, no message is displayed. ### Return Value int - Returns a value indicating whether DECLITEND is set correctly. ``` -------------------------------- ### decimal64ToString(decimal64, string) Source: https://speleotrove.com/decimal/dncomps.html Converts a decimal64 value to a scientific string representation. The result is placed in the provided character string buffer. ```APIDOC ## decimal64ToString(decimal64, string) ### Description Converts a decimal64 value to a scientific string representation. The result is placed in the provided character string buffer. ### Method Function ### Signature `char *decimal64ToString(decimal64 *decimal64, char *string)` ### Parameters - **decimal64** (`decimal64 *`) - Pointer to the structure to be converted to a string. - **string** (`char *`) - Pointer to the character string buffer which will receive the converted number. It must be at least `DECIMAL64_String` (24) characters long. ### Returns Returns _string_; no error is possible from this function. ``` -------------------------------- ### decDoubleToPacked Source: https://speleotrove.com/decimal/dnfloat.html Converts a decDouble to packed decimal format. pack receives DECDOUBLE_Pmax packed decimal digits (one digit per nibble) followed by a sign nibble and prefixed with an extra pad nibble (0) for decDouble and decQuad. Returns sign flag. ```APIDOC ## decDoubleToPacked(x, exp, pack) ### Description Converts a decDouble to packed decimal format. pack receives DECDOUBLE_Pmax packed decimal digits (one digit per nibble) followed by a sign nibble and prefixed with an extra pad nibble (0) for decDouble and decQuad. Returns sign flag. ### Parameters - **x** - decDouble to convert. - **exp** (int32_t *) - Pointer to store exponent. - **pack** (uint8_t *) - Buffer to store packed digits. ### Returns - **int32_t** - DECFLOAT_Sign if x has sign=1, otherwise 0. ``` -------------------------------- ### decDoubleNextToward Source: https://speleotrove.com/decimal/dnfloat.html Returns the next representable decDouble value toward a second operand. ```APIDOC ## decDoubleNextToward ### Description Returns the next representable decDouble value toward a second operand. ### Function Signature `decDouble *decDoubleNextToward(decDouble *r, const decDouble *x, const decDouble *y, decContext *set)` ### Parameters - **r** (decDouble *) - Pointer to the result decDouble. - **x** (const decDouble *) - Starting operand. - **y** (const decDouble *) - Target operand. - **set** (decContext *) - Context for status flags. ### Behavior - If x = y, result is decDoubleCopySign(r, x, y). - If either operand is NaN, result is as for arithmetic operations. - Otherwise, result is x plus (or minus) an infinitesimal amount, rounded toward +Infinity (or –Infinity) depending on whether y is larger (or smaller) than x. - Flags may be set, except if result is normal (finite, non-zero, not subnormal) no flags are set. ``` -------------------------------- ### decDoubleShow Source: https://speleotrove.com/decimal/dnfloat.html Displays a decDouble value for debugging purposes. Not a programming interface; format may change. ```APIDOC ## decDoubleShow(x, tag) ### Description Displays a decDouble value for debugging purposes. Not a programming interface; format may change. ### Parameters - **x** (void) - decDouble to display. - **tag** (const char *) - Label for output. ### Returns - **printf** - Output to stdout. ``` -------------------------------- ### decContextGetStatus(context) Source: https://speleotrove.com/decimal/dncont.html Returns the current status field from a decContext structure. ```APIDOC ## decContextGetStatus(context) ### Description Returns the current status field from a decContext structure. ### Parameters - **context** (`decContext *`) - Required - Pointer to the structure whose status is to be returned. ### Returns - **status** (`uint32_t`) - The current status field. ``` -------------------------------- ### decDoubleAdd(r, x, y, set) Source: https://speleotrove.com/decimal/dnfloat.html Adds two decDouble numbers and stores the result in r. The result is canonical. ```APIDOC ## decDoubleAdd(r, x, y, set) ### Description Adds the decDouble numbers x and y and stores the result in r. The result is canonical. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **x** (`const decDouble *`) - First input argument. - **y** (`const decDouble *`) - Second input argument. - **set** (`decContext *`) - Context for rounding and status. ### Return Value `decDouble *` - The result r. ``` -------------------------------- ### decDoubleMin Source: https://speleotrove.com/decimal/dnfloat.html Returns the minimum of two decDouble values. ```APIDOC ## decDoubleMin ### Description Returns the minimum of two decDouble values. ### Function Signature `decDouble *decDoubleMin(decDouble *r, const decDouble *x, const decDouble *y, decContext *set)` ### Parameters - **r** (decDouble *) - Pointer to the result decDouble. - **x** (const decDouble *) - First operand. - **y** (const decDouble *) - Second operand. - **set** (decContext *) - Context for status flags. ### Behavior - If exactly one operand is a quiet NaN, the other operand is returned. - Otherwise NaNs are handled as for arithmetic operations. ``` -------------------------------- ### decDoubleCompareTotal(r, x, y) Source: https://speleotrove.com/decimal/dnfloat.html Compares two decDouble numbers using total ordering and stores the result in r. ```APIDOC ## decDoubleCompareTotal(r, x, y) ### Description Compares the decDouble numbers x and y using total ordering and stores the result in r. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **x** (`const decDouble *`) - First input argument. - **y** (`const decDouble *`) - Second input argument. ### Return Value `decDouble *` - The result r. ``` -------------------------------- ### decDoubleIsFinite Source: https://speleotrove.com/decimal/dnfloat.html Tests whether a decDouble is finite. ```APIDOC ## decDoubleIsFinite ### Description Tests whether a decDouble is finite. ### Function Signature `uint32_t decDoubleIsFinite(const decDouble *x)` ### Parameters - **x** (const decDouble *) - Pointer to the decDouble value. ### Return Value - **uint32_t** - Non-zero if finite, zero otherwise. ``` -------------------------------- ### decDoubleCopy(r, x) Source: https://speleotrove.com/decimal/dnfloat.html Copies a decDouble number from x to r. ```APIDOC ## decDoubleCopy(r, x) ### Description Copies the decDouble number x to r. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **x** (`const decDouble *`) - Input argument. ### Return Value `decDouble *` - The result r. ``` -------------------------------- ### decimal64FromNumber(decimal64, number, context) Source: https://speleotrove.com/decimal/dncomps.html Converts a decNumber value to a decimal64 value. The decimal64 structure may receive a numeric value (including subnormal values and –0) or a special value. ```APIDOC ## decimal64FromNumber(decimal64, number, context) ### Description Converts a decNumber value to a decimal64 value. The decimal64 structure may receive a numeric value (including subnormal values and –0) or a special value. ### Method Function ### Signature `decimal64 *decimal64FromNumber(decimal64 *decimal64, decNumber *number, decContext *context)` ### Parameters - **decimal64** (`decimal64 *`) - Pointer to the structure to be set from the decNumber. This may receive a numeric value (including subnormal values and –0) or a special value. - **number** (`decNumber *`) - Pointer to the input structure. The decNumber structure will not be altered. - **context** (`decContext *`) - Pointer to a context structure whose _status_ field is used to report any error and whose other fields are used to control rounding, _etc._, as required. ### Returns Returns _decimal64_. ### Errors The possible errors are as for the decimal64FromString function, except that `DEC_Conversion_syntax` is not possible. ``` -------------------------------- ### decDoubleCopyAbs(r, x) Source: https://speleotrove.com/decimal/dnfloat.html Copies the absolute value of a decDouble number from x to r. ```APIDOC ## decDoubleCopyAbs(r, x) ### Description Copies the absolute value of the decDouble number x to r. ### Parameters - **r** (`decDouble *`) - Result argument, also the return value. - **x** (`const decDouble *`) - Input argument. ### Return Value `decDouble *` - The result r. ```