-
Notifications
You must be signed in to change notification settings - Fork 0
/
exponent_mode.c
41 lines (39 loc) 路 1.19 KB
/
exponent_mode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <stdlib.h>
/* This function calculates the power of a given base raised to a
specified exponent. It handles three specific cases:
1. If the exponent is 0, it returns 1, because any number raised to the
power of 0 is 1.
2. If the exponent is negative, it calculates the positive exponent power
and then takes the reciprocal to handle negative powers.
3. If the exponent is 1, it simply returns the base, as any number
raised to the power of 1 is the number itself. For all other positive
exponents, it multiplies the base by itself the number of times
specified by the exponent, effectively calculating the power.
This loop-based approach is necessary to manually compute the power
for integer exponents without using built-in functions. */
long double
exponent_calc (long double base, long double exponent)
{
if (exponent == 0)
{
return 1;
}
else if (exponent < 0)
{
return 1 / exponent_calc (base, -exponent);
}
else if (exponent == 1)
{
return base;
}
else
{
long double result = base;
for (int i = 1; i < exponent; i++)
{
result *= base;
}
return result;
}
}