2004-10-18 14:06:46 +02:00
|
|
|
/* 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
|
|
|
|
|
2004-11-03 18:43:48 +01:00
|
|
|
typedef enum {TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR} decimal_round_mode;
|
2004-10-19 14:38:54 +02:00
|
|
|
typedef int32 decimal_digit;
|
2004-10-18 14:06:46 +02:00
|
|
|
|
|
|
|
typedef struct st_decimal {
|
2004-10-19 14:38:54 +02:00
|
|
|
int intg, frac, len;
|
2004-10-18 14:06:46 +02:00
|
|
|
my_bool sign;
|
|
|
|
decimal_digit *buf;
|
|
|
|
} decimal;
|
|
|
|
|
2004-10-19 14:38:54 +02:00
|
|
|
int decimal2string(decimal *from, char *to, int *to_len);
|
2004-10-18 14:06:46 +02:00
|
|
|
int string2decimal(char *from, decimal *to, char **end);
|
2004-10-29 00:22:54 +02:00
|
|
|
int string2decimal_fixed(char *from, decimal *to, char **end);
|
2004-10-18 14:06:46 +02:00
|
|
|
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);
|
2004-10-19 14:38:54 +02:00
|
|
|
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);
|
2004-10-18 14:06:46 +02:00
|
|
|
|
|
|
|
int decimal_add(decimal *from1, decimal *from2, decimal *to);
|
|
|
|
int decimal_sub(decimal *from1, decimal *from2, decimal *to);
|
2004-10-20 15:26:03 +02:00
|
|
|
int decimal_cmp(decimal *from1, decimal *from2);
|
2004-10-18 14:06:46 +02:00
|
|
|
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);
|
2004-10-31 13:15:44 +01:00
|
|
|
int decimal_round(decimal *from, decimal *to, int new_scale, decimal_round_mode mode);
|
2004-10-18 14:06:46 +02:00
|
|
|
|
2004-10-19 14:38:54 +02:00
|
|
|
/*
|
|
|
|
the following works only on special "zero" decimal, not on any
|
|
|
|
decimal that happen to evaluate to zero
|
|
|
|
*/
|
2004-10-29 00:22:54 +02:00
|
|
|
|
2004-10-19 14:38:54 +02:00
|
|
|
#define decimal_is_zero(dec) ((dec)->intg1==1 && (dec)->frac1==0 && (dec)->buf[0]==0)
|
|
|
|
|
2004-10-29 00:22:54 +02:00
|
|
|
/* 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
|
2004-11-03 22:30:37 +01:00
|
|
|
of the decimal (including decimal dot, possible sign and \0)
|
2004-10-29 00:22:54 +02:00
|
|
|
*/
|
|
|
|
|
2004-11-03 22:30:37 +01:00
|
|
|
#define decimal_string_size(dec) ((dec)->intg + (dec)->frac + ((dec)->frac > 0) + 2)
|
2004-10-29 00:22:54 +02:00
|
|
|
|
|
|
|
/* negate a decimal */
|
2004-10-31 12:29:38 +01:00
|
|
|
#define decimal_neg(dec) do { (dec)->sign^=1; } while(0)
|
2004-10-29 00:22:54 +02:00
|
|
|
|
2004-10-18 14:06:46 +02:00
|
|
|
/*
|
|
|
|
conventions:
|
|
|
|
|
|
|
|
decimal_smth() == 0 -- everything's ok
|
2004-10-19 14:38:54 +02:00
|
|
|
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
|
2004-10-18 14:06:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define E_DEC_OK 0
|
2004-10-19 14:38:54 +02:00
|
|
|
#define E_DEC_TRUNCATED 1
|
|
|
|
#define E_DEC_OVERFLOW 2
|
2004-11-03 18:43:48 +01:00
|
|
|
#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
|
2004-10-18 14:06:46 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|