Back to index

php5  5.3.10
raise.c
Go to the documentation of this file.
00001 /* raise.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 /* Raise NUM1 to the NUM2 power.  The result is placed in RESULT.
00043    Maximum exponent is LONG_MAX.  If a NUM2 is not an integer,
00044    only the integer part is used.  */
00045 
00046 void
00047 bc_raise (bc_num num1, bc_num num2, bc_num *result, int scale TSRMLS_DC)
00048 {
00049    bc_num temp, power;
00050    long exponent;
00051    int rscale;
00052    int pwrscale;
00053    int calcscale;
00054    char neg;
00055 
00056    /* Check the exponent for scale digits and convert to a long. */
00057    if (num2->n_scale != 0)
00058      bc_rt_warn ("non-zero scale in exponent");
00059    exponent = bc_num2long (num2);
00060    if (exponent == 0 && (num2->n_len > 1 || num2->n_value[0] != 0))
00061        bc_rt_error ("exponent too large in raise");
00062 
00063    /* Special case if exponent is a zero. */
00064    if (exponent == 0)
00065      {
00066        bc_free_num (result);
00067        *result = bc_copy_num (BCG(_one_));
00068        return;
00069      }
00070 
00071    /* Other initializations. */
00072    if (exponent < 0)
00073      {
00074        neg = TRUE;
00075        exponent = -exponent;
00076        rscale = scale;
00077      }
00078    else
00079      {
00080        neg = FALSE;
00081        rscale = MIN (num1->n_scale*exponent, MAX(scale, num1->n_scale));
00082      }
00083 
00084    /* Set initial value of temp.  */
00085    power = bc_copy_num (num1);
00086    pwrscale = num1->n_scale;
00087    while ((exponent & 1) == 0)
00088      {
00089        pwrscale = 2*pwrscale;
00090        bc_multiply (power, power, &power, pwrscale TSRMLS_CC);
00091        exponent = exponent >> 1;
00092      }
00093    temp = bc_copy_num (power);
00094    calcscale = pwrscale;
00095    exponent = exponent >> 1;
00096 
00097    /* Do the calculation. */
00098    while (exponent > 0)
00099      {
00100        pwrscale = 2*pwrscale;
00101        bc_multiply (power, power, &power, pwrscale TSRMLS_CC);
00102        if ((exponent & 1) == 1) {
00103         calcscale = pwrscale + calcscale;
00104         bc_multiply (temp, power, &temp, calcscale TSRMLS_CC);
00105        }
00106        exponent = exponent >> 1;
00107      }
00108 
00109    /* Assign the value. */
00110    if (neg)
00111      {
00112        bc_divide (BCG(_one_), temp, result, rscale TSRMLS_CC);
00113        bc_free_num (&temp);
00114      }
00115    else
00116      {
00117        bc_free_num (result);
00118        *result = temp;
00119        if ((*result)->n_scale > rscale)
00120         (*result)->n_scale = rscale;
00121      }
00122    bc_free_num (&power);
00123 }
00124