Back to index

php5  5.3.10
raisemod.c
Go to the documentation of this file.
00001 /* raisemod.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 /* Raise BASE to the EXPO power, reduced modulo MOD.  The result is
00042    placed in RESULT.  If a EXPO is not an integer,
00043    only the integer part is used.  */
00044 
00045 int
00046 bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale TSRMLS_DC)
00047 {
00048   bc_num power, exponent, parity, temp;
00049   int rscale;
00050 
00051   /* Check for correct numbers. */
00052   if (bc_is_zero(mod TSRMLS_CC)) return -1;
00053   if (bc_is_neg(expo)) return -1;
00054 
00055   /* Set initial values.  */
00056   power = bc_copy_num (base);
00057   exponent = bc_copy_num (expo);
00058   temp = bc_copy_num (BCG(_one_));
00059   bc_init_num(&parity TSRMLS_CC);
00060 
00061   /* Check the base for scale digits. */
00062   if (base->n_scale != 0)
00063       bc_rt_warn ("non-zero scale in base");
00064 
00065   /* Check the exponent for scale digits. */
00066   if (exponent->n_scale != 0)
00067     {
00068       bc_rt_warn ("non-zero scale in exponent");
00069       bc_divide (exponent, BCG(_one_), &exponent, 0 TSRMLS_CC); /*truncate */
00070     }
00071 
00072   /* Check the modulus for scale digits. */
00073   if (mod->n_scale != 0)
00074       bc_rt_warn ("non-zero scale in modulus");
00075 
00076   /* Do the calculation. */
00077   rscale = MAX(scale, base->n_scale);
00078   while ( !bc_is_zero(exponent TSRMLS_CC) )
00079     {
00080       (void) bc_divmod (exponent, BCG(_two_), &exponent, &parity, 0 TSRMLS_CC);
00081       if ( !bc_is_zero(parity TSRMLS_CC) )
00082        {
00083          bc_multiply (temp, power, &temp, rscale TSRMLS_CC);
00084          (void) bc_modulo (temp, mod, &temp, scale TSRMLS_CC);
00085        }
00086 
00087       bc_multiply (power, power, &power, rscale TSRMLS_CC);
00088       (void) bc_modulo (power, mod, &power, scale TSRMLS_CC);
00089     }
00090 
00091   /* Assign the value. */
00092   bc_free_num (&power);
00093   bc_free_num (&exponent);
00094   bc_free_num (result);
00095   bc_free_num (&parity);
00096   *result = temp;
00097   return 0;   /* Everything is OK. */
00098 }