Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-nan中的sqrt函数_C_Sqrt - Fatal编程技术网

C-nan中的sqrt函数

C-nan中的sqrt函数,c,sqrt,C,Sqrt,在做一些代码练习时, 我观察到由sqrt功能引起的异常输出 代码是 #include<stdio.h> #include<math.h> int main() { double l,b,min_r,max_r; int i; scanf("%lf %lf",&b,&l); printf("%lf %lf\n",sqrt(l*l+b*b),sqrt(b*b-l*l)); return(0); } 为什么会发生这种情

在做一些代码练习时, 我观察到由sqrt功能引起的异常输出

代码是

#include<stdio.h>
#include<math.h>
int main()
{
    double l,b,min_r,max_r;
    int i;
    scanf("%lf %lf",&b,&l);
    printf("%lf %lf\n",sqrt(l*l+b*b),sqrt(b*b-l*l));
    return(0);
} 

为什么会发生这种情况。

看看数字:
b
是4,
l
是5。所以
b*b-l*l
is-9。-9的平方根是多少?它是一个虚数,但是
sqrt
不支持虚数结果,因此结果是nan(不是数字)。这是一个域错误。解决方案:不要将否定参数传递给
sqrt

看看数字:
b
是4,
l
是5。所以
b*b-l*l
is-9。-9的平方根是多少?它是一个虚数,但是
sqrt
不支持虚数结果,因此结果是nan(不是数字)。这是一个域错误。解决方案:在您的情况下,不要将否定参数传递给
sqrt

,不验证输入会导致问题

sqrt(b*b-l*l)
使用
b
as 4和
l
as 5生成一个-ve数字,这很可能是您不想要的


FWIW,负数的根需要表示虚部。

在您的情况下,不验证输入会导致问题

sqrt(b*b-l*l)
使用
b
as 4和
l
as 5生成一个-ve数字,这很可能是您不想要的


FWIW,负数的根需要表示虚部。

对于打印
双精度
%f
就足够了。(4*4-5*5)=-9,小于0。负数的sqrt()不是实数。%f在这里不是问题@SouravGhosh@SouravGhoshI我再说一遍。。。对于双%f就足够了。但在这篇文章中,%f不是问题。唯一的问题是,他试图打印负数的平方根@souravghosh对于打印
双精度
%f
就足够了。(4*4-5*5)=-9,小于0。负数的sqrt()不是实数。%f在这里不是问题@SouravGhosh@SouravGhoshI我再说一遍。。。对于双%f就足够了。但在这篇文章中,%f不是问题。唯一的问题是,他试图打印负数的平方根@苏拉沃什