c ++负平方根 [英] c++ negative square root

查看:185
本文介绍了c ++负平方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是如果平方根为负,则打印 *** 。但我不能想办法改变默认 nan 文本到 ***

  for(int i = x1; i <= x2; i ++){
double y = sqrt(pow * i-500);
if(y =?){
outFile< ***;所以,我应该在if语句中写什么来使它成为可能?或者有另一种方法检查y是否为nan然后打印 *

解决方案

检查对平方根函数的负输入?

  for(int i = x1; i < = x2; ++ i)
{
double x = pow(i,2)+ 3 * i-500;
if(x< 0)
{
outFile< ***;
}
else
{
outFile< sqrt(x);
}
}


My goal is to print *** if the square root is negative. But I can't think of a way to change default nan text to ***

for(int i=x1;i<=x2;i++){
   double y = sqrt(pow(i,2)+3*i-500);
    if(y = ?){
        outFile << "***";
    }

So, what should I write in the if statement to make it possible? Or maybe there is another way to check if the y is nan then print *

解决方案

How about checking for a negative input to the square root function?

for (int i = x1; i <= x2; ++i)
{
    double x = pow(i, 2) + 3*i - 500;
    if (x < 0)
    {
        outFile << "***";
    }
    else
    {
        outFile << sqrt(x);
    }
}

这篇关于c ++负平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