Clone of PhatAC @ https://github.com/floaterxk/PhatAC

decimal.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Copyright (c) 2000, 2015, 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_INCLUDED
  13. #define DECIMAL_INCLUDED
  14. typedef enum
  15. {TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR}
  16. decimal_round_mode;
  17. typedef int32 decimal_digit_t;
  18. /**
  19. intg is the number of *decimal* digits (NOT number of decimal_digit_t's !)
  20. before the point
  21. frac is the number of decimal digits after the point
  22. len is the length of buf (length of allocated space) in decimal_digit_t's,
  23. not in bytes
  24. sign false means positive, true means negative
  25. buf is an array of decimal_digit_t's
  26. */
  27. typedef struct st_decimal_t {
  28. int intg, frac, len;
  29. my_bool sign;
  30. decimal_digit_t *buf;
  31. } decimal_t;
  32. #ifndef MYSQL_ABI_CHECK
  33. int internal_str2dec(const char *from, decimal_t *to, char **end,
  34. my_bool fixed);
  35. int decimal2string(const decimal_t *from, char *to, int *to_len,
  36. int fixed_precision, int fixed_decimals,
  37. char filler);
  38. int decimal2ulonglong(decimal_t *from, ulonglong *to);
  39. int ulonglong2decimal(ulonglong from, decimal_t *to);
  40. int decimal2longlong(decimal_t *from, longlong *to);
  41. int longlong2decimal(longlong from, decimal_t *to);
  42. int decimal2double(const decimal_t *from, double *to);
  43. int double2decimal(double from, decimal_t *to);
  44. int decimal_actual_fraction(decimal_t *from);
  45. int decimal2bin(decimal_t *from, uchar *to, int precision, int scale);
  46. int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale);
  47. /**
  48. Convert decimal to lldiv_t.
  49. The integer part is stored in to->quot.
  50. The fractional part is multiplied to 10^9 and stored to to->rem.
  51. @param from Decimal value
  52. @param to lldiv_t value
  53. @retval 0 on success
  54. @retval !0 in error
  55. */
  56. int decimal2lldiv_t(const decimal_t *from, lldiv_t *to);
  57. /**
  58. Convert doube to lldiv_t.
  59. The integer part is stored in to->quot.
  60. The fractional part is multiplied to 10^9 and stored to to->rem.
  61. @param from Decimal value
  62. @param to lldiv_t value
  63. @retval 0 on success
  64. @retval !0 in error
  65. */
  66. int double2lldiv_t(double from, lldiv_t *to);
  67. int decimal_size(int precision, int scale);
  68. int decimal_bin_size(int precision, int scale);
  69. int decimal_result_size(decimal_t *from1, decimal_t *from2, char op,
  70. int param);
  71. int decimal_intg(const decimal_t *from);
  72. int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  73. int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  74. int decimal_cmp(const decimal_t *from1, const decimal_t *from2);
  75. int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  76. int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to,
  77. int scale_incr);
  78. int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  79. int decimal_round(const decimal_t *from, decimal_t *to, int new_scale,
  80. decimal_round_mode mode);
  81. int decimal_is_zero(const decimal_t *from);
  82. void max_decimal(int precision, int frac, decimal_t *to);
  83. #define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0)
  84. #define string2decimal_fixed(A,B,C) internal_str2dec((A), (B), (C), 1)
  85. /* set a decimal_t to zero */
  86. #define decimal_make_zero(dec) do { \
  87. (dec)->buf[0]=0; \
  88. (dec)->intg=1; \
  89. (dec)->frac=0; \
  90. (dec)->sign=0; \
  91. } while(0)
  92. /*
  93. returns the length of the buffer to hold string representation
  94. of the decimal (including decimal dot, possible sign and \0)
  95. */
  96. #define decimal_string_size(dec) (((dec)->intg ? (dec)->intg : 1) + \
  97. (dec)->frac + ((dec)->frac > 0) + 2)
  98. /*
  99. conventions:
  100. decimal_smth() == 0 -- everything's ok
  101. decimal_smth() <= 1 -- result is usable, but precision loss is possible
  102. decimal_smth() <= 2 -- result can be unusable, most significant digits
  103. could've been lost
  104. decimal_smth() > 2 -- no result was generated
  105. */
  106. #define E_DEC_OK 0
  107. #define E_DEC_TRUNCATED 1
  108. #define E_DEC_OVERFLOW 2
  109. #define E_DEC_DIV_ZERO 4
  110. #define E_DEC_BAD_NUM 8
  111. #define E_DEC_OOM 16
  112. #define E_DEC_ERROR 31
  113. #define E_DEC_FATAL_ERROR 30
  114. #endif // !MYSQL_ABI_CHECK
  115. #endif