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 /*
49 * scale and n->n_scale differ only in Number objects.
50 * This happens when explicitly specify the scale in a Number method.
51 */
52 if ((n1->n_scale > scale || n2->n_scale > scale) &&
53 n1->n_len == 1 && n2->n_len == 1 &&
54 n1->n_value[0] == 0 && n2->n_value[0] == 0 &&
55 bc_is_zero_for_scale(n1, scale) && bc_is_zero_for_scale(n2, scale)
56 ) {
57 /* e.g. 0.00 <=> -0.00 */
58 return BCMATH_EQUAL;
59 }
60 if (n1->n_sign == PLUS) {
61 /* Positive N1 > Negative N2 */
62 return BCMATH_LEFT_GREATER;
63 } else {
64 /* Negative N1 < Positive N1 */
65 return BCMATH_RIGHT_GREATER;
66 }
67 }
68
69 /* Now compare the magnitude. */
70 if (n1->n_len != n2->n_len) {
71 if (n1->n_len > n2->n_len) {
72 /* Magnitude of n1 > n2. */
73 if (!use_sign || n1->n_sign == PLUS) {
74 return BCMATH_LEFT_GREATER;
75 } else {
76 return BCMATH_RIGHT_GREATER;
77 }
78 } else {
79 /* Magnitude of n1 < n2. */
80 if (!use_sign || n1->n_sign == PLUS) {
81 return BCMATH_RIGHT_GREATER;
82 } else {
83 return BCMATH_LEFT_GREATER;
84 }
85 }
86 }
87
88 size_t n1_scale = MIN(n1->n_scale, scale);
89 size_t n2_scale = MIN(n2->n_scale, scale);
90
91 /* If we get here, they have the same number of integer digits.
92 check the integer part and the equal length part of the fraction. */
93 size_t count = n1->n_len + MIN (n1_scale, n2_scale);
94 n1ptr = n1->n_value;
95 n2ptr = n2->n_value;
96
97 while ((count > 0) && (*n1ptr == *n2ptr)) {
98 n1ptr++;
99 n2ptr++;
100 count--;
101 }
102
103 if (count != 0) {
104 if (*n1ptr > *n2ptr) {
105 /* Magnitude of n1 > n2. */
106 if (!use_sign || n1->n_sign == PLUS) {
107 return BCMATH_LEFT_GREATER;
108 } else {
109 return BCMATH_RIGHT_GREATER;
110 }
111 } else {
112 /* Magnitude of n1 < n2. */
113 if (!use_sign || n1->n_sign == PLUS) {
114 return BCMATH_RIGHT_GREATER;
115 } else {
116 return BCMATH_LEFT_GREATER;
117 }
118 }
119 }
120
121 /* They are equal up to the last part of the equal part of the fraction. */
122 if (n1_scale != n2_scale) {
123 if (n1_scale > n2_scale) {
124 for (count = n1_scale - n2_scale; count > 0; count--) {
125 if (*n1ptr++ != 0) {
126 /* Magnitude of n1 > n2. */
127 if (!use_sign || n1->n_sign == PLUS) {
128 return BCMATH_LEFT_GREATER;
129 } else {
130 return BCMATH_RIGHT_GREATER;
131 }
132 }
133 }
134 } else {
135 for (count = n2_scale - n1_scale; count > 0; count--) {
136 if (*n2ptr++ != 0) {
137 /* Magnitude of n1 < n2. */
138 if (!use_sign || n1->n_sign == PLUS) {
139 return BCMATH_RIGHT_GREATER;
140 } else {
141 return BCMATH_LEFT_GREATER;
142 }
143 }
144 }
145 }
146 }
147
148 /* They must be equal! */
149 return BCMATH_EQUAL;
150 }
151
152
153 /* This is the "user callable" routine to compare numbers N1 and N2. */
bc_compare(bc_num n1,bc_num n2,size_t scale)154 bcmath_compare_result bc_compare(bc_num n1, bc_num n2, size_t scale)
155 {
156 return _bc_do_compare(n1, n2, scale, true);
157 }
158