Back to index

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