Back to index

php5  5.3.10
sub.c
Go to the documentation of this file.
00001 /* sub.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 /* Here is the full subtract routine that takes care of negative numbers.
00043    N2 is subtracted from N1 and the result placed in RESULT.  SCALE_MIN
00044    is the minimum scale for the result. */
00045 
00046 void
00047 bc_sub (n1, n2, result, scale_min)
00048      bc_num n1, n2, *result;
00049      int scale_min;
00050 {
00051   bc_num diff = NULL;
00052   int cmp_res;
00053   int res_scale;
00054 
00055   if (n1->n_sign != n2->n_sign)
00056     {
00057       diff = _bc_do_add (n1, n2, scale_min);
00058       diff->n_sign = n1->n_sign;
00059     }
00060   else
00061     {
00062       /* subtraction must be done. */
00063       /* Compare magnitudes. */
00064       cmp_res = _bc_do_compare (n1, n2, FALSE, FALSE);
00065       switch (cmp_res)
00066        {
00067        case -1:
00068          /* n1 is less than n2, subtract n1 from n2. */
00069          diff = _bc_do_sub (n2, n1, scale_min);
00070          diff->n_sign = (n2->n_sign == PLUS ? MINUS : PLUS);
00071          break;
00072        case  0:
00073          /* They are equal! return zero! */
00074          res_scale = MAX (scale_min, MAX(n1->n_scale, n2->n_scale));
00075          diff = bc_new_num (1, res_scale);
00076          memset (diff->n_value, 0, res_scale+1);
00077          break;
00078        case  1:
00079          /* n2 is less than n1, subtract n2 from n1. */
00080          diff = _bc_do_sub (n1, n2, scale_min);
00081          diff->n_sign = n1->n_sign;
00082          break;
00083        }
00084     }
00085 
00086   /* Clean up and return. */
00087   bc_free_num (result);
00088   *result = diff;
00089 }
00090