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
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library 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 GNU
14 Lesser General Public License for more details. (LICENSE)
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to:
18
19 The Free Software Foundation, Inc.
20 59 Temple Place, Suite 330
21 Boston, MA 02111-1307 USA.
22
23 You may contact the author by:
24 e-mail: philnelson@acm.org
25 us-mail: Philip A. Nelson
26 Computer Science Department, 9062
27 Western Washington University
28 Bellingham, WA 98226-9062
29
30 *************************************************************************/
31
32 #ifndef _BCMATH_H_
33 #define _BCMATH_H_
34
35 #include <stddef.h>
36
37 typedef enum {PLUS, MINUS} sign;
38
39 typedef struct bc_struct *bc_num;
40
41 typedef struct bc_struct {
42 size_t n_len; /* The number of digits before the decimal point. */
43 size_t n_scale; /* The number of digits after the decimal point. */
44 char *n_value; /* The number. Not zero char terminated. */
45 unsigned int n_refs; /* The number of pointers to this number. */
46 sign n_sign;
47 } bc_struct;
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include "zend.h"
54 #include <stdbool.h>
55 #include "zend_string.h"
56
57 /* Needed for BCG() macro and PHP_ROUND_XXX */
58 #include "../../php_bcmath.h"
59
60 /* The base used in storing the numbers in n_value above.
61 Currently, this MUST be 10. */
62
63 #define BASE 10
64
65 /* Some useful macros and constants. */
66
67 #define CH_VAL(c) (c - '0')
68 #define BCD_CHAR(d) (d + '0')
69
70 #ifdef MIN
71 #undef MIN
72 #undef MAX
73 #endif
74 #define MAX(a, b) ((a)>(b)?(a):(b))
75 #define MIN(a, b) ((a)>(b)?(b):(a))
76
77 /* Function Prototypes */
78
79 void bc_init_numbers(void);
80
81 bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent);
82
83 bc_num _bc_new_num_nonzeroed_ex(size_t length, size_t scale, bool persistent);
84
85 void _bc_free_num_ex(bc_num *num, bool persistent);
86
87 /* Make a copy of a number! Just increments the reference count! */
bc_copy_num(bc_num num)88 static inline bc_num bc_copy_num(bc_num num)
89 {
90 num->n_refs++;
91 return num;
92 }
93
94 void bc_init_num(bc_num *num);
95
96 bool bc_str2num(bc_num *num, const char *str, const char *end, size_t scale, size_t *full_scale, bool auto_scale);
97
98 bc_num bc_long2num(zend_long lval);
99
100 zend_string *bc_num2str_ex(bc_num num, size_t scale);
101
102 void bc_int2num(bc_num *num, int val);
103
104 long bc_num2long(bc_num num);
105
106 typedef enum {
107 BCMATH_EQUAL = 0,
108 BCMATH_LEFT_GREATER = 1,
109 BCMATH_RIGHT_GREATER = -1
110 } bcmath_compare_result;
111
112 bcmath_compare_result bc_compare(bc_num n1, bc_num n2, size_t scale);
113
114 bool bc_is_zero(bc_num num);
115
116 bool bc_is_zero_for_scale(bc_num num, size_t scale);
117
118 bool bc_is_near_zero(bc_num num, size_t scale);
119
120 bool bc_is_neg(bc_num num);
121
122 void bc_rm_trailing_zeros(bc_num num);
123
124 bc_num bc_add(bc_num n1, bc_num n2, size_t scale_min);
125
126 #define bc_add_ex(n1, n2, result, scale_min) do { \
127 bc_num add_ex = bc_add(n1, n2, scale_min); \
128 bc_free_num (result); \
129 *(result) = add_ex; \
130 } while (0)
131
132 bc_num bc_sub(bc_num n1, bc_num n2, size_t scale_min);
133
134 #define bc_sub_ex(n1, n2, result, scale_min) do { \
135 bc_num sub_ex = bc_sub(n1, n2, scale_min); \
136 bc_free_num (result); \
137 *(result) = sub_ex; \
138 } while (0)
139
140 bc_num bc_multiply(bc_num n1, bc_num n2, size_t scale);
141
142 #define bc_multiply_ex(n1, n2, result, scale_min) do { \
143 bc_num mul_ex = bc_multiply(n1, n2, scale_min); \
144 bc_free_num (result); \
145 *(result) = mul_ex; \
146 } while (0)
147
148 bc_num bc_square(bc_num n1, size_t scale);
149
150 bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, size_t scale);
151
152 bool bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale);
153
154 bool bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale);
155
156 bc_num bc_floor_or_ceil(bc_num num, bool is_floor);
157
158 void bc_round(bc_num num, zend_long places, zend_long mode, bc_num *result);
159
160 typedef enum {
161 OK,
162 BASE_HAS_FRACTIONAL,
163 EXPO_HAS_FRACTIONAL,
164 EXPO_IS_NEGATIVE,
165 MOD_HAS_FRACTIONAL,
166 MOD_IS_ZERO
167 } raise_mod_status;
168
169 raise_mod_status bc_raisemod(bc_num base, bc_num exponent, bc_num mod, bc_num *result, size_t scale);
170
171 bool bc_raise(bc_num base, long exponent, bc_num *result, size_t scale);
172
173 void bc_raise_bc_exponent(bc_num base, bc_num exponent, bc_num *resul, size_t scale);
174
175 bool bc_sqrt(bc_num *num, size_t scale);
176
177 /* Prototypes needed for external utility routines. */
178 #define bc_new_num(length, scale) _bc_new_num_ex((length), (scale), 0)
179 #define bc_new_num_nonzeroed(length, scale) _bc_new_num_nonzeroed_ex((length), (scale), 0)
180 #define bc_free_num(num) _bc_free_num_ex((num), 0)
181 #define bc_num2str(num) bc_num2str_ex((num), (num->n_scale))
182
183 #endif
184