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 "bcmath.h"
33 #include <assert.h>
34 #include <stdbool.h>
35 #include <stddef.h>
36
37
38 /* Raise NUM1 to the NUM2 power. The result is placed in RESULT.
39 Maximum exponent is LONG_MAX. If a NUM2 is not an integer,
40 only the integer part is used. */
41
bc_raise(bc_num num1,long exponent,bc_num * result,size_t scale)42 void bc_raise(bc_num num1, long exponent, bc_num *result, size_t scale)
43 {
44 bc_num temp, power;
45 size_t rscale;
46 size_t pwrscale;
47 size_t calcscale;
48 bool is_neg;
49
50 /* Special case if exponent is a zero. */
51 if (exponent == 0) {
52 bc_free_num (result);
53 *result = bc_copy_num(BCG(_one_));
54 return;
55 }
56
57 /* Other initializations. */
58 if (exponent < 0) {
59 is_neg = true;
60 exponent = -exponent;
61 rscale = scale;
62 } else {
63 is_neg = false;
64 rscale = MIN (num1->n_scale * exponent, MAX(scale, num1->n_scale));
65 }
66
67 /* Set initial value of temp. */
68 power = bc_copy_num(num1);
69 pwrscale = num1->n_scale;
70 while ((exponent & 1) == 0) {
71 pwrscale = 2 * pwrscale;
72 bc_multiply(power, power, &power, pwrscale);
73 exponent = exponent >> 1;
74 }
75 temp = bc_copy_num(power);
76 calcscale = pwrscale;
77 exponent = exponent >> 1;
78
79 /* Do the calculation. */
80 while (exponent > 0) {
81 pwrscale = 2 * pwrscale;
82 bc_multiply(power, power, &power, pwrscale);
83 if ((exponent & 1) == 1) {
84 calcscale = pwrscale + calcscale;
85 bc_multiply(temp, power, &temp, calcscale);
86 }
87 exponent = exponent >> 1;
88 }
89
90 /* Assign the value. */
91 if (is_neg) {
92 bc_divide(BCG(_one_), temp, result, rscale);
93 bc_free_num (&temp);
94 } else {
95 bc_free_num (result);
96 *result = temp;
97 if ((*result)->n_scale > rscale) {
98 (*result)->n_scale = rscale;
99 }
100 }
101 bc_free_num (&power);
102 }
103
104 /* This is used internally by BCMath */
bc_raise_bc_exponent(bc_num base,bc_num expo,bc_num * result,size_t scale)105 void bc_raise_bc_exponent(bc_num base, bc_num expo, bc_num *result, size_t scale)
106 {
107 /* Exponent must not have fractional part */
108 assert(expo->n_scale == 0);
109
110 long exponent = bc_num2long(expo);
111 /* Exponent must be properly convertable to long */
112 if (exponent == 0 && (expo->n_len > 1 || expo->n_value[0] != 0)) {
113 assert(false && "Exponent is not well formed in internal call");
114 //assert(exponent != 0 || (expo->n_len == 0 && expo->n_value[0] == 0));
115 }
116 //assert(exponent != 0 || (expo->n_len == 0 && expo->n_value[0] == 0));
117 bc_raise(base, exponent, result, scale);
118 }
119
120