39
39
than N2 and +1 if N1 is greater than N2. If USE_SIGN is false, just
40
40
compare the magnitudes. */
41
41
42
- bcmath_compare_result _bc_do_compare (bc_num n1 , bc_num n2 , bool use_sign )
42
+ bcmath_compare_result _bc_do_compare (bc_num n1 , bc_num n2 , size_t scale , bool use_sign )
43
43
{
44
44
char * n1ptr , * n2ptr ;
45
45
@@ -73,9 +73,12 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, bool use_sign)
73
73
}
74
74
}
75
75
76
+ size_t n1_scale = MIN (n1 -> n_scale , scale );
77
+ size_t n2_scale = MIN (n2 -> n_scale , scale );
78
+
76
79
/* If we get here, they have the same number of integer digits.
77
80
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 );
81
+ size_t count = n1 -> n_len + MIN (n1_scale , n2_scale );
79
82
n1ptr = n1 -> n_value ;
80
83
n2ptr = n2 -> n_value ;
81
84
@@ -104,9 +107,9 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, bool use_sign)
104
107
}
105
108
106
109
/* They are equal up to the last part of the equal part of the fraction. */
107
- if (n1 -> n_scale != n2 -> n_scale ) {
108
- if (n1 -> n_scale > n2 -> n_scale ) {
109
- for (count = n1 -> n_scale - n2 -> n_scale ; count > 0 ; count -- ) {
110
+ if (n1_scale != n2_scale ) {
111
+ if (n1_scale > n2_scale ) {
112
+ for (count = n1_scale - n2_scale ; count > 0 ; count -- ) {
110
113
if (* n1ptr ++ != 0 ) {
111
114
/* Magnitude of n1 > n2. */
112
115
if (!use_sign || n1 -> n_sign == PLUS ) {
@@ -117,7 +120,7 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, bool use_sign)
117
120
}
118
121
}
119
122
} else {
120
- for (count = n2 -> n_scale - n1 -> n_scale ; count > 0 ; count -- ) {
123
+ for (count = n2_scale - n1_scale ; count > 0 ; count -- ) {
121
124
if (* n2ptr ++ != 0 ) {
122
125
/* Magnitude of n1 < n2. */
123
126
if (!use_sign || n1 -> n_sign == PLUS ) {
@@ -136,7 +139,7 @@ bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, bool use_sign)
136
139
137
140
138
141
/* This is the "user callable" routine to compare numbers N1 and N2. */
139
- bcmath_compare_result bc_compare (bc_num n1 , bc_num n2 )
142
+ bcmath_compare_result bc_compare (bc_num n1 , bc_num n2 , size_t scale )
140
143
{
141
- return _bc_do_compare (n1 , n2 , true);
144
+ return _bc_do_compare (n1 , n2 , scale , true);
142
145
}
0 commit comments