1

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?

6
  • Turn up your warning level.
    – chris
    Commented Oct 9, 2013 at 16:21
  • for j < 273.15, your return value is not explicitly set, which means you can get an unexpected return value.
    – thang
    Commented Oct 9, 2013 at 16:22
  • 1
    ctok returns double, but your else path does not return any double
    – taocp
    Commented Oct 9, 2013 at 16:23
  • possible duplicate of What do 1.#INF00, -1.#IND00 and -1.#IND mean?
    – GSerg
    Commented Oct 9, 2013 at 16:27
  • Look at your code, think about it carefully. What did you want to happen in the error case? How does that match up to the code you actually wrote?
    – john
    Commented Oct 9, 2013 at 16:27

3 Answers 3

2

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).

1

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

0
0

-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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.