xref: /php-src/ext/bcmath/libbcmath/src/bcmath.h (revision e56ed6e1)
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 	sign   n_sign;
43 	size_t n_len;   /* The number of digits before the decimal point. */
44 	size_t n_scale; /* The number of digits after the decimal point. */
45 	int    n_refs;  /* The number of pointers to this number. */
46 	char  *n_ptr;   /* The pointer to the actual storage.
47 	                  If NULL, n_value points to the inside of another number
48 	                  (bc_multiply...) and should not be "freed." */
49 	char  *n_value; /* The number. Not zero char terminated.
50 	                   May not point to the same place as n_ptr as
51 	                   in the case of leading zeros generated. */
52 } bc_struct;
53 
54 #ifdef HAVE_CONFIG_H
55 #include "config.h"
56 #endif
57 
58 #include "zend.h"
59 #include <stdbool.h>
60 #include "zend_string.h"
61 #include "../../php_bcmath.h" /* Needed for BCG() macro */
62 
63 /* The base used in storing the numbers in n_value above.
64    Currently, this MUST be 10. */
65 
66 #define BASE 10
67 
68 /*  Some useful macros and constants. */
69 
70 #define CH_VAL(c)     (c - '0')
71 #define BCD_CHAR(d)   (d + '0')
72 
73 #ifdef MIN
74 #undef MIN
75 #undef MAX
76 #endif
77 #define MAX(a, b)      ((a)>(b)?(a):(b))
78 #define MIN(a, b)      ((a)>(b)?(b):(a))
79 #define ODD(a)        ((a)&1)
80 
81 #ifndef LONG_MAX
82 #define LONG_MAX 0x7ffffff
83 #endif
84 
85 
86 /* Function Prototypes */
87 
88 void bc_init_numbers(void);
89 
90 bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent);
91 
92 void _bc_free_num_ex(bc_num *num, bool persistent);
93 
94 bc_num bc_copy_num(bc_num num);
95 
96 void bc_init_num(bc_num *num);
97 
98 bool bc_str2num(bc_num *num, char *str, size_t scale);
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 int bc_compare(bc_num n1, bc_num n2);
107 
108 bool bc_is_zero(bc_num num);
109 
110 bool bc_is_zero_for_scale(bc_num num, size_t scale);
111 
112 bool bc_is_near_zero(bc_num num, size_t scale);
113 
114 bool bc_is_neg(bc_num num);
115 
116 void bc_add(bc_num n1, bc_num n2, bc_num *result, size_t scale_min);
117 
118 void bc_sub(bc_num n1, bc_num n2, bc_num *result, size_t scale_min);
119 
120 void bc_multiply(bc_num n1, bc_num n2, bc_num *prod, size_t scale);
121 
122 bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale);
123 
124 bool bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale);
125 
126 bool bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale);
127 
128 typedef enum {
129 	OK,
130 	BASE_HAS_FRACTIONAL,
131 	EXPO_HAS_FRACTIONAL,
132 	EXPO_IS_NEGATIVE,
133 	MOD_HAS_FRACTIONAL,
134 	MOD_IS_ZERO
135 } raise_mod_status;
136 
137 raise_mod_status bc_raisemod(bc_num base, bc_num exponent, bc_num mod, bc_num *result, size_t scale);
138 
139 void bc_raise(bc_num base, long exponent, bc_num *resul, size_t scale);
140 
141 void bc_raise_bc_exponent(bc_num base, bc_num exponent, bc_num *resul, size_t scale);
142 
143 bool bc_sqrt(bc_num *num, size_t scale);
144 
145 void bc_out_num(bc_num num, int o_base, void (* out_char)(char), bool leading_zero);
146 
147 /* Prototypes needed for external utility routines. */
148 #define bc_new_num(length, scale)	_bc_new_num_ex((length), (scale), 0)
149 #define bc_free_num(num)			_bc_free_num_ex((num), 0)
150 #define bc_num2str(num)				bc_num2str_ex((num), (num->n_scale))
151 
152 #endif
153