Back to index

php5  5.3.10
divmod.c
Go to the documentation of this file.
00001 /* divmod.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 /* Division *and* modulo for numbers.  This computes both NUM1 / NUM2 and
00043    NUM1 % NUM2  and puts the results in QUOT and REM, except that if QUOT
00044    is NULL then that store will be omitted.
00045  */
00046 
00047 int
00048 bc_divmod (bc_num num1, bc_num num2, bc_num *quot, bc_num *rem, int scale TSRMLS_DC)
00049 {
00050   bc_num quotient = NULL;
00051   bc_num temp;
00052   int rscale;
00053 
00054   /* Check for correct numbers. */
00055   if (bc_is_zero (num2 TSRMLS_CC)) return -1;
00056 
00057   /* Calculate final scale. */
00058   rscale = MAX (num1->n_scale, num2->n_scale+scale);
00059   bc_init_num(&temp TSRMLS_CC);
00060 
00061   /* Calculate it. */
00062   bc_divide (num1, num2, &temp, scale TSRMLS_CC);
00063   if (quot)
00064     quotient = bc_copy_num (temp);
00065   bc_multiply (temp, num2, &temp, rscale TSRMLS_CC);
00066   bc_sub (num1, temp, rem, rscale);
00067   bc_free_num (&temp);
00068 
00069   if (quot)
00070     {
00071       bc_free_num (quot);
00072       *quot = quotient;
00073     }
00074 
00075   return 0;   /* Everything is OK. */
00076 }
00077 
00078 
00079 /* Modulo for numbers.  This computes NUM1 % NUM2  and puts the
00080    result in RESULT.   */
00081 
00082 int
00083 bc_modulo (bc_num num1, bc_num num2, bc_num *result, int scale TSRMLS_DC)
00084 {
00085   return bc_divmod (num1, num2, NULL, result, scale TSRMLS_CC);
00086 }
00087