decimal.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. #ifndef DECIMAL_INCLUDED
  19. #define DECIMAL_INCLUDED
  20. typedef enum
  21. {TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR}
  22. decimal_round_mode;
  23. typedef int32 decimal_digit_t;
  24. /**
  25. intg is the number of *decimal* digits (NOT number of decimal_digit_t's !)
  26. before the point
  27. frac is the number of decimal digits after the point
  28. len is the length of buf (length of allocated space) in decimal_digit_t's,
  29. not in bytes
  30. sign false means positive, true means negative
  31. buf is an array of decimal_digit_t's
  32. */
  33. typedef struct st_decimal_t {
  34. int intg, frac, len;
  35. my_bool sign;
  36. decimal_digit_t *buf;
  37. } decimal_t;
  38. #ifndef MYSQL_ABI_CHECK
  39. int internal_str2dec(const char *from, decimal_t *to, char **end,
  40. my_bool fixed);
  41. int decimal2string(const decimal_t *from, char *to, int *to_len,
  42. int fixed_precision, int fixed_decimals,
  43. char filler);
  44. int decimal2ulonglong(decimal_t *from, ulonglong *to);
  45. int ulonglong2decimal(ulonglong from, decimal_t *to);
  46. int decimal2longlong(decimal_t *from, longlong *to);
  47. int longlong2decimal(longlong from, decimal_t *to);
  48. int decimal2double(const decimal_t *from, double *to);
  49. int double2decimal(double from, decimal_t *to);
  50. int decimal_actual_fraction(decimal_t *from);
  51. int decimal2bin(decimal_t *from, uchar *to, int precision, int scale);
  52. int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale);
  53. /**
  54. Convert decimal to lldiv_t.
  55. The integer part is stored in to->quot.
  56. The fractional part is multiplied to 10^9 and stored to to->rem.
  57. @param from Decimal value
  58. @param to lldiv_t value
  59. @retval 0 on success
  60. @retval !0 in error
  61. */
  62. int decimal2lldiv_t(const decimal_t *from, lldiv_t *to);
  63. /**
  64. Convert doube to lldiv_t.
  65. The integer part is stored in to->quot.
  66. The fractional part is multiplied to 10^9 and stored to to->rem.
  67. @param from Decimal value
  68. @param to lldiv_t value
  69. @retval 0 on success
  70. @retval !0 in error
  71. */
  72. int double2lldiv_t(double from, lldiv_t *to);
  73. int decimal_size(int precision, int scale);
  74. int decimal_bin_size(int precision, int scale);
  75. int decimal_result_size(decimal_t *from1, decimal_t *from2, char op,
  76. int param);
  77. int decimal_intg(const decimal_t *from);
  78. int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  79. int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  80. int decimal_cmp(const decimal_t *from1, const decimal_t *from2);
  81. int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  82. int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to,
  83. int scale_incr);
  84. int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to);
  85. int decimal_round(const decimal_t *from, decimal_t *to, int new_scale,
  86. decimal_round_mode mode);
  87. int decimal_is_zero(const decimal_t *from);
  88. void max_decimal(int precision, int frac, decimal_t *to);
  89. #define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0)
  90. #define string2decimal_fixed(A,B,C) internal_str2dec((A), (B), (C), 1)
  91. /* set a decimal_t to zero */
  92. #define decimal_make_zero(dec) do { \
  93. (dec)->buf[0]=0; \
  94. (dec)->intg=1; \
  95. (dec)->frac=0; \
  96. (dec)->sign=0; \
  97. } while(0)
  98. /*
  99. returns the length of the buffer to hold string representation
  100. of the decimal (including decimal dot, possible sign and \0)
  101. */
  102. #define decimal_string_size(dec) (((dec)->intg ? (dec)->intg : 1) + \
  103. (dec)->frac + ((dec)->frac > 0) + 2)
  104. /*
  105. conventions:
  106. decimal_smth() == 0 -- everything's ok
  107. decimal_smth() <= 1 -- result is usable, but precision loss is possible
  108. decimal_smth() <= 2 -- result can be unusable, most significant digits
  109. could've been lost
  110. decimal_smth() > 2 -- no result was generated
  111. */
  112. #define E_DEC_OK 0
  113. #define E_DEC_TRUNCATED 1
  114. #define E_DEC_OVERFLOW 2
  115. #define E_DEC_DIV_ZERO 4
  116. #define E_DEC_BAD_NUM 8
  117. #define E_DEC_OOM 16
  118. #define E_DEC_ERROR 31
  119. #define E_DEC_FATAL_ERROR 30
  120. #endif // !MYSQL_ABI_CHECK
  121. #endif