1
/* Copyright (C) 2005-2006 MySQL AB
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19
It is interface module to fixed precision decimals library.
21
Most functions use 'uint mask' as parameter, if during operation error
22
which fit in this mask is detected then it will be processed automatically
23
here. (errors are E_DEC_* constants, see include/decimal.h)
25
Most function are just inline wrappers around library calls
35
#define DECIMAL_LONGLONG_DIGITS 22
36
#define DECIMAL_LONG_DIGITS 10
37
#define DECIMAL_LONG3_DIGITS 8
39
/** maximum length of buffer in our big digits (uint32). */
40
#define DECIMAL_BUFF_LENGTH 9
42
/* the number of digits that my_decimal can possibly contain */
43
#define DECIMAL_MAX_POSSIBLE_PRECISION (DECIMAL_BUFF_LENGTH * 9)
47
maximum guaranteed precision of number in decimal digits (number of our
48
digits * number of decimal digits in one our big digit - number of decimal
49
digits in one our big digit decreased by 1 (because we always put decimal
50
point on the border of our big digits))
52
#define DECIMAL_MAX_PRECISION (DECIMAL_MAX_POSSIBLE_PRECISION - 8*2)
53
#define DECIMAL_MAX_SCALE 30
54
#define DECIMAL_NOT_SPECIFIED 31
57
maximum length of string representation (number of maximum decimal
58
digits + 1 position for sign + 1 position for decimal point)
60
#define DECIMAL_MAX_STR_LENGTH (DECIMAL_MAX_POSSIBLE_PRECISION + 2)
63
maximum size of packet length.
65
#define DECIMAL_MAX_FIELD_SIZE DECIMAL_MAX_PRECISION
68
inline uint my_decimal_size(uint precision, uint scale)
71
Always allocate more space to allow library to put decimal point
74
return decimal_size(precision, scale) + 1;
78
inline int my_decimal_int_part(uint precision, uint decimals)
80
return precision - ((decimals == DECIMAL_NOT_SPECIFIED) ? 0 : decimals);
85
my_decimal class limits 'decimal_t' type to what we need in MySQL.
87
It contains internally all necessary space needed by the instance so
88
no extra memory is needed. One should call fix_buffer_pointer() function
89
when he moves my_decimal objects in memory.
92
class my_decimal :public decimal_t
94
decimal_digit_t buffer[DECIMAL_BUFF_LENGTH];
100
len= DECIMAL_BUFF_LENGTH;
102
#if !defined (HAVE_purify) && !defined(DBUG_OFF)
103
/* Set buffer to 'random' value to find wrong buffer usage */
104
for (uint i= 0; i < DECIMAL_BUFF_LENGTH; i++)
112
void fix_buffer_pointer() { buf= buffer; }
114
bool sign() const { return decimal_t::sign; }
115
void sign(bool s) { decimal_t::sign= s; }
116
uint precision() const { return intg + frac; }
121
void print_decimal(const my_decimal *dec);
122
void print_decimal_buff(const my_decimal *dec, const uchar* ptr, int length);
123
const char *dbug_decimal_as_string(char *buff, const my_decimal *val);
125
#define dbug_decimal_as_string(A) NULL
129
int decimal_operation_results(int result);
131
inline int decimal_operation_results(int result)
135
#endif /*MYSQL_CLIENT*/
138
void max_my_decimal(my_decimal *to, int precision, int frac)
140
DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION)&&
141
(frac <= DECIMAL_MAX_SCALE));
142
max_decimal(precision, frac, (decimal_t*) to);
145
inline void max_internal_decimal(my_decimal *to)
147
max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
150
inline int check_result(uint mask, int result)
153
decimal_operation_results(result);
157
inline int check_result_and_overflow(uint mask, int result, my_decimal *val)
159
if (check_result(mask, result) & E_DEC_OVERFLOW)
161
bool sign= val->sign();
162
val->fix_buffer_pointer();
163
max_internal_decimal(val);
169
inline uint my_decimal_length_to_precision(uint length, uint scale,
172
return (uint) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
175
inline uint32 my_decimal_precision_to_length(uint precision, uint8 scale,
178
set_if_smaller(precision, DECIMAL_MAX_PRECISION);
179
return (uint32)(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
183
int my_decimal_string_length(const my_decimal *d)
185
return decimal_string_size(d);
190
int my_decimal_max_length(const my_decimal *d)
192
/* -1 because we do not count \0 */
193
return decimal_string_size(d) - 1;
198
int my_decimal_get_binary_size(uint precision, uint scale)
200
return decimal_bin_size((int)precision, (int)scale);
205
void my_decimal2decimal(const my_decimal *from, my_decimal *to)
208
to->fix_buffer_pointer();
212
int my_decimal2binary(uint mask, const my_decimal *d, uchar *bin, int prec,
217
int binary2my_decimal(uint mask, const uchar *bin, my_decimal *d, int prec,
220
return check_result(mask, bin2decimal(bin, (decimal_t*) d, prec, scale));
225
int my_decimal_set_zero(my_decimal *d)
227
decimal_make_zero(((decimal_t*) d));
233
bool my_decimal_is_zero(const my_decimal *decimal_value)
235
return decimal_is_zero((decimal_t*) decimal_value);
240
int my_decimal_round(uint mask, const my_decimal *from, int scale,
241
bool truncate, my_decimal *to)
243
return check_result(mask, decimal_round((decimal_t*) from, to, scale,
244
(truncate ? TRUNCATE : HALF_UP)));
249
int my_decimal_floor(uint mask, const my_decimal *from, my_decimal *to)
251
return check_result(mask, decimal_round((decimal_t*) from, to, 0, FLOOR));
256
int my_decimal_ceiling(uint mask, const my_decimal *from, my_decimal *to)
258
return check_result(mask, decimal_round((decimal_t*) from, to, 0, CEILING));
263
int my_decimal2string(uint mask, const my_decimal *d, uint fixed_prec,
264
uint fixed_dec, char filler, String *str);
268
int my_decimal2int(uint mask, const my_decimal *d, my_bool unsigned_flag,
272
/* decimal_round can return only E_DEC_TRUNCATED */
273
decimal_round((decimal_t*)d, &rounded, 0, HALF_UP);
274
return check_result(mask, (unsigned_flag ?
275
decimal2ulonglong(&rounded, (ulonglong *)l) :
276
decimal2longlong(&rounded, l)));
281
int my_decimal2double(uint mask, const my_decimal *d, double *result)
283
/* No need to call check_result as this will always succeed */
284
return decimal2double((decimal_t*) d, result);
289
int str2my_decimal(uint mask, const char *str, my_decimal *d, char **end)
291
return check_result_and_overflow(mask, string2decimal(str,(decimal_t*)d,end),
296
int str2my_decimal(uint mask, const char *from, uint length,
297
CHARSET_INFO *charset, my_decimal *decimal_value);
299
#if defined(MYSQL_SERVER)
301
int string2my_decimal(uint mask, const String *str, my_decimal *d)
303
return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
307
my_decimal *date2my_decimal(MYSQL_TIME *ltime, my_decimal *dec);
310
#endif /*defined(MYSQL_SERVER) */
313
int double2my_decimal(uint mask, double val, my_decimal *d)
315
return check_result_and_overflow(mask, double2decimal(val, (decimal_t*)d), d);
320
int int2my_decimal(uint mask, longlong i, my_bool unsigned_flag, my_decimal *d)
322
return check_result(mask, (unsigned_flag ?
323
ulonglong2decimal((ulonglong)i, d) :
324
longlong2decimal(i, d)));
329
void my_decimal_neg(decimal_t *arg)
331
if (decimal_is_zero(arg))
341
int my_decimal_add(uint mask, my_decimal *res, const my_decimal *a,
344
return check_result_and_overflow(mask,
345
decimal_add((decimal_t*)a,(decimal_t*)b,res),
351
int my_decimal_sub(uint mask, my_decimal *res, const my_decimal *a,
354
return check_result_and_overflow(mask,
355
decimal_sub((decimal_t*)a,(decimal_t*)b,res),
361
int my_decimal_mul(uint mask, my_decimal *res, const my_decimal *a,
364
return check_result_and_overflow(mask,
365
decimal_mul((decimal_t*)a,(decimal_t*)b,res),
371
int my_decimal_div(uint mask, my_decimal *res, const my_decimal *a,
372
const my_decimal *b, int div_scale_inc)
374
return check_result_and_overflow(mask,
375
decimal_div((decimal_t*)a,(decimal_t*)b,res,
382
int my_decimal_mod(uint mask, my_decimal *res, const my_decimal *a,
385
return check_result_and_overflow(mask,
386
decimal_mod((decimal_t*)a,(decimal_t*)b,res),
393
-1 if a<b, 1 if a>b and 0 if a==b
396
int my_decimal_cmp(const my_decimal *a, const my_decimal *b)
398
return decimal_cmp((decimal_t*) a, (decimal_t*) b);
403
int my_decimal_intg(const my_decimal *a)
405
return decimal_intg((decimal_t*) a);
409
void my_decimal_trim(ulong *precision, uint *scale);
412
#endif /*my_decimal_h*/