MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
decimal.h
1 /* Copyright (c) 2000, 2011, 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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #ifndef _decimal_h
17 #define _decimal_h
18 
19 typedef enum
20 {TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR}
21  decimal_round_mode;
22 typedef int32 decimal_digit_t;
23 
33 typedef struct st_decimal_t {
34  int intg, frac, len;
35  my_bool sign;
36  decimal_digit_t *buf;
37 } decimal_t;
38 
39 int internal_str2dec(const char *from, decimal_t *to, char **end,
40  my_bool fixed);
41 int decimal2string(const decimal_t *from, char *to, int *to_len,
42  int fixed_precision, int fixed_decimals,
43  char filler);
44 int decimal2ulonglong(decimal_t *from, ulonglong *to);
45 int ulonglong2decimal(ulonglong from, decimal_t *to);
46 int decimal2longlong(decimal_t *from, longlong *to);
47 int longlong2decimal(longlong from, decimal_t *to);
48 int decimal2double(const decimal_t *from, double *to);
49 int double2decimal(double from, decimal_t *to);
50 int decimal_actual_fraction(decimal_t *from);
51 int decimal2bin(decimal_t *from, uchar *to, int precision, int scale);
52 int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale);
53 
63 int decimal2lldiv_t(const decimal_t *from, lldiv_t *to);
64 
75 int double2lldiv_t(double from, lldiv_t *to);
76 int decimal_size(int precision, int scale);
77 int decimal_bin_size(int precision, int scale);
78 int decimal_result_size(decimal_t *from1, decimal_t *from2, char op,
79  int param);
80 
81 int decimal_intg(const decimal_t *from);
82 int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
83 int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
84 int decimal_cmp(const decimal_t *from1, const decimal_t *from2);
85 int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
86 int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to,
87  int scale_incr);
88 int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
89 int decimal_round(const decimal_t *from, decimal_t *to, int new_scale,
90  decimal_round_mode mode);
91 int decimal_is_zero(const decimal_t *from);
92 void max_decimal(int precision, int frac, decimal_t *to);
93 
94 #define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0)
95 #define string2decimal_fixed(A,B,C) internal_str2dec((A), (B), (C), 1)
96 
97 /* set a decimal_t to zero */
98 
99 #define decimal_make_zero(dec) do { \
100  (dec)->buf[0]=0; \
101  (dec)->intg=1; \
102  (dec)->frac=0; \
103  (dec)->sign=0; \
104  } while(0)
105 
106 /*
107  returns the length of the buffer to hold string representation
108  of the decimal (including decimal dot, possible sign and \0)
109 */
110 
111 #define decimal_string_size(dec) (((dec)->intg ? (dec)->intg : 1) + \
112  (dec)->frac + ((dec)->frac > 0) + 2)
113 
114 /* negate a decimal */
115 #define decimal_neg(dec) do { (dec)->sign^=1; } while(0)
116 
117 /*
118  conventions:
119 
120  decimal_smth() == 0 -- everything's ok
121  decimal_smth() <= 1 -- result is usable, but precision loss is possible
122  decimal_smth() <= 2 -- result can be unusable, most significant digits
123  could've been lost
124  decimal_smth() > 2 -- no result was generated
125 */
126 
127 #define E_DEC_OK 0
128 #define E_DEC_TRUNCATED 1
129 #define E_DEC_OVERFLOW 2
130 #define E_DEC_DIV_ZERO 4
131 #define E_DEC_BAD_NUM 8
132 #define E_DEC_OOM 16
133 
134 #define E_DEC_ERROR 31
135 #define E_DEC_FATAL_ERROR 30
136 
137 #endif
138