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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
RavenEye
  • 23
  • 1
  • 6
  • Turn up your warning level. – chris Oct 09 '13 at 16:21
  • for j < 273.15, your return value is not explicitly set, which means you can get an unexpected return value. – thang Oct 09 '13 at 16:22
  • 1
    ctok returns double, but your else path does not return any double – taocp Oct 09 '13 at 16:23
  • possible duplicate of [What do 1.#INF00, -1.#IND00 and -1.#IND mean?](http://stackoverflow.com/questions/347920/what-do-1-inf00-1-ind00-and-1-ind-mean) – GSerg Oct 09 '13 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 Oct 09 '13 at 16:27
  • possible duplicate of [Why the return value of double is -1.#IND?](http://stackoverflow.com/q/7476177/11683) – GSerg Oct 09 '13 at 16:28

3 Answers3

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

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
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

alexbuisson
  • 7,699
  • 3
  • 31
  • 44
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

Len Holgate
  • 21,282
  • 4
  • 45
  • 92