Mstdlib-1.24.0
Decimal

Data Structures

struct  M_decimal_t
 

Enumerations

enum  M_DECIMAL_RETVAL {
  M_DECIMAL_SUCCESS = 0 ,
  M_DECIMAL_OVERFLOW = 1 ,
  M_DECIMAL_TRUNCATION = 2 ,
  M_DECIMAL_INVALID = 3
}
 
enum  M_decimal_round_t {
  M_DECIMAL_ROUND_NONE = 0 ,
  M_DECIMAL_ROUND_TRADITIONAL = 1 ,
  M_DECIMAL_ROUND_BANKERS = 2
}
 

Functions

void M_decimal_create (M_decimal_t *dec)
 
void M_decimal_from_int (M_decimal_t *dec, M_int64 integer, M_uint8 implied_dec)
 
M_int64 M_decimal_to_int (const M_decimal_t *dec, M_uint8 implied_dec)
 
enum M_DECIMAL_RETVAL M_decimal_from_str (const char *string, size_t len, M_decimal_t *val, const char **endptr)
 
enum M_DECIMAL_RETVAL M_decimal_to_str (const M_decimal_t *dec, char *buf, size_t buf_len)
 
M_int8 M_decimal_cmp (const M_decimal_t *dec1, const M_decimal_t *dec2)
 
enum M_DECIMAL_RETVAL M_decimal_transform (M_decimal_t *dec, M_uint8 num_dec, M_decimal_round_t round)
 
void M_decimal_reduce (M_decimal_t *dec)
 
M_uint8 M_decimal_num_decimals (const M_decimal_t *dec)
 
void M_decimal_duplicate (M_decimal_t *dest, const M_decimal_t *src)
 
enum M_DECIMAL_RETVAL M_decimal_multiply (M_decimal_t *dest, const M_decimal_t *dec1, const M_decimal_t *dec2)
 
enum M_DECIMAL_RETVAL M_decimal_divide (M_decimal_t *dest, const M_decimal_t *dec1, const M_decimal_t *dec2, M_decimal_round_t round)
 
enum M_DECIMAL_RETVAL M_decimal_subtract (M_decimal_t *dest, const M_decimal_t *dec1, const M_decimal_t *dec2)
 
enum M_DECIMAL_RETVAL M_decimal_add (M_decimal_t *dest, const M_decimal_t *dec1, const M_decimal_t *dec2)
 

Detailed Description

Floating point number type. Used instead of double or float to elimiate rounding errors.

Example:

const char *s1 = "1.01";
const char *s2 = "0.001";
char out[16];
M_mem_set(out, 0, sizeof(out));
M_decimal_from_str(s1, M_str_len(s1), &d1, NULL);
M_decimal_from_str(s2, M_str_len(s2), &d2, NULL);
M_decimal_add(&d1, &d1, &d2);
if (M_decimal_to_str(&d1, out, sizeof(out)) != M_DECIMAL_SUCCESS) {
M_printf("failure\n");
} else {
M_printf("out='%s'\n", out);
}
enum M_DECIMAL_RETVAL M_decimal_add(M_decimal_t *dest, const M_decimal_t *dec1, const M_decimal_t *dec2)
enum M_DECIMAL_RETVAL M_decimal_from_str(const char *string, size_t len, M_decimal_t *val, const char **endptr)
enum M_DECIMAL_RETVAL M_decimal_to_str(const M_decimal_t *dec, char *buf, size_t buf_len)
void M_decimal_reduce(M_decimal_t *dec)
@ M_DECIMAL_SUCCESS
Definition: m_decimal.h:86
Definition: m_decimal.h:78
ssize_t M_printf(const char *fmt,...)
void * M_mem_set(void *s, int c, size_t n)
size_t M_str_len(const char *s) M_WARN_UNUSED_RESULT

Example output:

out='1.011'

Data Structure Documentation

◆ M_decimal_t

struct M_decimal_t

Structure defining storage for decimal numbers.

This structure should never be touched directly. It is only made public to reduce the overhead of using this datatype (no malloc needed).

Data Fields
M_int64 num

Number represented.

M_uint8 num_dec

How many implied decimal places.

Enumeration Type Documentation

◆ M_DECIMAL_RETVAL

Result/Error codes for M_decimal functions.

Enumerator
M_DECIMAL_SUCCESS 

Operation successful.

M_DECIMAL_OVERFLOW 

An overflow occurred in the operation.

M_DECIMAL_TRUNCATION 

The result was truncated/rounded in order to approximate the best result. This is true on most divide operations.

M_DECIMAL_INVALID 

Invalid data.

◆ M_decimal_round_t

Rounding formula

Enumerator
M_DECIMAL_ROUND_NONE 

Truncate

M_DECIMAL_ROUND_TRADITIONAL 

Traditional, aka Round Half away from Zero.

M_DECIMAL_ROUND_BANKERS 

Bankers, aka Round Half to Even

Function Documentation

◆ M_decimal_create()

void M_decimal_create ( M_decimal_t dec)

Create new zero'd out decimal number.

Parameters
[out]decNew decimal output.

◆ M_decimal_from_int()

void M_decimal_from_int ( M_decimal_t dec,
M_int64  integer,
M_uint8  implied_dec 
)

