VS2017编译器关于C语言下,对复数的运算并不支持,利用complex.h头文件并不适用,通过自编的子函数,实现一般性的复数计算,精度与Matlab软件下对比,精度一致。 一、头文件ComplexCalculation.h,用于复数运算的一些函数头和定义
#ifndef _COMPLEXCALCULATION_H_
#define _COMPLEXCALCULATION_H_
#define ASSERT_ENABLE 1
#define IS_COMPLEX_DIVISOR_CORRENT(DIVISOR_REAL, DIVISOR_IMAG) ((DIVISOR_REAL != 0) || (DIVISOR_IMAG != 0))
typedef double mathDouble;
typedef unsigned char mathUint_8;
typedef unsigned short int mathUint_16;
typedef unsigned int mathUint_32;
typedef struct _ReDefcomplex
{
mathDouble Real;
mathDouble Imag;
}complexType;
complexType complexAdd(complexType a, complexType b);
complexType complexSubtract(complexType minuend, complexType subtrahend);
complexType complexMultiply(complexType a, complexType b);
complexType complexDivision(complexType dividend, complexType divisor);
mathDouble complexAbs(complexType a);
mathDouble complexAngle(complexType a);
complexType complexByAbsAngle(mathDouble r, mathDouble theta);
complexType complexExp(complexType a);
#if ASSERT_ENABLE
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((mathUint_8 *)__FILE__, __LINE__))
void assert_failed(mathUint_8* file, mathUint_32 line);
#else
#define assert_param(expr) ((void)0)
#endif
#endif
二、ComplexCalculation.c,用于复数运算的一些函数
#include "ComplexCalculation.h"
#include "math.h"
#include "stdio.h"
complexType complexAdd(complexType a, complexType b)
{
complexType result;
result.Real = a.Real + b.Real;
result.Imag = a.Imag + b.Imag;
return result;
}
complexType complexSubtract(complexType minuend, complexType subtrahend)
{
complexType result;
result.Real = minuend.Real - subtrahend.Real;
result.Imag = minuend.Imag - subtrahend.Imag;
return result;
}
complexType complexMultiply(complexType a, complexType b)
{
complexType result;
result.Real = a.Real * b.Real - a.Imag * b.Imag;
result.Imag = a.Imag * b.Real + a.Real * b.Imag;
return result;
}
complexType complexDivision(complexType dividend, complexType divisor)
{
complexType result;
assert_param(IS_COMPLEX_DIVISOR_CORRENT(divisor.Real, divisor.Imag));
result.Real = (mathDouble)(dividend.Real * divisor.Real + dividend.Imag * divisor.Imag) / \
(divisor.Real * divisor.Real + divisor.Imag * divisor.Imag);
result.Imag = (mathDouble)(dividend.Imag * divisor.Real - dividend.Real * divisor.Imag) / \
(divisor.Real * divisor.Real + divisor.Imag * divisor.Imag);
return result;
}
mathDouble complexAbs(complexType a)
{
return (sqrt(pow(a.Real, 2) + pow(a.Imag, 2)));
}
mathDouble complexAngle(complexType a)
{
return (atan2(a.Imag, a.Real));
}
complexType complexByAbsAngle(mathDouble r, mathDouble theta)
{
complexType tmp_1, tmp_2;
tmp_1.Real = 0;
tmp_1.Imag = theta;
tmp_2 = complexExp(tmp_1);
tmp_2.Real *= r;
tmp_2.Imag *= r;
return tmp_2;
}
complexType complexExp(complexType a)
{
complexType result;
result.Real = exp(a.Real) * cos(a.Imag);
result.Imag = exp(a.Real) * sin(a.Imag);
return result;
}
#if ASSERT_ENABLE
void assert_failed(mathUint_8* file, mathUint_32 line)
{
printf("Assert Error in File: %s \r\nLine: %d \r\n", file, line);
}
#endif
三、主函数通道,main.c
#include "ComplexCalculation.h"
#include "stdio.h"
int main(void)
{
complexType a, b, c;
a.Imag = 1.0;
a.Real = 0;
b.Real = -0.043369177255479;
b.Imag = 0.0;
c = complexAdd(a, b);
printf("complexAdd: c.Real %20.15lf, c.Imag %20.15lf \r\n", c.Real, c.Imag);
c = complexSubtract(a, b);
printf("complexSubtract: c.Real %20.15lf, c.Imag %20.15lf \r\n", c.Real, c.Imag);
c = complexMultiply(a, b);
printf("complexMultiply: c.Real %20.15lf, c.Imag %20.15lf \r\n", c.Real, c.Imag);
c = complexDivision(a, b);
printf("complexDivision: c.Real %20.15lf, c.Imag %20.15lf \r\n", c.Real, c.Imag);
printf("Abs(c): %f\r\n", complexAbs(a));
printf("Angle(c): %f\r\n", complexAngle(a));
c = complexByAbsAngle(complexAbs(a), complexAngle(a));
printf("complexByAbsAngle: a.Real %20.15lf, a.Imag %20.15lf \r\n", c.Real, c.Imag);
c= complexExp(a);
printf("complexExp: c.Real %20.15lf, c.Imag %20.15lf \r\n", c.Real, c.Imag);
}
|