CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
43
Task/Exponentiation-operator/C/exponentiation-operator.c
Normal file
43
Task/Exponentiation-operator/C/exponentiation-operator.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
int ipow(int base, int exp)
|
||||
{
|
||||
int pow = base;
|
||||
int v = 1;
|
||||
if (exp < 0) {
|
||||
assert (base != 0); /* divide by zero */
|
||||
return (base*base != 1)? 0: (exp&1)? base : 1;
|
||||
}
|
||||
|
||||
while(exp > 0 )
|
||||
{
|
||||
if (exp & 1) v *= pow;
|
||||
pow *= pow;
|
||||
exp >>= 1;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
double dpow(double base, int exp)
|
||||
{
|
||||
double v=1.0;
|
||||
double pow = (exp <0)? 1.0/base : base;
|
||||
if (exp < 0) exp = - exp;
|
||||
|
||||
while(exp > 0 )
|
||||
{
|
||||
if (exp & 1) v *= pow;
|
||||
pow *= pow;
|
||||
exp >>= 1;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("2^6 = %d\n", (int)ipow(2,6));
|
||||
printf("2^-6 = %lf\n", ipow(2,-6));
|
||||
printf("2.71^6 = %lf\n", dpow(2.71,6));
|
||||
printf("2.71^-6 = %lf\n", dpow(2.71,-6));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue