您好,登錄后才能下訂單哦!
復(fù)數(shù)在數(shù)學(xué)運(yùn)算中十分重要,在編寫數(shù)值運(yùn)算或者算法的時(shí)候,我們會(huì)用到復(fù)數(shù)這種概念。 那么,復(fù)數(shù)在C/C++語言中是如何表示的呢?我們接下來一一介紹。
C語言中復(fù)數(shù)
在數(shù)學(xué)中一個(gè)復(fù)數(shù)可以定義為 (z=a + bi) 的形式。 C 語言在 ISO C99 時(shí)就引入了復(fù)數(shù)類型。它是通過 complex.h 中定義的。 我們可以使用 complex , __complex__ , 或 _ComplexI 類型符號(hào)來表示。在C語言中有三種復(fù)數(shù)類型,分別為 float complex , double complex , long double complex 。他們之間 的區(qū)別就是表示復(fù)數(shù)中實(shí)部和虛步的數(shù)的數(shù)據(jù)類型不同。 complex 其實(shí)就是一個(gè)數(shù)組,數(shù)組中有兩個(gè)元素,一個(gè)表示復(fù)數(shù)的實(shí)部,一個(gè)表示復(fù)數(shù)的虛部。
定義一個(gè)復(fù)數(shù)
在 complex.h 頭文件中定義了兩個(gè)宏 _Complex_I 和 I 來創(chuàng)建一個(gè)復(fù)數(shù)。
Macro: const float complex _Complex_I; Macro: const float complex I;
這兩個(gè)宏表示復(fù)數(shù) (0+1i) , 我們可是使用這個(gè)單位復(fù)數(shù)來創(chuàng)建任何復(fù)數(shù)。
#include <stdio.h> #include <complex.h> int main(int argc, char *argv[]) { complex double a = 3.0 + 4.0 * _Complex_I; __complex__ double b = 4.0 + 5.0 * _Complex_I; _Complex double c = 5.0 + 6.0 * _Complex_I; printf("a=%f+%fi\n", creal(a),cimag(a)); printf("b=%f+%fi\n", creal(b), cimag(b)); printf("c=%f+%fi\n", creal(c), cimag(c)); printf("the arg of a is %d", carg(a)); return 0; } a=3.000000+4.000000i b=4.000000+5.000000i c=5.000000+6.000000i the arg of a is 176
復(fù)數(shù)的基本操作函數(shù)
在 complex.h 頭文件中定義一些對(duì)復(fù)數(shù)的基本操作的函數(shù)。
函數(shù)功能
creal 獲取復(fù)數(shù)的實(shí)部
cimag 獲取復(fù)數(shù)的虛部
conj 獲取復(fù)數(shù)的共軛
carg 獲取,復(fù)平面上穿過原點(diǎn)和復(fù)數(shù)在復(fù)平面表示的點(diǎn),的直線和實(shí)數(shù)軸之間的夾角
cproj 返回復(fù)數(shù)在黎曼球面上的投影
復(fù)數(shù)的數(shù)值操作
復(fù)數(shù)類型也可以像普通數(shù)值類型,~int, double, long~ 一樣進(jìn)行直接使用數(shù)值操作符號(hào),進(jìn)行數(shù)值操作。
#include <stdio.h> #include <complex.h> int main(int argc, char *argv[]) { complex double a = 3.0 + 4.0 * _Complex_I; __complex__ double b = 4.0 + 5.0 * _Complex_I; _Complex double c = 5.0 + 6.0 * _Complex_I; complex double d =a + b; complex double f = a *b ; complex double g = a/b; printf ("d=a+b=%f+%fi\n",creal(d),cimag(d)); printf ("f=a*b=%f+%fi\n",creal(f),cimag(f)); printf("g=a/b=%f+%fi\n",creal(g),cimag(g)); return 0; } d=a+b=7.000000+9.000000i f=a*b=-8.000000+31.000000i g=a/b=0.780488+0.024390i
以上就是學(xué)編程應(yīng)該知道的c語言中的復(fù)數(shù)操作的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注億速云其它相關(guān)文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。