~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
1122.2.10 by Monty Taylor
Fixed all of the include guards.
16
#ifndef MYSTRINGS_DECIMAL_H
17
#define MYSTRINGS_DECIMAL_H
1 by brian
clean slate
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_bin_size(int precision, int scale);
50
51
int decimal_intg(decimal_t *from);
52
int decimal_add(decimal_t *from1, decimal_t *from2, decimal_t *to);
53
int decimal_sub(decimal_t *from1, decimal_t *from2, decimal_t *to);
54
int decimal_cmp(decimal_t *from1, decimal_t *from2);
55
int decimal_mul(decimal_t *from1, decimal_t *from2, decimal_t *to);
56
int decimal_div(decimal_t *from1, decimal_t *from2, decimal_t *to,
57
                int scale_incr);
58
int decimal_mod(decimal_t *from1, decimal_t *from2, decimal_t *to);
59
int decimal_round(decimal_t *from, decimal_t *to, int new_scale,
60
                  decimal_round_mode mode);
61
int decimal_is_zero(decimal_t *from);
62
void max_decimal(int precision, int frac, decimal_t *to);
63
64
#define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0)
65
#define string2decimal_fixed(A,B,C) internal_str2dec((A), (B), (C), 1)
66
67
/* set a decimal_t to zero */
68
69
#define decimal_make_zero(dec)        do {                \
70
                                        (dec)->buf[0]=0;    \
71
                                        (dec)->intg=1;      \
72
                                        (dec)->frac=0;      \
73
                                        (dec)->sign=0;      \
74
                                      } while(0)
75
76
/*
77
  returns the length of the buffer to hold string representation
78
  of the decimal (including decimal dot, possible sign and \0)
79
*/
80
81
#define decimal_string_size(dec) (((dec)->intg ? (dec)->intg : 1) + \
82
				  (dec)->frac + ((dec)->frac > 0) + 2)
83
84
/* negate a decimal */
85
#define decimal_neg(dec) do { (dec)->sign^=1; } while(0)
86
87
/*
88
  conventions:
89
90
    decimal_smth() == 0     -- everything's ok
91
    decimal_smth() <= 1     -- result is usable, but precision loss is possible
92
    decimal_smth() <= 2     -- result can be unusable, most significant digits
93
                               could've been lost
94
    decimal_smth() >  2     -- no result was generated
95
*/
96
97
#define E_DEC_OK                0
98
#define E_DEC_TRUNCATED         1
99
#define E_DEC_OVERFLOW          2
100
#define E_DEC_DIV_ZERO          4
101
#define E_DEC_BAD_NUM           8
102
#define E_DEC_OOM              16
103
104
#define E_DEC_ERROR            31
105
#define E_DEC_FATAL_ERROR      30
106
575.1.11 by Monty Taylor
Coupla little fixes.
107
#if defined(__cplusplus)
108
}
109
#endif
110
1122.2.10 by Monty Taylor
Fixed all of the include guards.
111
#endif /* MYSTRINGS_DECIMAL_H */
1 by brian
clean slate
112