From 7c9f4fd3f6cfa720f9f9bb6d47ac1641f4bc4f77 Mon Sep 17 00:00:00 2001 From: jofret Date: Thu, 19 Jun 2008 13:08:29 +0000 Subject: Enable division and tests --- src/type/doubleComplex.c | 13 +++++++++++- src/type/floatComplex.c | 14 ++++++++++++- src/type/testDoubleComplex.c | 50 +++++++++++++++++++++++++++++++------------- src/type/testFloatComplex.c | 32 ++++++++++++++++++++++------ 4 files changed, 86 insertions(+), 23 deletions(-) diff --git a/src/type/doubleComplex.c b/src/type/doubleComplex.c index 02976237..46dfb188 100644 --- a/src/type/doubleComplex.c +++ b/src/type/doubleComplex.c @@ -175,7 +175,18 @@ doubleComplex ztimess(doubleComplex z1, doubleComplex z2) { #endif } - +/* +** \function zdivides +** \brief Divide 2 Complex numbers. +*/ +doubleComplex zdevides(doubleComplex z1, doubleComplex z2) { +#ifndef STDC99 + return DoubleComplex((z1.real*z2.real + z1.imag*z2.imag) / (z2.real*z2.real + z2.imag* z2.imag), + (z1.imag*z2.real - z1.real*z2.imag) / (z2.real*z2.real + z2.imag* z2.imag)); +#else + return z1 / z2; +#endif +} /* diff --git a/src/type/floatComplex.c b/src/type/floatComplex.c index c2b2bdb6..af23cb98 100644 --- a/src/type/floatComplex.c +++ b/src/type/floatComplex.c @@ -171,12 +171,24 @@ floatComplex cdiffs(floatComplex z1, floatComplex z2) { floatComplex ctimess(floatComplex z1, floatComplex z2) { #ifndef STDC99 return FloatComplex(z1.real*z2.real - z1.imag*z2.imag, - z1.real*z2.imag + z2.real*z1.imag); + z1.real*z2.imag + z2.real*z1.imag); #else return z1 * z2; #endif } +/* +** \function cdivides +** \brief Divide 2 Complex numbers. +*/ +floatComplex cdevides(floatComplex z1, floatComplex z2) { +#ifndef STDC99 + return FloatComplex((z1.real*z2.real + z1.imag*z2.imag) / (z2.real*z2.real + z2.imag* z2.imag), + (z1.imag*z2.real - z1.real*z2.imag) / (z2.real*z2.real + z2.imag* z2.imag)); +#else + return z1 / z2; +#endif +} diff --git a/src/type/testDoubleComplex.c b/src/type/testDoubleComplex.c index fa40f627..ebfd8403 100644 --- a/src/type/testDoubleComplex.c +++ b/src/type/testDoubleComplex.c @@ -16,10 +16,7 @@ #define size 10000 -int matrixCreation(void); -int addAndDiff(void); - -int matrixCreation(void) { +static int matrixCreation(void) { double real[size]; double imag[size]; @@ -58,9 +55,9 @@ int matrixCreation(void) { return 0; } -int addAndDiff(void) { +static int addAndDiff(void) { /* z = -3 + 25*%i */ - doubleComplex z = DoubleComplex(3,-25); + doubleComplex z = DoubleComplex(3.0,-25.0); /* y = -3.123456 + 25.123456*%i */ doubleComplex y = DoubleComplex(-3.123456,25.123456); /* t = z + y */ @@ -71,32 +68,55 @@ int addAndDiff(void) { /* z = -3 + 25*%i */ printf("Partie reelle = %f\n", zreals(z)); - assert(zreals(z) == (double)3); + assert(zreals(z) == 3.0); printf("Partie imaginaire = %f\n", zimags(z)); - assert(zimags(z) == (double)-25); + assert(zimags(z) == -25.0); /* y = -3.123456 + 25.123456*%i */ printf("Partie reelle = %f\n", zreals(y)); - assert(zreals(y) == (double)-3.123456); + assert(zreals(y) == -3.123456); printf("Partie imaginaire = %f\n", zimags(y)); - assert(zimags(y) == (double)25.123456); + assert(zimags(y) == 25.123456); /* Try to have somme addition */ printf("Partie reelle = %f\n", zreals(t)); - assert(zreals(t) == (double)3 + (double)-3.123456); + assert(zreals(t) == 3.0 + -3.123456); printf("Partie imaginaire = %f\n", zimags(t)); - assert(zimags(t) == (double)-25 + (double)25.123456); + assert(zimags(t) == -25.0 + 25.123456); /* Try to have somme diff */ printf("Partie reelle = %f\n", zreals(u)); - assert(zreals(u) == (double)3 - (double)-3.123456); + assert(zreals(u) == 3.0 - -3.123456); printf("Partie imaginaire = %f\n", zimags(u)); - assert(zimags(u) == (double)-25 - (double)25.123456); + assert(zimags(u) == -25.0 - 25.123456); + return 0; +} + +static int timesAndDevide(void) { + /* z1 = 1 + i */ + doubleComplex z1 = DoubleComplex(1.0, 1.0); + /* z2 = 2 + i */ + doubleComplex z2 = DoubleComplex(2.0, 1.0); + + doubleComplex z1_times_z2 = ztimess(z1, z2); + doubleComplex z1_devide_z2 = zdevides(z1, z2); + + /* z1 * z2 = 1 + 3i */ + printf("z1_times_z2 = %e + %ei\n", zreals(z1_times_z2), zimags(z1_times_z2)); + assert(zreals(z1_times_z2) == 1.0); + assert(zimags(z1_times_z2) == 3.0); + + /* z1 / z2 = 0.6 + 0.2i */ + printf("z1_devide_z2 = %e + %ei\n", zreals(z1_devide_z2), zimags(z1_devide_z2)); + assert(zreals(z1_devide_z2) == 0.6); + assert(zimags(z1_devide_z2) == 0.2); + return 0; } int main(void) { - addAndDiff(); matrixCreation(); + addAndDiff(); + timesAndDevide(); return 0; } diff --git a/src/type/testFloatComplex.c b/src/type/testFloatComplex.c index 4dab9977..779258fb 100644 --- a/src/type/testFloatComplex.c +++ b/src/type/testFloatComplex.c @@ -18,10 +18,7 @@ #define size 10000 -int matrixCreation(void); -int addAndDiff(void); - -int matrixCreation(void) { +static int matrixCreation(void) { float real[size]; float imag[size]; @@ -60,7 +57,7 @@ int matrixCreation(void) { return 0; } -int addAndDiff(void) { +static int addAndDiff(void) { /* z = -3 + 25*%i */ floatComplex z = FloatComplex((float)3, (float)-25); /* y = -3.123456 + 25.123456*%i */ @@ -98,8 +95,31 @@ int addAndDiff(void) { return 0; } +static int timesAndDevide(void) { + /* z1 = 1 + i */ + floatComplex z1 = FloatComplex(1.0f, 1.0f); + /* z2 = 2 + i */ + floatComplex z2 = FloatComplex(2.0f, 1.0f); + + floatComplex z1_times_z2 = ctimess(z1, z2); + floatComplex z1_devide_z2 = cdevides(z1, z2); + + /* z1 * z2 = 1 + 3i */ + printf("z1_times_z2 = %e + %ei\n", creals(z1_times_z2), cimags(z1_times_z2)); + assert(creals(z1_times_z2) == 1.0f); + assert(cimags(z1_times_z2) == 3.0f); + + /* z1 / z2 = 0.6 + 0.2i */ + printf("z1_devide_z2 = %e + %ei\n", creals(z1_devide_z2), cimags(z1_devide_z2)); + assert(creals(z1_devide_z2) == 0.6f); + assert(cimags(z1_devide_z2) == 0.2f); + + return 0; +} + int main(void) { - addAndDiff(); matrixCreation(); + addAndDiff(); + timesAndDevide(); return 0; } -- cgit