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 php_error_docref (NULL, E_WARNING, "non-zero scale in exponent");
58 exponent = bc_num2long (num2);
59 if (exponent == 0 && (num2->n_len > 1 || num2->n_value[0] != 0))
60 php_error_docref (NULL, E_WARNING, "exponent too large");
61
62 /* Special case if exponent is a zero. */
63 if (exponent == 0)
64 {
65 bc_free_num (result);
66 *result = bc_copy_num (BCG(_one_));
67 return;
68 }
69
70 /* Other initializations. */
71 if (exponent < 0)
72 {
73 neg = TRUE;
74 exponent = -exponent;
75 rscale = scale;
76 }
77 else
78 {
79 neg = FALSE;
80 rscale = MIN (num1->n_scale*exponent, MAX(scale, num1->n_scale));
81 }
82
83 /* Set initial value of temp. */
84 power = bc_copy_num (num1);
85 pwrscale = num1->n_scale;
86 while ((exponent & 1) == 0)
87 {
88 pwrscale = 2*pwrscale;
89 bc_multiply (power, power, &power, pwrscale);
90 exponent = exponent >> 1;
91 }
92 temp = bc_copy_num (power);
93 calcscale = pwrscale;
94 exponent = exponent >> 1;
95
96 /* Do the calculation. */
97 while (exponent > 0)
98 {
99 pwrscale = 2*pwrscale;
100 bc_multiply (power, power, &power, pwrscale);
101 if ((exponent & 1) == 1) {
102 calcscale = pwrscale + calcscale;
103 bc_multiply (temp, power, &temp, calcscale);
104 }
105 exponent = exponent >> 1;
106 }
107
108 /* Assign the value. */
109 if (neg)
110 {
111 bc_divide (BCG(_one_), temp, result, rscale);
112 bc_free_num (&temp);
113 }
114 else
115 {
116 bc_free_num (result);
117 *result = temp;
118 if ((*result)->n_scale > rscale)
119 (*result)->n_scale = rscale;
120 }
121 bc_free_num (&power);
122 }
123