xref: /PHP-8.0/ext/bcmath/libbcmath/src/raise.c (revision cd05b56a)
1 /* raise.c: bcmath library file. */
2 /*
3     Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
4     Copyright (C) 2000 Philip A. Nelson
5 
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2 of the License, or (at your option) any later version.
10 
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Lesser General Public License for more details.  (LICENSE)
15 
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to:
18 
19       The Free Software Foundation, Inc.
20       59 Temple Place, Suite 330
21       Boston, MA 02111-1307 USA.
22 
23     You may contact the author by:
24        e-mail:  philnelson@acm.org
25       us-mail:  Philip A. Nelson
26                 Computer Science Department, 9062
27                 Western Washington University
28                 Bellingham, WA 98226-9062
29 
30 *************************************************************************/
31 
32 #include <config.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <stdarg.h>
37 #include "bcmath.h"
38 #include "private.h"
39 
40 
41 /* Raise NUM1 to the NUM2 power.  The result is placed in RESULT.
42    Maximum exponent is LONG_MAX.  If a NUM2 is not an integer,
43    only the integer part is used.  */
44 
45 void
bc_raise(bc_num num1,bc_num num2,bc_num * result,int scale)46 bc_raise (bc_num num1, bc_num num2, bc_num *result, int scale)
47 {
48    bc_num temp, power;
49    long exponent;
50    int rscale;
51    int pwrscale;
52    int calcscale;
53    char neg;
54 
55 	/* Check the exponent for scale digits and convert to a long. */
56 	if (num2->n_scale != 0) {
57 		/* 2nd argument from PHP_FUNCTION(bcpow) */
58 		zend_argument_value_error(2, "cannot have a fractional part");
59 		return;
60 	}
61 	exponent = bc_num2long (num2);
62 	if (exponent == 0 && (num2->n_len > 1 || num2->n_value[0] != 0)) {
63 		/* 2nd argument from PHP_FUNCTION(bcpow) */
64 		zend_argument_value_error(2, "is too large");
65 		return;
66 	}
67 
68    /* Special case if exponent is a zero. */
69    if (exponent == 0)
70      {
71        bc_free_num (result);
72        *result = bc_copy_num (BCG(_one_));
73        return;
74      }
75 
76    /* Other initializations. */
77    if (exponent < 0)
78      {
79        neg = TRUE;
80        exponent = -exponent;
81        rscale = scale;
82      }
83    else
84      {
85        neg = FALSE;
86        rscale = MIN (num1->n_scale*exponent, MAX(scale, num1->n_scale));
87      }
88 
89    /* Set initial value of temp.  */
90    power = bc_copy_num (num1);
91    pwrscale = num1->n_scale;
92    while ((exponent & 1) == 0)
93      {
94        pwrscale = 2*pwrscale;
95        bc_multiply (power, power, &power, pwrscale);
96        exponent = exponent >> 1;
97      }
98    temp = bc_copy_num (power);
99    calcscale = pwrscale;
100    exponent = exponent >> 1;
101 
102    /* Do the calculation. */
103    while (exponent > 0)
104      {
105        pwrscale = 2*pwrscale;
106        bc_multiply (power, power, &power, pwrscale);
107        if ((exponent & 1) == 1) {
108 	 calcscale = pwrscale + calcscale;
109 	 bc_multiply (temp, power, &temp, calcscale);
110        }
111        exponent = exponent >> 1;
112      }
113 
114    /* Assign the value. */
115    if (neg)
116      {
117        bc_divide (BCG(_one_), temp, result, rscale);
118        bc_free_num (&temp);
119      }
120    else
121      {
122        bc_free_num (result);
123        *result = temp;
124        if ((*result)->n_scale > rscale)
125 	 (*result)->n_scale = rscale;
126      }
127    bc_free_num (&power);
128 }
129