Decimals — mpdecimal 2.5.1 documentation
Description: libmpdec documentation

# Decimals

## Data Type

The decimal data type is called `mpd_t`, which is short for multi precision decimal.

    #include <mpdecimal.h>

    typedef struct mpd_t {
           uint8_t flags;       // [memory flags] | [specials] | sign
           mpd_ssize_t exp;     // exponent
           mpd_ssize_t digits;  // number of decimal digits
           mpd_ssize_t len;     // number of words in use
           mpd_ssize_t alloc;   // number of allocated words
           mpd_uint_t *data;    // words
    } mpd_t;

The flags contain two types of information: The lowest bit is the sign of the decimal. The following three bits determine whether the decimal is Infinity, NaN or sNaN. The upper four bits carry the information how the storage was allocated. The normal case is that both the struct and the data have been dynamically allocated by `mpd_new`. More on this in the section Advanced Memory Handling.

## Allocation Functions

    mpd_t *mpd_qnew(void);
    mpd_t *mpd_new(mpd_context_t *ctx);

`mpd_qnew` attempts to allocate a dynamic decimal with `MPD_MINALLOC` words reserved for the coefficient. If successful, the return value is the new pointer, `NULL` otherwise. `mpd_new` is the same, but raises the `MPD_Malloc_error` exception on failure.

    mpd_t *mpd_qnew_size(mpd_ssize_t size);

Same as `mpd_qnew`, but the size argument indicates that size words should be allocated for the coefficient rather than `MPD_MINALLOC` words.

    void mpd_del(mpd_t *dec);

`mpd_del` frees all storage allocated for a decimal. The function may also be called with a completely or partly static decimal as an argument, see Advanced Memory Handling.