bcmath.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* bcmath.h: bcmath library header. */
  2. /*
  3. Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
  4. Copyright (C) 2000 Philip A. Nelson
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details. (COPYING.LIB)
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to:
  15. The Free Software Foundation, Inc.
  16. 59 Temple Place, Suite 330
  17. Boston, MA 02111-1307 USA.
  18. You may contact the author by:
  19. e-mail: philnelson@acm.org
  20. us-mail: Philip A. Nelson
  21. Computer Science Department, 9062
  22. Western Washington University
  23. Bellingham, WA 98226-9062
  24. *************************************************************************/
  25. #ifndef _BCMATH_H_
  26. #define _BCMATH_H_
  27. typedef enum {PLUS, MINUS} sign;
  28. typedef struct bc_struct *bc_num;
  29. typedef struct bc_struct
  30. {
  31. sign n_sign;
  32. int n_len; /* The number of digits before the decimal point. */
  33. int n_scale; /* The number of digits after the decimal point. */
  34. int n_refs; /* The number of pointers to this number. */
  35. bc_num n_next; /* Linked list for available list. */
  36. char *n_ptr; /* The pointer to the actual storage.
  37. If NULL, n_value points to the inside of
  38. another number (bc_multiply...) and should
  39. not be "freed." */
  40. char *n_value; /* The number. Not zero char terminated.
  41. May not point to the same place as n_ptr as
  42. in the case of leading zeros generated. */
  43. } bc_struct;
  44. #ifdef HAVE_CONFIG_H
  45. #include "config.h"
  46. #endif
  47. #include "php.h"
  48. #include "../../php_bcmath.h"
  49. /* The base used in storing the numbers in n_value above.
  50. Currently this MUST be 10. */
  51. #define BASE 10
  52. /* Some useful macros and constants. */
  53. #define CH_VAL(c) (c - '0')
  54. #define BCD_CHAR(d) (d + '0')
  55. #ifdef MIN
  56. #undef MIN
  57. #undef MAX
  58. #endif
  59. #define MAX(a, b) ((a)>(b)?(a):(b))
  60. #define MIN(a, b) ((a)>(b)?(b):(a))
  61. #define ODD(a) ((a)&1)
  62. #ifndef TRUE
  63. #define TRUE 1
  64. #define FALSE 0
  65. #endif
  66. #ifndef LONG_MAX
  67. #define LONG_MAX 0x7ffffff
  68. #endif
  69. /* Function Prototypes */
  70. /* Define the _PROTOTYPE macro if it is needed. */
  71. #ifndef _PROTOTYPE
  72. #ifdef __STDC__
  73. #define _PROTOTYPE(func, args) func args
  74. #else
  75. #define _PROTOTYPE(func, args) func()
  76. #endif
  77. #endif
  78. _PROTOTYPE(void bc_init_numbers, (TSRMLS_D));
  79. _PROTOTYPE(bc_num _bc_new_num_ex, (int length, int scale, int persistent));
  80. _PROTOTYPE(void _bc_free_num_ex, (bc_num *num, int persistent));
  81. _PROTOTYPE(bc_num bc_copy_num, (bc_num num));
  82. _PROTOTYPE(void bc_init_num, (bc_num *num TSRMLS_DC));
  83. _PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale TSRMLS_DC));
  84. _PROTOTYPE(char *bc_num2str, (bc_num num));
  85. _PROTOTYPE(void bc_int2num, (bc_num *num, int val));
  86. _PROTOTYPE(long bc_num2long, (bc_num num));
  87. _PROTOTYPE(int bc_compare, (bc_num n1, bc_num n2));
  88. _PROTOTYPE(char bc_is_zero, (bc_num num TSRMLS_DC));
  89. _PROTOTYPE(char bc_is_near_zero, (bc_num num, int scale));
  90. _PROTOTYPE(char bc_is_neg, (bc_num num));
  91. _PROTOTYPE(void bc_add, (bc_num n1, bc_num n2, bc_num *result, int scale_min));
  92. _PROTOTYPE(void bc_sub, (bc_num n1, bc_num n2, bc_num *result, int scale_min));
  93. _PROTOTYPE(void bc_multiply, (bc_num n1, bc_num n2, bc_num *prod, int scale TSRMLS_DC));
  94. _PROTOTYPE(int bc_divide, (bc_num n1, bc_num n2, bc_num *quot, int scale TSRMLS_DC));
  95. _PROTOTYPE(int bc_modulo, (bc_num num1, bc_num num2, bc_num *result,
  96. int scale TSRMLS_DC));
  97. _PROTOTYPE(int bc_divmod, (bc_num num1, bc_num num2, bc_num *quot,
  98. bc_num *rem, int scale TSRMLS_DC));
  99. _PROTOTYPE(int bc_raisemod, (bc_num base, bc_num expo, bc_num mod,
  100. bc_num *result, int scale TSRMLS_DC));
  101. _PROTOTYPE(void bc_raise, (bc_num num1, bc_num num2, bc_num *result,
  102. int scale TSRMLS_DC));
  103. _PROTOTYPE(int bc_sqrt, (bc_num *num, int scale TSRMLS_DC));
  104. _PROTOTYPE(void bc_out_num, (bc_num num, int o_base, void (* out_char)(int),
  105. int leading_zero TSRMLS_DC));
  106. /* Prototypes needed for external utility routines. */
  107. _PROTOTYPE(void bc_rt_warn, (char *mesg ,...));
  108. _PROTOTYPE(void bc_rt_error, (char *mesg ,...));
  109. _PROTOTYPE(void bc_out_of_memory, (void));
  110. #define bc_new_num(length, scale) _bc_new_num_ex((length), (scale), 0)
  111. #define bc_free_num(num) _bc_free_num_ex((num), 0)
  112. #endif