9
3

decimal.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  12. #ifndef _decimal_h
  13. #define _decimal_h
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef enum
  18. {TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR}
  19. decimal_round_mode;
  20. typedef int32 decimal_digit_t;
  21. /**
  22. intg is the number of *decimal* digits (NOT number of decimal_digit_t's !)
  23. before the point
  24. frac is the number of decimal digits after the point
  25. len is the length of buf (length of allocated space) in decimal_digit_t's,
  26. not in bytes
  27. sign false means positive, true means negative
  28. buf is an array of decimal_digit_t's
  29. */
  30. typedef struct st_decimal_t {
  31. int intg, frac, len;
  32. my_bool sign;
  33. decimal_digit_t *buf;
  34. } decimal_t;
  35. int internal_str2dec(const char *from, decimal_t *to, char **end,
  36. my_bool fixed);
  37. int decimal2string(const decimal_t *from, char *to, int *to_len,
  38. int fixed_precision, int fixed_decimals,
  39. char filler);
  40. int decimal2ulonglong(const decimal_t *from, ulonglong *to);
  41. int ulonglong2decimal(ulonglong from, decimal_t *to);
  42. int decimal2longlong(const decimal_t *from, longlong *to);
  43. int longlong2decimal(longlong from, decimal_t *to);
  44. int decimal2double(const decimal_t *from, double *to);
  45. int double2decimal(double from, decimal_t *to);
  46. int decimal_actual_fraction(decimal_t *from);
  47. int decimal2bin(const decimal_t *from, uchar *to, int precision, int scale);
  48. int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale);
  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. int decimal_intg(const decimal_t *from);
  54. int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  55. int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  56. int decimal_cmp(const decimal_t *from1, const decimal_t *from2);
  57. int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  58. int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to,
  59. int scale_incr);
  60. int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  61. int decimal_round(const decimal_t *from, decimal_t *to, int new_scale,
  62. decimal_round_mode mode);
  63. int decimal_is_zero(const decimal_t *from);
  64. void max_decimal(int precision, int frac, decimal_t *to);
  65. #define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0)
  66. #define string2decimal_fixed(A,B,C) internal_str2dec((A), (B), (C), 1)
  67. /* set a decimal_t to zero */
  68. #define decimal_make_zero(dec) do { \
  69. (dec)->buf[0]=0; \
  70. (dec)->intg=1; \
  71. (dec)->frac=0; \
  72. (dec)->sign=0; \
  73. } while(0)
  74. /*
  75. returns the length of the buffer to hold string representation
  76. of the decimal (including decimal dot, possible sign and \0)
  77. */
  78. #define decimal_string_size(dec) (((dec)->intg ? (dec)->intg : 1) + \
  79. (dec)->frac + ((dec)->frac > 0) + 2)
  80. /* negate a decimal */
  81. #define decimal_neg(dec) do { (dec)->sign^=1; } while(0)
  82. /*
  83. conventions:
  84. decimal_smth() == 0 -- everything's ok
  85. decimal_smth() <= 1 -- result is usable, but precision loss is possible
  86. decimal_smth() <= 2 -- result can be unusable, most significant digits
  87. could've been lost
  88. decimal_smth() > 2 -- no result was generated
  89. */
  90. #define E_DEC_OK 0
  91. #define E_DEC_TRUNCATED 1
  92. #define E_DEC_OVERFLOW 2
  93. #define E_DEC_DIV_ZERO 4
  94. #define E_DEC_BAD_NUM 8
  95. #define E_DEC_OOM 16
  96. #define E_DEC_ERROR 31
  97. #define E_DEC_FATAL_ERROR 30
  98. #ifdef __cplusplus
  99. }
  100. #endif
  101. #endif