What type of data should I use to represent a complex number? (C language)

This question already has an answer here:

  • How to work with complex numbers in C? 6 answers

  • A complex number should be a struct in C.

    struct complex {
       double real;
       double imaginary;
    }
    

    Because C does not support operator overloading (like, for example, C++ does), you cannot use operators "+" and "-", but instead need to implement functions like add and sub .

    struct complex add (struct complex c1, struct complex c2) {
       struct complex result;
    
       result.real = c1.real + c2.real;
       result.imaginary = c1.imaginary + c2.imaginary;
       return result;
    }
    

    That answers the question that you asked - Starting with c99, there is, however, a C99 type that supports complex numbers more directly.

    链接地址: http://www.djcxy.com/p/24384.html

    上一篇: Visual Studio中“stdafx.h”的用途是什么?

    下一篇: 我应该使用什么类型的数据来表示复数? (C语言)