9
3

decimal.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. #ifndef _decimal_h
  13. #define _decimal_h
  14. typedef enum
  15. {TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR}
  16. decimal_round_mode;
  17. typedef int32 decimal_digit_t;
  18. typedef struct st_decimal_t {
  19. int intg, frac, len;
  20. my_bool sign;
  21. decimal_digit_t *buf;
  22. } decimal_t;
  23. int internal_str2dec(const char *from, decimal_t *to, char **end,
  24. my_bool fixed);
  25. int decimal2string(decimal_t *from, char *to, int *to_len,
  26. int fixed_precision, int fixed_decimals,
  27. char filler);
  28. int decimal2ulonglong(decimal_t *from, ulonglong *to);
  29. int ulonglong2decimal(ulonglong from, decimal_t *to);
  30. int decimal2longlong(decimal_t *from, longlong *to);
  31. int longlong2decimal(longlong from, decimal_t *to);
  32. int decimal2double(decimal_t *from, double *to);
  33. int double2decimal(double from, decimal_t *to);
  34. int decimal_actual_fraction(decimal_t *from);
  35. int decimal2bin(decimal_t *from, uchar *to, int precision, int scale);
  36. int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale);
  37. int decimal_size(int precision, int scale);
  38. int decimal_bin_size(int precision, int scale);
  39. int decimal_result_size(decimal_t *from1, decimal_t *from2, char op,
  40. int param);
  41. int decimal_intg(decimal_t *from);
  42. int decimal_add(decimal_t *from1, decimal_t *from2, decimal_t *to);
  43. int decimal_sub(decimal_t *from1, decimal_t *from2, decimal_t *to);
  44. int decimal_cmp(decimal_t *from1, decimal_t *from2);
  45. int decimal_mul(decimal_t *from1, decimal_t *from2, decimal_t *to);
  46. int decimal_div(decimal_t *from1, decimal_t *from2, decimal_t *to,
  47. int scale_incr);
  48. int decimal_mod(decimal_t *from1, decimal_t *from2, decimal_t *to);
  49. int decimal_round(decimal_t *from, decimal_t *to, int new_scale,
  50. decimal_round_mode mode);
  51. int decimal_is_zero(decimal_t *from);
  52. void max_decimal(int precision, int frac, decimal_t *to);
  53. #define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0)
  54. #define string2decimal_fixed(A,B,C) internal_str2dec((A), (B), (C), 1)
  55. /* set a decimal_t to zero */
  56. #define decimal_make_zero(dec) do { \
  57. (dec)->buf[0]=0; \
  58. (dec)->intg=1; \
  59. (dec)->frac=0; \
  60. (dec)->sign=0; \
  61. } while(0)
  62. /*
  63. returns the length of the buffer to hold string representation
  64. of the decimal (including decimal dot, possible sign and \0)
  65. */
  66. #define decimal_string_size(dec) (((dec)->intg ? (dec)->intg : 1) + \
  67. (dec)->frac + ((dec)->frac > 0) + 2)
  68. /* negate a decimal */
  69. #define decimal_neg(dec) do { (dec)->sign^=1; } while(0)
  70. /*
  71. conventions:
  72. decimal_smth() == 0 -- everything's ok
  73. decimal_smth() <= 1 -- result is usable, but precision loss is possible
  74. decimal_smth() <= 2 -- result can be unusable, most significant digits
  75. could've been lost
  76. decimal_smth() > 2 -- no result was generated
  77. */
  78. #define E_DEC_OK 0
  79. #define E_DEC_TRUNCATED 1
  80. #define E_DEC_OVERFLOW 2
  81. #define E_DEC_DIV_ZERO 4
  82. #define E_DEC_BAD_NUM 8
  83. #define E_DEC_OOM 16
  84. #define E_DEC_ERROR 31
  85. #define E_DEC_FATAL_ERROR 30
  86. #endif