Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
43
Task/Exponentiation-operator/C/exponentiation-operator-1.c
Normal file
43
Task/Exponentiation-operator/C/exponentiation-operator-1.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", ipow(2,6));
|
||||
printf("2^-6 = %d\n", ipow(2,-6));
|
||||
printf("2.71^6 = %lf\n", dpow(2.71,6));
|
||||
printf("2.71^-6 = %lf\n", dpow(2.71,-6));
|
||||
}
|
||||
13
Task/Exponentiation-operator/C/exponentiation-operator-2.c
Normal file
13
Task/Exponentiation-operator/C/exponentiation-operator-2.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#define generic_pow(base, exp)\
|
||||
_Generic((base),\
|
||||
double: dpow,\
|
||||
int: ipow)\
|
||||
(base, exp)
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("2^6 = %d\n", generic_pow(2,6));
|
||||
printf("2^-6 = %d\n", generic_pow(2,-6));
|
||||
printf("2.71^6 = %lf\n", generic_pow(2.71,6));
|
||||
printf("2.71^-6 = %lf\n", generic_pow(2.71,-6));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue