1

it's my first day messing around with C++. I'm trying to do just a really basic code looking for the roots in a quadratic equation. Here is my code so far:

#include <iostream>
#include <cmath>

int main () {

    int a, b, c;
    double root1, root2;

    std::cout << "Enter the integers a, b, and c to fit in the quadratic equation: ax^2 + bx + c >> " << std::endl;
    std::cout << "a = ";
    std::cin >> a;
    std::cout << "b = ";
    std::cin >> b;
    std::cout << "c = ";
    std::cin >> c;
    std::cout <<"\n";
    std::cout << "Quadratic equation to solve is : " << a << "x^2 + " << b << "x + " << c <<std::endl;

    root1 = (-b + sqrt(b*b - 4*a*c))/(2*a); 
    root2 = (-b - sqrt(b*b - 4*a*c))/(2*a);

    if (root1 && root2 != nan) {
        std::cout << "root 1 = " << root1 << std::endl;
        std::cout << "root 2 = " << root2 << std::endl;
    }
    else 
    std::cout << "no root exists" << std::endl;

    return 0;
    }

I'm getting this error:

invalid operands to binary expression ('double' and 'double (*)(const char *)')

in the line:

if (root1 && root2 != nan) 

I'm looking for a simple test to see if the roots exist and this obviously doesn't work. Thanks in advance for your help!

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
user3460758
  • 957
  • 7
  • 15
  • 25

4 Answers4

5

To check if something is a real number, use isnan:

if(!isnan(root1) && !isnan(root2)) 

Explanation:

isnan determines if the given floating point number arg is not-a-number (NaN). It returns true if arg is NaN, false otherwise.

The NaN values are used to identify undefined or non-representable values for floating-point elements, such as the square root of negative numbers or the result of 0/0. In C++, it is implemented with function overloads for each floating-point type, each returning a bool value.

Raging Bull
  • 18,593
  • 13
  • 50
  • 55
  • [nan is a function](http://www.cplusplus.com/reference/cmath/nan-function/). Can't do something like `root1 != nan`. – John Bupit May 07 '14 at 15:25
  • @JohnBupit: No problem, mate!! BTW, at first time, I believed `nan` is a variable he uses. Since there is no `nan` variable declared, I understand that OP is after `nan` function. Thanks for pointing it. – Raging Bull May 07 '14 at 15:28
  • When you are including `` (like you should), you should call `std::isnan`. – T.C. May 07 '14 at 15:45
  • Yes I was planning to accept it but had to wait a few more minutes. Thank you! – user3460758 May 07 '14 at 15:57
3

Use (C++11):

#include <cmath>
...
if (!isnan(root1) && !isnan(root2)) 
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
3

double (*)(const char *) is a type which represents a pointer to a function that returns a double and takes a const char * argument. You'll find if you look at a reference for cmath that nan is the function in question.

Looks like you should be able to call it with an empty string to get a suitable value:

nan("")

However, you can't provide a double on one side of && and a bool on the other, so you'll need to have a suitable test for root1 as well.

And yes, that type syntax for nan is a bit crazy, that's how C does function pointer syntax, and the name of a function by itself represents a pointer to it, so that's what you get out of the compiler because C++ inherited C-style function pointers.

Matthew Walton
  • 9,809
  • 3
  • 27
  • 36
  • Got it! Just went with the simple solution of checking if the value in the square root was greater than 0. Out of curiosity though, is there any simple statement to check if something is a real number? – user3460758 May 07 '14 at 15:23
  • The `double` datatype represents a real number. If you're looking for a function that checks if a `string` is a real number or not, [check this out](http://stackoverflow.com/questions/1012571/stdstring-to-float-or-double). Also, please please do not _answer_ to ask a question. – John Bupit May 07 '14 at 15:33
  • `isnan()` comes from `is not a number`, so I think that using it is very good to see if it is a number or not – thedarkside ofthemoon May 07 '14 at 15:33
1

Use if (root1 != nan("") && root2 != nan(""))

The problem is in two places:

  1. root1 is always true except when it is 0
  2. nan is not declared, it should be nan("")

But I think it is better to use !isnan(root1) instead of just nan

thedarkside ofthemoon
  • 2,251
  • 6
  • 31
  • 48