xref: /PHP-8.3/ext/bcmath/libbcmath/src/compare.c (revision e56ed6e1)
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,bool use_sign,bool ignore_last)42 int _bc_do_compare(bc_num n1, bc_num n2, bool use_sign, bool ignore_last)
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 (1);
51 		} else {
52 			/* Negative N1 < Positive N1 */
53 			return (-1);
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 (1);
63 			} else {
64 				return (-1);
65 			}
66 		} else {
67 			/* Magnitude of n1 < n2. */
68 			if (!use_sign || n1->n_sign == PLUS) {
69 				return (-1);
70 			} else {
71 				return (1);
72 			}
73 		}
74 	}
75 
76 	/* If we get here, they have the same number of integer digits.
77 	   check the integer part and the equal length part of the fraction. */
78 	size_t count = n1->n_len + MIN (n1->n_scale, n2->n_scale);
79 	n1ptr = n1->n_value;
80 	n2ptr = n2->n_value;
81 
82 	while ((count > 0) && (*n1ptr == *n2ptr)) {
83 		n1ptr++;
84 		n2ptr++;
85 		count--;
86 	}
87 
88 	if (ignore_last && count == 1 && n1->n_scale == n2->n_scale) {
89 		return (0);
90 	}
91 	if (count != 0) {
92 		if (*n1ptr > *n2ptr) {
93 			/* Magnitude of n1 > n2. */
94 			if (!use_sign || n1->n_sign == PLUS) {
95 				return (1);
96 			} else {
97 				return (-1);
98 			}
99 		} else {
100 			/* Magnitude of n1 < n2. */
101 			if (!use_sign || n1->n_sign == PLUS) {
102 				return (-1);
103 			} else {
104 				return (1);
105 			}
106 		}
107 	}
108 
109 	/* They are equal up to the last part of the equal part of the fraction. */
110 	if (n1->n_scale != n2->n_scale) {
111 		if (n1->n_scale > n2->n_scale) {
112 			for (count = n1->n_scale - n2->n_scale; count > 0; count--) {
113 				if (*n1ptr++ != 0) {
114 					/* Magnitude of n1 > n2. */
115 					if (!use_sign || n1->n_sign == PLUS) {
116 						return (1);
117 					} else {
118 						return (-1);
119 					}
120 				}
121 			}
122 		} else {
123 			for (count = n2->n_scale - n1->n_scale; count > 0; count--) {
124 				if (*n2ptr++ != 0) {
125 					/* Magnitude of n1 < n2. */
126 					if (!use_sign || n1->n_sign == PLUS) {
127 						return (-1);
128 					} else {
129 						return (1);
130 					}
131 				}
132 			}
133 		}
134 	}
135 
136 	/* They must be equal! */
137 	return (0);
138 }
139 
140 
141 /* This is the "user callable" routine to compare numbers N1 and N2. */
bc_compare(bc_num n1,bc_num n2)142 int bc_compare(bc_num n1, bc_num n2)
143 {
144 	return _bc_do_compare(n1, n2, true, false);
145 }
146