Search code examples
c++for-loopsqrt

c++ negative square root


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 *


Solution

  • 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);
        }
    }