RavenEye
RavenEye

Reputation: 23

c++ Receiving "-1.#IND" from output?

Pretty simple problem here: When I test the wrong data input I give an error message but -1.#IND comes up after it?

for instance I type a negative where there should be a positive and I get "wrong input dummy-1.#IND"

#include "Header.h"

void error_rep(){
cout<<"Wrong input dummy";
}

double ctok(double c){
double j = c *273.15;
if (j >= -273.15){
return j;
}
else 
    error_rep();
}

int main(){
double c = 0;
cin >> c;

    double k = ctok(c);
    cout<<k<<endl;


keep_window_open();
}

What does this mean? and why is it coming up? How do I get rid of it?

Upvotes: 1

Views: 2760

Answers (3)

Mike Seymour
Mike Seymour

Reputation: 254531

What does this mean?

It's Microsoftese for "not a number". It means that k is not a valid floating-point number.

and why is it coming up?

When your range test fails, you don't return a value. This gives undefined behaviour; in practice, it's likely to be equivalent to returning an uninitalised value, which is likely to be garbage. Your compiler should warn you about this, if you have suitable warnings enabled.

How do i get rid of it?

I'd report the error by throwing an exception; then nothing can attempt to use the invalid return value if the function fails. Alternatively, you could return a type with a testable "invalid" state, such as boost::optional<double> or std::pair<double, bool>, and test it before use.

By the way, if that's supposed to be converting degrees Celsius to Kelvin, then you want to add 273.15, not multiply by it; and compare with zero after the conversion (or with 273.15 before converting, if you prefer).

Upvotes: 2

alexbuisson
alexbuisson

Reputation: 8469

A good compiler with all warning turns on, will have say that an execution path doesn't have a return ...,

 double ctok(double c){
 double j = c *273.15;
 if (j >= -273.15){
    return j;
 }
else {
     error_rep();
      ///here
      throw ;//somthing
}

}

and try-catch exception around ctok call

Upvotes: 1

Len Holgate
Len Holgate

Reputation: 21636

-1.#IND means that the double value is "negative indefinate NaN". Basically the value stored can't be represented as a number in a double.

See http://blogs.msdn.com/b/oldnewthing/archive/2013/02/21/10395734.aspx

Upvotes: 0

Related Questions