~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
#ifndef _decimal_h
17
#define _decimal_h
18
575.1.11 by Monty Taylor
Coupla little fixes.
19
#if defined(__cplusplus)
20
extern "C" {
21
#endif
22
1 by brian
clean slate
23
typedef enum
24
{TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR}
25
  decimal_round_mode;
205 by Brian Aker
uint32 -> uin32_t
26
typedef int32_t decimal_digit_t;
1 by brian
clean slate
27
28
typedef struct st_decimal_t {
29
  int    intg, frac, len;
276 by Brian Aker
Cleaned out my_bool from strings.
30
  bool sign;
1 by brian
clean slate
31
  decimal_digit_t *buf;
32
} decimal_t;
33
287.3.10 by Monty Taylor
Const correctness change.
34
int internal_str2dec(char *from, decimal_t *to, char **end,
276 by Brian Aker
Cleaned out my_bool from strings.
35
                     bool fixed);
1 by brian
clean slate
36
int decimal2string(decimal_t *from, char *to, int *to_len,
37
                   int fixed_precision, int fixed_decimals,
38
                   char filler);
151 by Brian Aker
Ulonglong to uint64_t
39
int decimal2uint64_t(decimal_t *from, uint64_t *to);
40
int uint64_t2decimal(uint64_t from, decimal_t *to);
152 by Brian Aker
longlong replacement
41
int decimal2int64_t(decimal_t *from, int64_t *to);
42
int int64_t2decimal(int64_t from, decimal_t *to);
1 by brian
clean slate
43
int decimal2double(decimal_t *from, double *to);
44
int double2decimal(double from, decimal_t *to);
45
int decimal_actual_fraction(decimal_t *from);
481 by Brian Aker
Remove all of uchar.
46
int decimal2bin(decimal_t *from, unsigned char *to, int precision, int scale);
47
int bin2decimal(const unsigned char *from, decimal_t *to, int precision, int scale);
1 by brian
clean slate
48
49
int decimal_size(int precision, int scale);
50
int decimal_bin_size(int precision, int scale);
51
int decimal_result_size(decimal_t *from1, decimal_t *from2, char op,
52
                        int param);
53
54
int decimal_intg(decimal_t *from);
55
int decimal_add(decimal_t *from1, decimal_t *from2, decimal_t *to);
56
int decimal_sub(decimal_t *from1, decimal_t *from2, decimal_t *to);
57
int decimal_cmp(decimal_t *from1, decimal_t *from2);
58
int decimal_mul(decimal_t *from1, decimal_t *from2, decimal_t *to);
59
int decimal_div(decimal_t *from1, decimal_t *from2, decimal_t *to,
60
                int scale_incr);
61
int decimal_mod(decimal_t *from1, decimal_t *from2, decimal_t *to);
62
int decimal_round(decimal_t *from, decimal_t *to, int new_scale,
63
                  decimal_round_mode mode);
64
int decimal_is_zero(decimal_t *from);
65
void max_decimal(int precision, int frac, decimal_t *to);
66
67
#define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0)
68
#define string2decimal_fixed(A,B,C) internal_str2dec((A), (B), (C), 1)
69
70
/* set a decimal_t to zero */
71
72
#define decimal_make_zero(dec)        do {                \
73
                                        (dec)->buf[0]=0;    \
74
                                        (dec)->intg=1;      \
75
                                        (dec)->frac=0;      \
76
                                        (dec)->sign=0;      \
77
                                      } while(0)
78
79
/*
80
  returns the length of the buffer to hold string representation
81
  of the decimal (including decimal dot, possible sign and \0)
82
*/
83
84
#define decimal_string_size(dec) (((dec)->intg ? (dec)->intg : 1) + \
85
				  (dec)->frac + ((dec)->frac > 0) + 2)
86
87
/* negate a decimal */
88
#define decimal_neg(dec) do { (dec)->sign^=1; } while(0)
89
90
/*
91
  conventions:
92
93
    decimal_smth() == 0     -- everything's ok
94
    decimal_smth() <= 1     -- result is usable, but precision loss is possible
95
    decimal_smth() <= 2     -- result can be unusable, most significant digits
96
                               could've been lost
97
    decimal_smth() >  2     -- no result was generated
98
*/
99
100
#define E_DEC_OK                0
101
#define E_DEC_TRUNCATED         1
102
#define E_DEC_OVERFLOW          2
103
#define E_DEC_DIV_ZERO          4
104
#define E_DEC_BAD_NUM           8
105
#define E_DEC_OOM              16
106
107
#define E_DEC_ERROR            31
108
#define E_DEC_FATAL_ERROR      30
109
575.1.11 by Monty Taylor
Coupla little fixes.
110
#if defined(__cplusplus)
111
}
112
#endif
113
1 by brian
clean slate
114
#endif
115