MySQL 8.0.39
Source Code Documentation
|
#include "decimal.h"
#include <limits.h>
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <algorithm>
#include <type_traits>
#include <utility>
#include "integer_digits.h"
#include "m_ctype.h"
#include "m_string.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_sys.h"
#include "myisampack.h"
Namespaces | |
namespace | anonymous_namespace{decimal.cc} |
Macros | |
#define | DIG_PER_DEC1 9 |
#define | DIG_MASK 100000000 |
#define | DIG_BASE 1000000000 |
#define | DIG_MAX (DIG_BASE - 1) |
#define | ROUND_UP(X) (((X) + DIG_PER_DEC1 - 1) / DIG_PER_DEC1) |
#define | sanity(d) assert((d)->len > 0) |
#define | ADD(to, from1, from2, carry) |
#define | ADD2(to, from1, from2, carry) |
#define | SUB(to, from1, from2, carry) |
#define | SUB2(to, from1, from2, carry) |
#define | LLDIV_MIN -1000000000000000000LL |
#define | LLDIV_MAX 1000000000000000000LL |
Typedefs | |
using | dec1 = decimal_digit_t |
Internally decimal numbers are stored base 10^9 (see DIG_BASE below) So one variable of type decimal_digit_t is limited: More... | |
using | dec2 = int64_t |
A wider variant of dec1, to avoid overflow in intermediate results. More... | |
using | udec1 = std::make_unsigned< dec1 >::type |
An unsigned type with the same width as dec1. More... | |
Functions | |
static dec1 | div_by_pow10 (dec1 x, int p) |
static dec1 | mod_by_pow10 (dec1 x, int p) |
void | anonymous_namespace{decimal.cc}::fix_intg_frac_error (const int &len, int *intg1, int *frac1, int *error) |
Verifies input arguments len, intg1 and frac1, and sets error output argument to indicate over/under-flow or OK. More... | |
static ALWAYS_INLINE int | decimal_bin_size_inline (int precision, int scale) |
static int | count_leading_zeroes (int i, dec1 val) |
static int | count_trailing_zeroes (int i, dec1 val) |
void | max_decimal (int precision, int frac, decimal_t *to) |
static dec1 * | remove_leading_zeroes (const decimal_t *from, int *intg_result) |
int | decimal_actual_fraction (const decimal_t *from) |
int | decimal2string (const decimal_t *from, char *to, int *to_len, int fixed_precision, int fixed_decimals) |
static void | digits_bounds (const decimal_t *from, int *start_result, int *end_result) |
static void | do_mini_left_shift (decimal_t *dec, int shift, int beg, int last) |
static void | do_mini_right_shift (decimal_t *dec, int shift, int beg, int last) |
int | decimal_shift (decimal_t *dec, int shift) |
int | string2decimal (const char *from, decimal_t *to, const char **end) |
void | widen_fraction (int new_frac, decimal_t *d) |
Add zeros behind comma to increase precision of decimal. More... | |
int | decimal2double (const decimal_t *from, double *to) |
int | double2decimal (double from, decimal_t *to) |
static int | ull2dec (ulonglong from, decimal_t *to) |
int | ulonglong2decimal (ulonglong from, decimal_t *to) |
int | longlong2decimal (longlong from, decimal_t *to) |
int | decimal2ulonglong (const decimal_t *from, ulonglong *to) |
int | decimal2longlong (const decimal_t *from, longlong *to) |
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 | decimal2bin (const decimal_t *from, uchar *to, int precision, int frac) |
int | bin2decimal (const uchar *from, decimal_t *to, int precision, int scale, bool keep_prec) |
int | decimal_size (int precision, int scale) |
int | decimal_bin_size (int precision, int scale) |
int | decimal_round (const decimal_t *from, decimal_t *to, int scale, decimal_round_mode mode) |
static int | do_add (const decimal_t *from1, const decimal_t *from2, decimal_t *to) |
static int | do_sub (const decimal_t *from1, const decimal_t *from2, decimal_t *to) |
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_is_zero (const decimal_t *from) |
int | decimal_mul (const decimal_t *from_1, const decimal_t *from_2, decimal_t *to) |
static int | do_div_mod (const decimal_t *from1, const decimal_t *from2, decimal_t *to, decimal_t *mod, int scale_incr) |
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) |
Variables | |
static const dec1 | powers10 [DIG_PER_DEC1+1] |
static const int | dig2bytes [DIG_PER_DEC1+1] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 4} |
static const dec1 | frac_max [DIG_PER_DEC1 - 1] |
#define ADD | ( | to, | |
from1, | |||
from2, | |||
carry | |||
) |
#define ADD2 | ( | to, | |
from1, | |||
from2, | |||
carry | |||
) |
#define DIG_BASE 1000000000 |
#define DIG_MASK 100000000 |
#define DIG_MAX (DIG_BASE - 1) |
#define DIG_PER_DEC1 9 |
#define LLDIV_MAX 1000000000000000000LL |
#define LLDIV_MIN -1000000000000000000LL |
#define ROUND_UP | ( | X | ) | (((X) + DIG_PER_DEC1 - 1) / DIG_PER_DEC1) |
#define sanity | ( | d | ) | assert((d)->len > 0) |
#define SUB | ( | to, | |
from1, | |||
from2, | |||
carry | |||
) |
#define SUB2 | ( | to, | |
from1, | |||
from2, | |||
carry | |||
) |
using dec1 = decimal_digit_t |
Internally decimal numbers are stored base 10^9 (see DIG_BASE below) So one variable of type decimal_digit_t is limited:
0 < decimal_digit <= DIG_MAX < DIG_BASE
in the decimal_t:
intg is the number of decimal digits (NOT number of decimal_digit_t's !) before the point frac - number of decimal digits after the point buf is an array of decimal_digit_t's len is the length of buf (length of allocated space) in decimal_digit_t's, not in bytes
using dec2 = int64_t |
A wider variant of dec1, to avoid overflow in intermediate results.
|
inlinestatic |
|
inlinestatic |
int decimal2double | ( | const decimal_t * | from, |
double * | to | ||
) |
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.
from | Decimal value | |
[out] | to | lldiv_t value |
0 | on success |
!0 | in error |
int decimal2string | ( | const decimal_t * | from, |
char * | to, | ||
int * | to_len, | ||
int | fixed_precision, | ||
int | fixed_decimals | ||
) |
int decimal_actual_fraction | ( | const decimal_t * | from | ) |
int decimal_bin_size | ( | int | precision, |
int | scale | ||
) |
|
static |
int decimal_div | ( | const decimal_t * | from1, |
const decimal_t * | from2, | ||
decimal_t * | to, | ||
int | scale_incr | ||
) |
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.
int decimal_is_zero | ( | const decimal_t * | from | ) |
int decimal_round | ( | const decimal_t * | from, |
decimal_t * | to, | ||
int | scale, | ||
decimal_round_mode | mode | ||
) |
int decimal_shift | ( | decimal_t * | dec, |
int | shift | ||
) |
int decimal_size | ( | int | precision, |
int | scale | ||
) |
|
static |
|
static |
|
static |
|
static |
int double2decimal | ( | double | from, |
decimal_t * | to | ||
) |
int double2lldiv_t | ( | double | nr, |
lldiv_t * | lld | ||
) |
Convert double value to lldiv_t value.
nr | The double value to convert from. | |
[out] | lld | The lldit_t variable to convert to. |
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.
void max_decimal | ( | int | precision, |
int | frac, | ||
decimal_t * | to | ||
) |
int string2decimal | ( | const char * | from, |
decimal_t * | to, | ||
const char ** | end | ||
) |
void widen_fraction | ( | int | new_frac, |
decimal_t * | d | ||
) |
Add zeros behind comma to increase precision of decimal.
new_frac | the new fraction | |
[in,out] | d | the decimal target |
new_frac is expected to >= than cd->frac and new fraction is expected to fit in d.
|
static |
|
static |
|
static |