decimal.h 3.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (C) 2000 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef _decimal_h
#define _decimal_h

#include <my_global.h>

22
typedef enum {TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR} decimal_round_mode;
23
typedef int32 decimal_digit;
24 25

typedef struct st_decimal {
26
  int    intg, frac, len;
27 28 29 30
  my_bool sign;
  decimal_digit *buf;
} decimal;

31
int decimal2string(decimal *from, char *to, int *to_len);
32
int string2decimal(char *from, decimal *to, char **end);
serg@serg.mylan's avatar
serg@serg.mylan committed
33
int string2decimal_fixed(char *from, decimal *to, char **end);
34 35 36 37 38 39
int decimal2ulonglong(decimal *from, ulonglong *to);
int ulonglong2decimal(ulonglong from, decimal *to);
int decimal2longlong(decimal *from, longlong *to);
int longlong2decimal(longlong from, decimal *to);
int decimal2double(decimal *from, double *to);
int double2decimal(double from, decimal *to);
40 41 42 43 44 45
int decimal2bin(decimal *from, char *to, int precision, int scale);
int bin2decimal(char *from, decimal *to, int precision, int scale);

int decimal_size(int precision, int scale);
int decimal_bin_size(int precision, int scale);
int decimal_result_size(decimal *from1, decimal *from2, char op, int param);
46 47 48

int decimal_add(decimal *from1, decimal *from2, decimal *to);
int decimal_sub(decimal *from1, decimal *from2, decimal *to);
serg@serg.mylan's avatar
serg@serg.mylan committed
49
int decimal_cmp(decimal *from1, decimal *from2);
50 51 52
int decimal_mul(decimal *from1, decimal *from2, decimal *to);
int decimal_div(decimal *from1, decimal *from2, decimal *to, int scale_incr);
int decimal_mod(decimal *from1, decimal *from2, decimal *to);
serg@serg.mylan's avatar
serg@serg.mylan committed
53
int decimal_round(decimal *from, decimal *to, int new_scale, decimal_round_mode mode);
54

55 56 57 58
/*
  the following works only on special "zero" decimal, not on any
  decimal that happen to evaluate to zero
*/
serg@serg.mylan's avatar
serg@serg.mylan committed
59

60 61
#define decimal_is_zero(dec) ((dec)->intg1==1 && (dec)->frac1==0 && (dec)->buf[0]==0)

serg@serg.mylan's avatar
serg@serg.mylan committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/* set a decimal to zero */

#define decimal_make_zero(dec)        do {                \
                                        (dec)->buf[0]=0;    \
                                        (dec)->intg=1;      \
                                        (dec)->frac=0;      \
                                        (dec)->sign=0;      \
                                      } while(0)

/*
  returns the length of the buffer to hold string representation
  of the decimal
*/

#define decimal_string_size(dec) ((dec)->intg + (dec)->frac + ((dec)->frac > 0) + 1)

/* negate a decimal */
serg@serg.mylan's avatar
serg@serg.mylan committed
79
#define decimal_neg(dec) do { (dec)->sign^=1; } while(0)
serg@serg.mylan's avatar
serg@serg.mylan committed
80

81 82 83 84
/*
  conventions:

    decimal_smth() == 0     -- everything's ok
85 86 87 88
    decimal_smth() <= 1     -- result is usable, but precision loss is possible
    decimal_smth() <= 2     -- result can be unusable, most significant digits
                               could've been lost
    decimal_smth() >  2     -- no result was generated
89 90 91
*/

#define E_DEC_OK                0
92 93
#define E_DEC_TRUNCATED         1
#define E_DEC_OVERFLOW          2
94 95 96 97 98 99
#define E_DEC_DIV_ZERO          4
#define E_DEC_BAD_NUM           8
#define E_DEC_OOM              16

#define E_DEC_ERROR            31
#define E_DEC_FATAL_ERROR      30
100 101 102

#endif