MySQL 8.0.39
Source Code Documentation
decimal.h File Reference
#include <stdlib.h>
#include "my_inttypes.h"
#include "my_macros.h"

Go to the source code of this file.

Classes

struct  decimal_t
 intg is the number of decimal digits (NOT number of decimal_digit_t's !) before the point frac is the number of decimal digits after the point len is the length of buf (length of allocated space) in decimal_digit_t's, not in bytes sign false means positive, true means negative buf is an array of decimal_digit_t's More...
 

Macros

#define E_DEC_OK   0
 
#define E_DEC_TRUNCATED   1
 
#define E_DEC_OVERFLOW   2
 
#define E_DEC_DIV_ZERO   4
 
#define E_DEC_BAD_NUM   8
 
#define E_DEC_OOM   16
 
#define E_DEC_FATAL_ERROR    (E_DEC_OVERFLOW | E_DEC_DIV_ZERO | E_DEC_BAD_NUM | E_DEC_OOM)
 
#define E_DEC_ERROR   (E_DEC_FATAL_ERROR | E_DEC_TRUNCATED)
 

Typedefs

typedef int32 decimal_digit_t
 

Enumerations

enum  decimal_round_mode {
  TRUNCATE = 0 , HALF_EVEN , HALF_UP , CEILING ,
  FLOOR
}
 

Functions

void widen_fraction (int new_frac, decimal_t *d)
 Add zeros behind comma to increase precision of decimal. More...
 
int string2decimal (const char *from, decimal_t *to, const char **end)
 
int decimal2string (const decimal_t *from, char *to, int *to_len, int fixed_precision, int fixed_decimals)
 
int decimal2string (const decimal_t *from, char *to, int *to_len)
 
int decimal2ulonglong (const decimal_t *from, ulonglong *to)
 
int ulonglong2decimal (ulonglong from, decimal_t *to)
 
int decimal2longlong (const decimal_t *from, longlong *to)
 
int longlong2decimal (longlong from, decimal_t *to)
 
int decimal2double (const decimal_t *from, double *to)
 
int double2decimal (double from, decimal_t *to)
 
int decimal_actual_fraction (const decimal_t *from)
 
int decimal2bin (const decimal_t *from, uchar *to, int precision, int scale)
 
int bin2decimal (const uchar *from, decimal_t *to, int precision, int scale, bool keep_prec=false)
 
int decimal2lldiv_t (const decimal_t *from, lldiv_t *to)
 Convert decimal to lldiv_t. More...
 
int double2lldiv_t (double nr, lldiv_t *lld)
 Convert double value to lldiv_t value. More...
 
int decimal_size (int precision, int scale)
 
int decimal_bin_size (int precision, int scale)
 
int decimal_intg (const decimal_t *from)
 Returns the number of decimal digits before the decimal point in a decimal_t, with any insignificant leading zeros removed. More...
 
int decimal_add (const decimal_t *from1, const decimal_t *from2, decimal_t *to)
 
int decimal_sub (const decimal_t *from1, const decimal_t *from2, decimal_t *to)
 
int decimal_cmp (const decimal_t *from1, const decimal_t *from2)
 
int decimal_mul (const decimal_t *from1, const decimal_t *from2, decimal_t *to)
 
int decimal_div (const decimal_t *from1, const decimal_t *from2, decimal_t *to, int scale_incr)
 
int decimal_mod (const decimal_t *from1, const decimal_t *from2, decimal_t *to)
 
int decimal_round (const decimal_t *from, decimal_t *to, int new_scale, decimal_round_mode mode)
 
int decimal_is_zero (const decimal_t *from)
 
void max_decimal (int precision, int frac, decimal_t *to)
 
int decimal_shift (decimal_t *dec, int shift)
 
static void decimal_make_zero (decimal_t *dec)
 
static int decimal_string_size (const decimal_t *dec)
 Returns the length of the buffer to hold string representation of the decimal (including decimal dot, possible sign and \0) More...
 

Variables

static constexpr int DECIMAL_MAX_SCALE {30}
 
static constexpr int DECIMAL_NOT_SPECIFIED {DECIMAL_MAX_SCALE + 1}
 

Macro Definition Documentation

◆ E_DEC_BAD_NUM

#define E_DEC_BAD_NUM   8

◆ E_DEC_DIV_ZERO

#define E_DEC_DIV_ZERO   4

◆ E_DEC_ERROR

#define E_DEC_ERROR   (E_DEC_FATAL_ERROR | E_DEC_TRUNCATED)

◆ E_DEC_FATAL_ERROR

#define E_DEC_FATAL_ERROR    (E_DEC_OVERFLOW | E_DEC_DIV_ZERO | E_DEC_BAD_NUM | E_DEC_OOM)

◆ E_DEC_OK

#define E_DEC_OK   0

◆ E_DEC_OOM

#define E_DEC_OOM   16

◆ E_DEC_OVERFLOW

#define E_DEC_OVERFLOW   2

◆ E_DEC_TRUNCATED

#define E_DEC_TRUNCATED   1

Typedef Documentation

◆ decimal_digit_t

Enumeration Type Documentation

◆ decimal_round_mode

Enumerator
TRUNCATE 
HALF_EVEN 
HALF_UP 
CEILING 
FLOOR 

Function Documentation

◆ bin2decimal()

int bin2decimal ( const uchar from,
decimal_t to,
int  precision,
int  scale,
bool  keep_prec = false 
)

◆ decimal2bin()

int decimal2bin ( const decimal_t from,
uchar to,
int  precision,
int  scale 
)

◆ decimal2double()

int decimal2double ( const decimal_t from,
double *  to 
)

◆ decimal2lldiv_t()

int decimal2lldiv_t ( const decimal_t from,
lldiv_t *  to 
)

Convert decimal to lldiv_t.

The integer part is stored in to->quot. The fractional part is multiplied to 10^9 and stored to to->rem.

Parameters
fromDecimal value
[out]tolldiv_t value
Return values
0on success
!0in error

◆ decimal2longlong()

int decimal2longlong ( const decimal_t from,
longlong to 
)

◆ decimal2string() [1/2]

int decimal2string ( const decimal_t from,
char *  to,
int *  to_len 
)
inline

◆ decimal2string() [2/2]

int decimal2string ( const decimal_t from,
char *  to,
int *  to_len,
int  fixed_precision,
int  fixed_decimals 
)

◆ decimal2ulonglong()

int decimal2ulonglong ( const decimal_t from,
ulonglong to 
)

◆ decimal_actual_fraction()

int decimal_actual_fraction ( const decimal_t from)

◆ decimal_add()

int decimal_add ( const decimal_t from1,
const decimal_t from2,
decimal_t to 
)

◆ decimal_bin_size()

int decimal_bin_size ( int  precision,
int  scale 
)

◆ decimal_cmp()

int decimal_cmp ( const decimal_t from1,
const decimal_t from2 
)

◆ decimal_div()

int decimal_div ( const decimal_t from1,
const decimal_t from2,
decimal_t to,
int  scale_incr 
)

◆ decimal_intg()

int decimal_intg ( const decimal_t from)

Returns the number of decimal digits before the decimal point in a decimal_t, with any insignificant leading zeros removed.

◆ decimal_is_zero()

int decimal_is_zero ( const decimal_t from)

◆ decimal_make_zero()

static void decimal_make_zero ( decimal_t dec)
inlinestatic

◆ decimal_mod()

int decimal_mod ( const decimal_t from1,
const decimal_t from2,
decimal_t to 
)

◆ decimal_mul()

int decimal_mul ( const decimal_t from1,
const decimal_t from2,
decimal_t to 
)

◆ decimal_round()

int decimal_round ( const decimal_t from,
decimal_t to,
int  new_scale,
decimal_round_mode  mode 
)

◆ decimal_shift()

int decimal_shift ( decimal_t dec,
int  shift 
)

◆ decimal_size()

int decimal_size ( int  precision,
int  scale 
)

◆ decimal_string_size()

static int decimal_string_size ( const decimal_t dec)
inlinestatic

Returns the length of the buffer to hold string representation of the decimal (including decimal dot, possible sign and \0)

◆ decimal_sub()

int decimal_sub ( const decimal_t from1,
const decimal_t from2,
decimal_t to 
)

◆ double2decimal()

int double2decimal ( double  from,
decimal_t to 
)

◆ double2lldiv_t()

int double2lldiv_t ( double  nr,
lldiv_t *  lld 
)

Convert double value to lldiv_t value.

Parameters
nrThe double value to convert from.
[out]lldThe lldit_t variable to convert to.
Returns
0 on success, error code on error.

Integer part goes into lld.quot. Fractional part multiplied to 1000000000 (10^9) goes to lld.rem. Typically used in datetime calculations to split seconds and nanoseconds.

◆ longlong2decimal()

int longlong2decimal ( longlong  from,
decimal_t to 
)

◆ max_decimal()

void max_decimal ( int  precision,
int  frac,
decimal_t to 
)

◆ string2decimal()

int string2decimal ( const char *  from,
decimal_t to,
const char **  end 
)

◆ ulonglong2decimal()

int ulonglong2decimal ( ulonglong  from,
decimal_t to 
)

◆ widen_fraction()

void widen_fraction ( int  new_frac,
decimal_t d 
)

Add zeros behind comma to increase precision of decimal.

Parameters
new_fracthe new fraction
[in,out]dthe decimal target

new_frac is expected to >= than cd->frac and new fraction is expected to fit in d.

Variable Documentation

◆ DECIMAL_MAX_SCALE

constexpr int DECIMAL_MAX_SCALE {30}
staticconstexpr

◆ DECIMAL_NOT_SPECIFIED

constexpr int DECIMAL_NOT_SPECIFIED {DECIMAL_MAX_SCALE + 1}
staticconstexpr