[英]How does C handle complex equations if using REALs
對於你們來說,這可能是一件容易的事,但是我找不到確切的答案,我只是想確保自己不會忽略任何東西。 我有一個方程式,我知道可以使用復雜的解決方案,但是我已經使用“ double”和/或“ float”在C中對其進行了編程。 如果我不使用“復雜”類型,C是否會簡單地忽略復雜部分? 換句話說,它只是返回實部嗎? 通過不使用“復雜”會產生任何錯誤嗎? 謝謝。
C語言中有一個“復雜”和“虛構”數據類型。但是,由於自引入以來只有幾年時間,因此某些舊系統可能不支持它。 因此,最好明確地處理此類解決方案。
如果執行的是sqrt(-1)之類的非法操作, 則將生成錯誤。
以下文章最有可能更好地回答您的問題如何在C中使用復數?
sqrt()
的文檔(如果您閱讀過的話)告訴您它返回域錯誤。
您可以通過測試用例自己找到它:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
int main(int argc, char *argv[])
{
double foo = -1.234;
double foo_sqrt = sqrt(foo);
if (errno == EDOM) {
fprintf(stderr, "Error: EDOM - Mathematics argument out of domain of function (POSIX.1, C99)\n");
return EXIT_FAILURE;
}
/* we never get here */
fprintf(stdout, "sqrt(%f) = %f\n", foo, sqrt(foo));
return EXIT_SUCCESS;
}
然后編譯並運行:
$ gcc -lm -std=c99 -Wall sqrt_test.c -o sqrt_test
$ ./sqrt_test
Error: EDOM - Mathematics argument out of domain of function (POSIX.1, C99)
$ echo $?
1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.