0

Query for setting the condition of "undefined" in C++ Like:

double func(double x)
{
    double f = sin(x) / x;
    return f;
}
int main(){
    double l;
    cout << "Enter x= ";
    cin >> l;
    if(condition for setting f(x)==nan){
    cout<<"The value can not be defined"<<endl;
    }
    else{
    cout<<"the value of f = "<<f(x)<<endl;
    } 
    return 0;
}

If I have to put the condion for checking the value of f(x) is undefined(displays nan in terminal), what should I write when I don't know at which value of x f(x) in undefined. In shorts, f(x)==?? (what should I put??)

Subhra Dey
  • 31
  • 1
  • 5
  • 2
    See https://stackoverflow.com/questions/570669/checking-if-a-double-or-float-is-nan-in-c – dedObed Oct 25 '20 at 20:43
  • there's no function definition `f` in your code, so `f(x)` will not compile. – JHBonarius Oct 25 '20 at 20:48
  • The value of `func` is never undefined. The value of `func(0.0)` is [defined](https://en.cppreference.com/w/cpp/language/operator_arithmetic#Multiplicative_operators) to be `NaN`, hence the terminal's displayed value. It would be better to describe your goal as checking if the value of `func(x)` is either not a real number (if you want to also catch infinities) or not a number (what "NaN" stands for). – JaMiT Oct 25 '20 at 21:44

0 Answers0