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
bc_square_ex(bc_num n1,bc_num * result,size_t scale_min)37 void bc_square_ex(bc_num n1, bc_num *result, size_t scale_min) {
38 bc_num square_ex = bc_square(n1, scale_min);
39 bc_free_num(result);
40 *(result) = square_ex;
41 }
42
43 /* Raise NUM1 to the NUM2 power. The result is placed in RESULT.
44 Maximum exponent is LONG_MAX. If a NUM2 is not an integer,
45 only the integer part is used. */
bc_raise(bc_num num1,long exponent,bc_num * result,size_t scale)46 void bc_raise(bc_num num1, long exponent, bc_num *result, size_t scale) {
47 bc_num temp, power;
48 size_t rscale;
49 size_t pwrscale;
50 size_t calcscale;
51 bool is_neg;
52
53 /* Special case if exponent is a zero. */
54 if (exponent == 0) {
55 bc_free_num (result);
56 *result = bc_copy_num(BCG(_one_));
57 return;
58 }
59
60 /* Other initializations. */
61 if (exponent < 0) {
62 is_neg = true;
63 exponent = -exponent;
64 rscale = scale;
65 } else {
66 is_neg = false;
67 rscale = MIN (num1->n_scale * exponent, MAX(scale, num1->n_scale));
68 }
69
70 /* Set initial value of temp. */
71 power = bc_copy_num(num1);
72 pwrscale = num1->n_scale;
73 while ((exponent & 1) == 0) {
74 pwrscale = 2 * pwrscale;
75 bc_square_ex(power, &power, pwrscale);
76 exponent = exponent >> 1;
77 }
78 temp = bc_copy_num(power);
79 calcscale = pwrscale;
80 exponent = exponent >> 1;
81
82 /* Do the calculation. */
83 while (exponent > 0) {
84 pwrscale = 2 * pwrscale;
85 bc_square_ex(power, &power, pwrscale);
86 if ((exponent & 1) == 1) {
87 calcscale = pwrscale + calcscale;
88 bc_multiply_ex(temp, power, &temp, calcscale);
89 }
90 exponent = exponent >> 1;
91 }
92
93 /* Assign the value. */
94 if (is_neg) {
95 bc_divide(BCG(_one_), temp, result, rscale);
96 bc_free_num (&temp);
97 } else {
98 bc_free_num (result);
99 *result = temp;
100 (*result)->n_scale = MIN(scale, (*result)->n_scale);
101 }
102 bc_free_num (&power);
103 }
104
105 /* This is used internally by BCMath */
bc_raise_bc_exponent(bc_num base,bc_num expo,bc_num * result,size_t scale)106 void bc_raise_bc_exponent(bc_num base, bc_num expo, bc_num *result, size_t scale) {
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