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