MySQL  8.0.20
Source Code Documentation
decimal.h
Go to the documentation of this file.
1 /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef DECIMAL_INCLUDED
24 #define DECIMAL_INCLUDED
25 
26 #ifndef MYSQL_ABI_CHECK
27 #include <stdlib.h>
28 #endif
29 
30 #include "my_inttypes.h"
31 #include "my_macros.h"
32 
33 typedef enum {
34  TRUNCATE = 0,
41 
42 /**
43  intg is the number of *decimal* digits (NOT number of decimal_digit_t's !)
44  before the point
45  frac is the number of decimal digits after the point
46  len is the length of buf (length of allocated space) in decimal_digit_t's,
47  not in bytes
48  sign false means positive, true means negative
49  buf is an array of decimal_digit_t's
50  */
51 struct decimal_t {
52  int intg, frac, len;
53  bool sign;
55 };
56 
57 #ifndef MYSQL_ABI_CHECK
58 void widen_fraction(int new_frac, decimal_t *d);
59 int string2decimal(const char *from, decimal_t *to, const char **end);
60 int decimal2string(const decimal_t *from, char *to, int *to_len,
61  int fixed_precision, int fixed_decimals);
62 inline int decimal2string(const decimal_t *from, char *to, int *to_len) {
63  return decimal2string(from, to, to_len, 0, 0);
64 }
65 int decimal2ulonglong(const decimal_t *from, ulonglong *to);
67 int decimal2longlong(const decimal_t *from, longlong *to);
68 int longlong2decimal(longlong from, decimal_t *to);
69 int decimal2double(const decimal_t *from, double *to);
70 int double2decimal(double from, decimal_t *to);
71 int decimal_actual_fraction(const decimal_t *from);
72 int decimal2bin(const decimal_t *from, uchar *to, int precision, int scale);
73 int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale,
74  bool keep_prec = false);
75 
76 /**
77  Convert decimal to lldiv_t.
78  The integer part is stored in to->quot.
79  The fractional part is multiplied to 10^9 and stored to to->rem.
80  @param from Decimal value
81  @param [out] to lldiv_t value
82  @retval 0 on success
83  @retval !0 in error
84 */
85 int decimal2lldiv_t(const decimal_t *from, lldiv_t *to);
86 
87 /**
88  Convert doube to lldiv_t.
89  The integer part is stored in to->quot.
90  The fractional part is multiplied to 10^9 and stored to to->rem.
91  @param nr Decimal value
92  @param [out] lld lldiv_t value
93  @retval 0 on success
94  @retval !0 in error
95 */
96 
97 int double2lldiv_t(double nr, lldiv_t *lld);
98 int decimal_size(int precision, int scale);
99 int decimal_bin_size(int precision, int scale);
100 
101 int decimal_intg(const decimal_t *from);
102 int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
103 int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
104 int decimal_cmp(const decimal_t *from1, const decimal_t *from2);
105 int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
106 int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to,
107  int scale_incr);
108 int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
109 int decimal_round(const decimal_t *from, decimal_t *to, int new_scale,
110  decimal_round_mode mode);
111 int decimal_is_zero(const decimal_t *from);
112 void max_decimal(int precision, int frac, decimal_t *to);
113 int decimal_shift(decimal_t *dec, int shift);
114 
115 /* set a decimal_t to zero */
116 static inline void decimal_make_zero(decimal_t *dec) {
117  dec->buf[0] = 0;
118  dec->intg = 1;
119  dec->frac = 0;
120  dec->sign = false;
121 }
122 
123 /**
124  Returns the length of the buffer to hold string representation
125  of the decimal (including decimal dot, possible sign and \0)
126 */
127 static inline int decimal_string_size(const decimal_t *dec) {
128  return (dec->intg ? dec->intg : 1) + dec->frac + (dec->frac > 0) + 2;
129 }
130 
131 /*
132  conventions:
133 
134  decimal_smth() == 0 -- everything's ok
135  decimal_smth() <= 1 -- result is usable, but precision loss is
136  possible decimal_smth() <= 2 -- result can be unusable, most significant
137  digits could've been lost decimal_smth() > 2 -- no result was generated
138 */
139 
140 #define E_DEC_OK 0
141 #define E_DEC_TRUNCATED 1
142 #define E_DEC_OVERFLOW 2
143 #define E_DEC_DIV_ZERO 4
144 #define E_DEC_BAD_NUM 8
145 #define E_DEC_OOM 16
146 
147 #define E_DEC_ERROR 31
148 #define E_DEC_FATAL_ERROR 30
149 
150 static constexpr int DECIMAL_MAX_SCALE{30};
151 static constexpr int DECIMAL_NOT_SPECIFIED{DECIMAL_MAX_SCALE + 1};
152 
153 #endif // MYSQL_ABI_CHECK
154 
155 #endif // DECIMAL_INCLUDED
Some common macros.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
unsigned char uchar
Definition: my_inttypes.h:51
int intg
Definition: decimal.h:52
int decimal_shift(decimal_t *dec, int shift)
Definition: decimal.cc:730
int double2decimal(double from, decimal_t *to)
Definition: decimal.cc:1071
decimal_digit_t * buf
Definition: decimal.h:54
decimal_round_mode
Definition: decimal.h:33
int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale, bool keep_prec=false)
Definition: decimal.cc:1476
int decimal2bin(const decimal_t *from, uchar *to, int precision, int scale)
Definition: decimal.cc:1341
void widen_fraction(int new_frac, decimal_t *d)
Add zeros behind comma to increase precision of decimal.
Definition: decimal.cc:1015
Some integer typedefs for easier portability.
int decimal2string(const decimal_t *from, char *to, int *to_len, int fixed_precision, int fixed_decimals)
Definition: decimal.cc:496
void max_decimal(int precision, int frac, decimal_t *to)
Definition: decimal.cc:402
int decimal_cmp(const decimal_t *from1, const decimal_t *from2)
Definition: decimal.cc:2030
int decimal2longlong(const decimal_t *from, longlong *to)
Definition: decimal.cc:1148
int frac
Definition: decimal.h:52
Type dec(Shards< COUNT > &shards, size_t id)
Decrement the counter of a shard by 1.
Definition: ut0counter.h:287
Definition: decimal.h:35
int decimal2lldiv_t(const decimal_t *from, lldiv_t *to)
Convert decimal to lldiv_t.
Definition: decimal.cc:1201
Definition: decimal.h:38
int32 decimal_digit_t
Definition: decimal.h:40
int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
Definition: decimal.cc:2069
int longlong2decimal(longlong from, decimal_t *to)
Definition: decimal.cc:1117
int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
Definition: decimal.cc:2496
int decimal_actual_fraction(const decimal_t *from)
Definition: decimal.cc:456
int decimal2double(const decimal_t *from, double *to)
Definition: decimal.cc:1042
int decimal_bin_size(int precision, int scale)
Definition: decimal.cc:1622
static constexpr int DECIMAL_NOT_SPECIFIED
Definition: decimal.h:151
int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to, int scale_incr)
Definition: decimal.cc:2464
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:188
int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
Definition: decimal.cc:2020
int decimal_round(const decimal_t *from, decimal_t *to, int new_scale, decimal_round_mode mode)
Definition: decimal.cc:1644
long long int longlong
Definition: my_inttypes.h:54
Definition: decimal.h:36
static void decimal_make_zero(decimal_t *dec)
Definition: decimal.h:116
int decimal_is_zero(const decimal_t *from)
Definition: decimal.cc:2040
int double2lldiv_t(double nr, lldiv_t *lld)
Convert doube to lldiv_t.
Definition: decimal.cc:1234
int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
Definition: decimal.cc:2025
int string2decimal(const char *from, decimal_t *to, const char **end)
Definition: decimal.cc:902
int32_t int32
Definition: my_inttypes.h:65
int decimal2ulonglong(const decimal_t *from, ulonglong *to)
Definition: decimal.cc:1124
static constexpr int DECIMAL_MAX_SCALE
Definition: decimal.h:150
Definition: decimal.h:34
int ulonglong2decimal(ulonglong from, decimal_t *to)
Definition: decimal.cc:1112
int decimal_size(int precision, int scale)
Definition: decimal.cc:1597
bool sign
Definition: decimal.h:53
intg is the number of decimal digits (NOT number of decimal_digit_t&#39;s !) before the point frac is the...
Definition: decimal.h:51
int len
Definition: decimal.h:52
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...
Definition: decimal.h:127
int decimal_intg(const decimal_t *from)
Definition: decimal.cc:2014
Definition: decimal.h:37