Back to index

php5  5.3.10
compare.c
Go to the documentation of this file.
00001 /* compare.c: bcmath library file. */
00002 /*
00003     Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
00004     Copyright (C) 2000 Philip A. Nelson
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.  (COPYING.LIB)
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to:
00018 
00019       The Free Software Foundation, Inc.
00020       59 Temple Place, Suite 330
00021       Boston, MA 02111-1307 USA.
00022 
00023     You may contact the author by:
00024        e-mail:  philnelson@acm.org
00025       us-mail:  Philip A. Nelson
00026                 Computer Science Department, 9062
00027                 Western Washington University
00028                 Bellingham, WA 98226-9062
00029        
00030 *************************************************************************/
00031 
00032 #include <config.h>
00033 #include <stdio.h>
00034 #include <assert.h>
00035 #include <stdlib.h>
00036 #include <ctype.h>
00037 #include <stdarg.h>
00038 #include "bcmath.h"
00039 #include "private.h"
00040 
00041 
00042 /* Compare two bc numbers.  Return value is 0 if equal, -1 if N1 is less
00043    than N2 and +1 if N1 is greater than N2.  If USE_SIGN is false, just
00044    compare the magnitudes. */
00045 
00046  int
00047 _bc_do_compare (n1, n2, use_sign, ignore_last)
00048      bc_num n1, n2;
00049      int use_sign;
00050      int ignore_last;
00051 {
00052   char *n1ptr, *n2ptr;
00053   int  count;
00054 
00055   /* First, compare signs. */
00056   if (use_sign && n1->n_sign != n2->n_sign)
00057     {
00058       if (n1->n_sign == PLUS)
00059        return (1);   /* Positive N1 > Negative N2 */
00060       else
00061        return (-1);  /* Negative N1 < Positive N1 */
00062     }
00063 
00064   /* Now compare the magnitude. */
00065   if (n1->n_len != n2->n_len)
00066     {
00067       if (n1->n_len > n2->n_len)
00068        {
00069          /* Magnitude of n1 > n2. */
00070          if (!use_sign || n1->n_sign == PLUS)
00071            return (1);
00072          else
00073            return (-1);
00074        }
00075       else
00076        {
00077          /* Magnitude of n1 < n2. */
00078          if (!use_sign || n1->n_sign == PLUS)
00079            return (-1);
00080          else
00081            return (1);
00082        }
00083     }
00084 
00085   /* If we get here, they have the same number of integer digits.
00086      check the integer part and the equal length part of the fraction. */
00087   count = n1->n_len + MIN (n1->n_scale, n2->n_scale);
00088   n1ptr = n1->n_value;
00089   n2ptr = n2->n_value;
00090 
00091   while ((count > 0) && (*n1ptr == *n2ptr))
00092     {
00093       n1ptr++;
00094       n2ptr++;
00095       count--;
00096     }
00097   if (ignore_last && count == 1 && n1->n_scale == n2->n_scale)
00098     return (0);
00099   if (count != 0)
00100     {
00101       if (*n1ptr > *n2ptr)
00102        {
00103          /* Magnitude of n1 > n2. */
00104          if (!use_sign || n1->n_sign == PLUS)
00105            return (1);
00106          else
00107            return (-1);
00108        }
00109       else
00110        {
00111          /* Magnitude of n1 < n2. */
00112          if (!use_sign || n1->n_sign == PLUS)
00113            return (-1);
00114          else
00115            return (1);
00116        }
00117     }
00118 
00119   /* They are equal up to the last part of the equal part of the fraction. */
00120   if (n1->n_scale != n2->n_scale)
00121     {
00122       if (n1->n_scale > n2->n_scale)
00123        {
00124          for (count = n1->n_scale-n2->n_scale; count>0; count--)
00125            if (*n1ptr++ != 0)
00126              {
00127               /* Magnitude of n1 > n2. */
00128               if (!use_sign || n1->n_sign == PLUS)
00129                 return (1);
00130               else
00131                 return (-1);
00132              }
00133        }
00134       else
00135        {
00136          for (count = n2->n_scale-n1->n_scale; count>0; count--)
00137            if (*n2ptr++ != 0)
00138              {
00139               /* Magnitude of n1 < n2. */
00140               if (!use_sign || n1->n_sign == PLUS)
00141                 return (-1);
00142               else
00143                 return (1);
00144              }
00145        }
00146     }
00147 
00148   /* They must be equal! */
00149   return (0);
00150 }
00151 
00152 
00153 /* This is the "user callable" routine to compare numbers N1 and N2. */
00154 
00155 int
00156 bc_compare (n1, n2)
00157      bc_num n1, n2;
00158 {
00159   return _bc_do_compare (n1, n2, TRUE, FALSE);
00160 }
00161