如何用C显示i的负数平方根 [英] How to Print Out the Square Root of A Negative Number with i Displayed in C
问题描述
我试图弄清楚如果正好是负数(由用户输入)时如何显示数字的平方根,如果是,请正确显示显示的 i。当我执行普通的 sqrt
函数时,结果总是类似于-1。#IND。当我尝试使用双复数变量时,正数或负数都将变得干净。
I am trying to figure out how to display the square root of a number if it happens to be negative (as it is entered by the user), and if so, display it correctly with the "i" displayed as well. When I do the normal sqrt
function, the result is always something like -1.#IND. When I tried using the double complex variables, the positive numbers nor the negative numbers would come out clean.
下面是我的代码;评论是我的目标。用户输入4个变量,可以是正整数或负整数。
Below is my code; the comments are what my goal is. The 4 num variables are entered by the user and can be any integer, positive or negative.
// Display the square root of each number. Remember that the user can enter negative numbers and
// will need to find the negative root with the "i" displayed.
printf("\nThe square root of %d is %.4f", num1, sqrt(num1));
printf("\nThe square root of %d is %.4f", num2, sqrt(num2));
printf("\nThe square root of %d is %.4f", num3, sqrt(num3));
printf("\nThe square root of %d is %.4f", num4, sqrt(num4));
推荐答案
如果使用浮点运算,则可以使用内置的复杂实用程序,例如:
If you're working with floating point you can use the built-in complex utilities, e.g.:
#include <complex.h>
#include <stdio.h>
int main(void)
{
double complex num = -4.0;
double complex s = csqrt(num);
printf("%.2f + %.2fi\n", creal(s), cimag(s));
}
这篇关于如何用C显示i的负数平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!