Browse Source

refactoring: changing functions for double types

master
fdai7848 11 months ago
parent
commit
0db2ed8c28
  1. 28
      src/exponent.c
  2. 5
      src/exponent.h
  3. 22
      test/test_exponent.c

28
src/exponent.c

@ -2,36 +2,22 @@
#include "exponent.h"
int expI(unsigned int e, int num){
int prod = 1;
if(e==0){
double expI(double e, double num){
double prod = 1.0;
if(e==0.0){
return 1;
}else{
for(int i = 1; i <=e; i++){
for(int i = 1; i <= (int) e; i++){
prod *= num;
}
return prod;
}
}
float expIN(int e, int num){
double expIN(double e, double num){
if(e>0){
return (float) expI(e, num);
return expI(e, num);
}else{
printf("%d, %d\n", e, num);
printf("%f\n", 1.0 / (float) expI((e*-1), num));
return (1.0 / (float) expI((e*-1), num));
}
}
float expF(int e, float num){
float prod = 1;
if(e==0){
return 1;
}else{
for(int i = 1; i <=e; i++){
prod *= num;
}
return prod;
return (1.0 / expI((e*-1), num));
}
}

5
src/exponent.h

@ -1,8 +1,7 @@
#ifndef EXPONENT_H
#define EXPONENT_H
int expI(unsigned int e, int num);
float expIN(int e, int num);
float expF(int e, float num);
double expI(double e, double num);
double expIN(double e, double num);
#endif // exponent.h

22
test/test_exponent.c

@ -12,24 +12,24 @@ void tearDown(void)
{
}
void test_IntegerExponent_ForPositiveExponent(void){
void test_DoubleBase_ForPositiveExponent(void){
int r1, r2, r3;
r1 = expI(2, -2);
r2 = expI(3, -3);
r3 = expI(4, 5);
r1 = expI(2.0, -2.0);
r2 = expI(3.0, -3.0);
r3 = expI(4.0, 5.0);
TEST_ASSERT_EQUAL_INT(4, r1);
TEST_ASSERT_EQUAL_INT(-27, r2);
TEST_ASSERT_EQUAL_INT(625, r3);
TEST_ASSERT_EQUAL_INT(4.0, r1);
TEST_ASSERT_EQUAL_INT(-27.0, r2);
TEST_ASSERT_EQUAL_INT(625.0, r3);
}
void test_IntegerExponent_ForNegativeExponent(void){
void test_DoubleBase_ForNegativeExponent(void){
float r1, r2, r3;
r1 = expIN(-1, 2);
r2 = expIN(-3, -3);
r3 = expIN(-2, -2);
r1 = expIN(-1.0, 2.0);
r2 = expIN(-3.0, -3.0);
r3 = expIN(-2.0, -2.0);
printf("Result 1: %d\n", r1);
printf("Result 2: %d\n", r2);

Loading…
Cancel
Save