Convert to a decimal representation from an integer

Parameters
[out]decNew decimal output.
[in]integerInteger to convert to decimal.
[in]implied_decNumber of implied decimals in integer input.

◆ M_decimal_to_int()

M_int64 M_decimal_to_int ( const M_decimal_t dec,
M_uint8  implied_dec 
)

Convert from a decimal representation to an integer with implied decimal places. If the conversion causes truncation, the number will be rounded. For example a decimal of 123.456, with implied decimals 2, will return 12346.

Parameters
[in]decDecimal type
[in]implied_decNumber of implied decimal positions.
Returns
Rounded integer representation of number.

◆ M_decimal_from_str()

enum M_DECIMAL_RETVAL M_decimal_from_str ( const char *  string,
size_t  len,
M_decimal_t val,
const char **  endptr 
)

Convert to a decimal representation from a string.

Parameters
[in]stringBuffer with decimal representation.
[in]lenLength of bytes to evaluate from string.
[out]valNew decimal output.
[out]endptrPointer to end of evaluated decimal.
Returns
One of the enum M_DECIMAL_RETVAL values.

◆ M_decimal_to_str()

enum M_DECIMAL_RETVAL M_decimal_to_str ( const M_decimal_t dec,
char *  buf,
size_t  buf_len 
)

Convert from a decimal representation to a string.

Parameters
[in]decDecimal type.
[out]bufBuffer to output string representation.
[in]buf_lenLength of output buffer.
Returns
One of the enum M_DECIMAL_RETVAL values.

◆ M_decimal_cmp()

M_int8 M_decimal_cmp ( const M_decimal_t dec1,
const M_decimal_t dec2 
)

Compare 2 decimals.

Parameters
[in]dec1Decimal 1.
[in]dec2Decimal 2.
Returns
-1 if dec1 < dec2, 0 if dec1 == dec2, 1 if dec1 > dec2.

◆ M_decimal_transform()

enum M_DECIMAL_RETVAL M_decimal_transform ( M_decimal_t dec,
M_uint8  num_dec,
M_decimal_round_t  round 
)

Transform decimal number representation to have the specified number of decimal places (rounding if needed).

Parameters
[in,out]decDecimal type.
[in]num_decNumber of decimal places number should be transformed to.
[in]roundRound method to use when rounding.
Returns
One of the enum M_DECIMAL_RETVAL values.

◆ M_decimal_reduce()

void M_decimal_reduce ( M_decimal_t dec)

Reduce the decimal representation to the smallest number of decimal places possible without reducing precision (remove trailing zeros).

Parameters
[in,out]decDecimal type.

◆ M_decimal_num_decimals()

M_uint8 M_decimal_num_decimals ( const M_decimal_t dec)

Number of decimal places present in the M_decimal_t representation.

Parameters
[in]decDecimal type.
Returns
Number of decimals currently represented by type.

◆ M_decimal_duplicate()

void M_decimal_duplicate ( M_decimal_t dest,
const M_decimal_t src 
)

Copy the decimal object from the source into the destination

Parameters
[out]destNew decimal duplicated from src.
[in]srcDecimal type to copy.

◆ M_decimal_multiply()

enum M_DECIMAL_RETVAL M_decimal_multiply ( M_decimal_t dest,
const M_decimal_t dec1,
const M_decimal_t dec2 
)

Multiply the two decimals together putting the result in dest.

The destination and one of the sources may be the same. The number of resulting decimal places will be the same as the largest input.

Parameters
[out]destNew decimal with result.
[in]dec1First decimal to multiply.
[in]dec2Second decimal to multiply.
Returns
One of the enum M_DECIMAL_RETVAL values.

◆ M_decimal_divide()

enum M_DECIMAL_RETVAL M_decimal_divide ( M_decimal_t dest,
const M_decimal_t dec1,
const M_decimal_t dec2,
M_decimal_round_t  round 
)

Divide the two decimals, putting the result in dest.

The destination and one of the sources may be the same. The maximum number of decimal places able to be represented will be.

Parameters
[out]destNew decimal with result.
[in]dec1First decimal (numerator).
[in]dec2Second decimal (denominator).
[in]roundThe result may not be able to be represented fully, select the rounding method if truncated.
Returns
One of the enum M_DECIMAL_RETVAL values.

◆ M_decimal_subtract()

enum M_DECIMAL_RETVAL M_decimal_subtract ( M_decimal_t dest,
const M_decimal_t dec1,
const M_decimal_t dec2 
)

Subtract two decimals, putting the result in dest.

The destination and one of the sources may be the same. The number of resulting decimal places will be the same as the largest input.

Parameters
[out]destNew decimal with result.
[in]dec1First decimal.
[in]dec2Second decimal.
Returns
One of the enum M_DECIMAL_RETVAL values.

◆ M_decimal_add()

enum M_DECIMAL_RETVAL M_decimal_add ( M_decimal_t dest,
const M_decimal_t dec1,
const M_decimal_t dec2 
)

Add two decimals, putting the result in dest.

The destination and one of the sources may be the same. The number of resulting decimal places will be the same as the largest input.

Parameters
[out]destNew decimal with result.
[in]dec1First decimal.
[in]dec2Second decimal.
Returns
One of the enum M_DECIMAL_RETVAL values