xref: /php-src/ext/bcmath/libbcmath/src/compare.c (revision be4b10e9)
1 /* compare.c: bcmath library file. */
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 #include <stdbool.h>
33 #include "bcmath.h"
34 #include "private.h"
35 #include <stddef.h>
36 
37 
38 /* Compare two bc numbers.  Return value is 0 if equal, -1 if N1 is less
39    than N2 and +1 if N1 is greater than N2.  If USE_SIGN is false, just
40    compare the magnitudes. */
41 
_bc_do_compare(bc_num n1,bc_num n2,size_t scale,bool use_sign)42 bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool use_sign)
43 {
44 	char *n1ptr, *n2ptr;
45 
46 	/* First, compare signs. */
47 	if (use_sign && n1->n_sign != n2->n_sign) {
48 		if (n1->n_sign == PLUS) {
49 			/* Positive N1 > Negative N2 */
50 			return BCMATH_LEFT_GREATER;
51 		} else {
52 			/* Negative N1 < Positive N1 */
53 			return BCMATH_RIGHT_GREATER;
54 		}
55 	}
56 
57 	/* Now compare the magnitude. */
58 	if (n1->n_len != n2->n_len) {
59 		if (n1->n_len > n2->n_len) {
60 			/* Magnitude of n1 > n2. */
61 			if (!use_sign || n1->n_sign == PLUS) {
62 				return BCMATH_LEFT_GREATER;
63 			} else {
64 				return BCMATH_RIGHT_GREATER;
65 			}
66 		} else {
67 			/* Magnitude of n1 < n2. */
68 			if (!use_sign || n1->n_sign == PLUS) {
69 				return BCMATH_RIGHT_GREATER;
70 			} else {
71 				return BCMATH_LEFT_GREATER;
72 			}
73 		}
74 	}
75 
76 	size_t n1_scale = MIN(n1->n_scale, scale);
77 	size_t n2_scale = MIN(n2->n_scale, scale);
78 
79 	/* If we get here, they have the same number of integer digits.
80 	   check the integer part and the equal length part of the fraction. */
81 	size_t count = n1->n_len + MIN (n1_scale, n2_scale);
82 	n1ptr = n1->n_value;
83 	n2ptr = n2->n_value;
84 
85 	while ((count > 0) && (*n1ptr == *n2ptr)) {
86 		n1ptr++;
87 		n2ptr++;
88 		count--;
89 	}
90 
91 	if (count != 0) {
92 		if (*n1ptr > *n2ptr) {
93 			/* Magnitude of n1 > n2. */
94 			if (!use_sign || n1->n_sign == PLUS) {
95 				return BCMATH_LEFT_GREATER;
96 			} else {
97 				return BCMATH_RIGHT_GREATER;
98 			}
99 		} else {
100 			/* Magnitude of n1 < n2. */
101 			if (!use_sign || n1->n_sign == PLUS) {
102 				return BCMATH_RIGHT_GREATER;
103 			} else {
104 				return BCMATH_LEFT_GREATER;
105 			}
106 		}
107 	}
108 
109 	/* They are equal up to the last part of the equal part of the fraction. */
110 	if (n1_scale != n2_scale) {
111 		if (n1_scale > n2_scale) {
112 			for (count = n1_scale - n2_scale; count > 0; count--) {
113 				if (*n1ptr++ != 0) {
114 					/* Magnitude of n1 > n2. */
115 					if (!use_sign || n1->n_sign == PLUS) {
116 						return BCMATH_LEFT_GREATER;
117 					} else {
118 						return BCMATH_RIGHT_GREATER;
119 					}
120 				}
121 			}
122 		} else {
123 			for (count = n2_scale - n1_scale; count > 0; count--) {
124 				if (*n2ptr++ != 0) {
125 					/* Magnitude of n1 < n2. */
126 					if (!use_sign || n1->n_sign == PLUS) {
127 						return BCMATH_RIGHT_GREATER;
128 					} else {
129 						return BCMATH_LEFT_GREATER;
130 					}
131 				}
132 			}
133 		}
134 	}
135 
136 	/* They must be equal! */
137 	return BCMATH_EQUAL;
138 }
139 
140 
141 /* This is the "user callable" routine to compare numbers N1 and N2. */
bc_compare(bc_num n1,bc_num n2,size_t scale)142 bcmath_compare_result bc_compare(bc_num n1, bc_num n2, size_t scale)
143 {
144 	return _bc_do_compare(n1, n2, scale, true);
145 }
146