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
32
#include <mystrings/decimal.h>
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_t). */
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)
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; }
119
#ifndef DRIZZLE_CLIENT
120
int decimal_operation_results(int result);
122
inline int decimal_operation_results(int result)
126
#endif /*DRIZZLE_CLIENT*/
129
void max_my_decimal(my_decimal *to, int precision, int frac)
131
assert((precision <= DECIMAL_MAX_PRECISION)&&
132
(frac <= DECIMAL_MAX_SCALE));
133
max_decimal(precision, frac, (decimal_t*) to);
136
inline void max_internal_decimal(my_decimal *to)
138
max_my_decimal(to, DECIMAL_MAX_PRECISION, 0);
141
inline int check_result(uint mask, int result)
144
decimal_operation_results(result);
148
inline int check_result_and_overflow(uint mask, int result, my_decimal *val)
150
if (check_result(mask, result) & E_DEC_OVERFLOW)
152
bool sign= val->sign();
153
val->fix_buffer_pointer();
154
max_internal_decimal(val);
160
inline uint my_decimal_length_to_precision(uint length, uint scale,
163
return (uint) (length - (scale>0 ? 1:0) - (unsigned_flag ? 0:1));
166
inline uint32_t my_decimal_precision_to_length(uint precision, uint8_t scale,
169
set_if_smaller(precision, DECIMAL_MAX_PRECISION);
170
return (uint32_t)(precision + (scale>0 ? 1:0) + (unsigned_flag ? 0:1));
174
int my_decimal_string_length(const my_decimal *d)
176
return decimal_string_size(d);
181
int my_decimal_max_length(const my_decimal *d)
183
/* -1 because we do not count \0 */
184
return decimal_string_size(d) - 1;
189
int my_decimal_get_binary_size(uint precision, uint scale)
191
return decimal_bin_size((int)precision, (int)scale);
196
void my_decimal2decimal(const my_decimal *from, my_decimal *to)
199
to->fix_buffer_pointer();
203
int my_decimal2binary(uint mask, const my_decimal *d, uchar *bin, int prec,
208
int binary2my_decimal(uint mask, const uchar *bin, my_decimal *d, int prec,
211
return check_result(mask, bin2decimal(bin, (decimal_t*) d, prec, scale));
216
int my_decimal_set_zero(my_decimal *d)
218
decimal_make_zero(((decimal_t*) d));
224
bool my_decimal_is_zero(const my_decimal *decimal_value)
226
return decimal_is_zero((decimal_t*) decimal_value);
231
int my_decimal_round(uint mask, const my_decimal *from, int scale,
232
bool truncate, my_decimal *to)
234
return check_result(mask, decimal_round((decimal_t*) from, to, scale,
235
(truncate ? TRUNCATE : HALF_UP)));
240
int my_decimal_floor(uint mask, const my_decimal *from, my_decimal *to)
242
return check_result(mask, decimal_round((decimal_t*) from, to, 0, FLOOR));
247
int my_decimal_ceiling(uint mask, const my_decimal *from, my_decimal *to)
249
return check_result(mask, decimal_round((decimal_t*) from, to, 0, CEILING));
253
#ifndef DRIZZLE_CLIENT
254
int my_decimal2string(uint mask, const my_decimal *d, uint fixed_prec,
255
uint fixed_dec, char filler, String *str);
259
int my_decimal2int(uint mask, const my_decimal *d, bool unsigned_flag,
263
/* decimal_round can return only E_DEC_TRUNCATED */
264
decimal_round((decimal_t*)d, &rounded, 0, HALF_UP);
265
return check_result(mask, (unsigned_flag ?
266
decimal2uint64_t(&rounded, (uint64_t *)l) :
267
decimal2int64_t(&rounded, l)));
272
int my_decimal2double(uint mask __attribute__((unused)),
273
const my_decimal *d, double *result)
275
/* No need to call check_result as this will always succeed */
276
return decimal2double((decimal_t*) d, result);
281
int str2my_decimal(uint mask, char *str, my_decimal *d, char **end)
283
return check_result_and_overflow(mask, string2decimal(str,(decimal_t*)d,end),
288
int str2my_decimal(uint mask, const char *from, uint length,
289
const CHARSET_INFO * charset, my_decimal *decimal_value);
291
#if defined(DRIZZLE_SERVER)
293
int string2my_decimal(uint mask, const String *str, my_decimal *d)
295
return str2my_decimal(mask, str->ptr(), str->length(), str->charset(), d);
299
my_decimal *date2my_decimal(DRIZZLE_TIME *ltime, my_decimal *dec);
302
#endif /*defined(DRIZZLE_SERVER) */
305
int double2my_decimal(uint mask, double val, my_decimal *d)
307
return check_result_and_overflow(mask, double2decimal(val, (decimal_t*)d), d);
312
int int2my_decimal(uint mask, int64_t i, bool unsigned_flag, my_decimal *d)
314
return check_result(mask, (unsigned_flag ?
315
uint64_t2decimal((uint64_t)i, d) :
316
int64_t2decimal(i, d)));
321
void my_decimal_neg(decimal_t *arg)
323
if (decimal_is_zero(arg))
333
int my_decimal_add(uint mask, my_decimal *res, const my_decimal *a,
336
return check_result_and_overflow(mask,
337
decimal_add((decimal_t*)a,(decimal_t*)b,res),
343
int my_decimal_sub(uint mask, my_decimal *res, const my_decimal *a,
346
return check_result_and_overflow(mask,
347
decimal_sub((decimal_t*)a,(decimal_t*)b,res),
353
int my_decimal_mul(uint mask, my_decimal *res, const my_decimal *a,
356
return check_result_and_overflow(mask,
357
decimal_mul((decimal_t*)a,(decimal_t*)b,res),
363
int my_decimal_div(uint mask, my_decimal *res, const my_decimal *a,
364
const my_decimal *b, int div_scale_inc)
366
return check_result_and_overflow(mask,
367
decimal_div((decimal_t*)a,(decimal_t*)b,res,
374
int my_decimal_mod(uint mask, my_decimal *res, const my_decimal *a,
377
return check_result_and_overflow(mask,
378
decimal_mod((decimal_t*)a,(decimal_t*)b,res),
385
-1 if a<b, 1 if a>b and 0 if a==b
388
int my_decimal_cmp(const my_decimal *a, const my_decimal *b)
390
return decimal_cmp((decimal_t*) a, (decimal_t*) b);
395
int my_decimal_intg(const my_decimal *a)
397
return decimal_intg((decimal_t*) a);
401
void my_decimal_trim(uint32_t *precision, uint *scale);
404
#endif /*my_decimal_h*/