15

I am new to programming and trying to write a new program. While checking through my program it is returning the error code 1.#QNAN. I have tried isolating the variable and researching for answers but cannot find any solutions.

My code:

 // This is a program to decide what culvert should be properly used for a specific installation
// using given measurements and data
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
// initializing functions 
double slope_function();
double cbasin();
// initializing classes: Subdivisions specs

//intitializing global variables
double edge_road =0;
double up_stream  =0;
double down_stream =0;
double tbm =0.0;

//double culv_length =0;
double slope = 0.0 ;
char street_name[1001];
int min_culv = 15;
double up_strm_culv =0;
double dwn_strm_culv =0;


int main (int nNumberofArgs, char* pszArgs[])
{
    cout<< "This program will allow the surveyor to double check their calculations\n";
    cout << "in deciding what size, type, and requirements are allowed for the\n";
    cout << "installation of culverts in Terrebonne Parish.\n\n";



// begin input
   cout << "what is the name of the street\nwhere the culverts will be installed: ";
   cin.getline (street_name,1000);
   cout << endl;
   cout << "What is the Benchmark: ";
   cin >> tbm;
   cout << endl;
   cout << "What is the elevation of the edge of the road: ";
   cin >> edge_road;
   cout << endl;
   cout << "What is the up-stream culvert size: ";
   cin >> up_strm_culv;
   cout << endl;
   cout << "What is the culverts up-stream inverted elevation: ";
   cin >> up_stream;
   cout << endl;
   cout << "What is the down-stream culvert size: ";
   cin >> dwn_strm_culv;
   cout << endl;
   cout << "What is the culverts down-stream inverted elevation: ";
   cin >> down_stream;
   cout << endl;
   cout << "What is the length of culvert requested: ";
   cin >> culv_length;


   cout << "Your slope is : "; 
   cout << slope_function();
   cout << endl;
   cout << street_name;
   cout << endl;
   cout << cbasin();


    cout << endl;
  // wait until user is ready before terminating program
  // to allow the user to see the program results 
  system ("pause");
  return 0;
}  

// slope function 
double slope_function()
{
    double riseoverrun = 0.0;
    slope = (up_stream - down_stream)/ culv_length;
    return slope;
}


// Catch Basin function
double cbasin ( )
{
    double cb = 0;
    cb = culv_length / 60;
    cout << endl;
    cout << "You need ";
   cout << cb;
   cout << " catch basins for this job.";
   cout << endl;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
William Heck
  • 151
  • 1
  • 1
  • 3
  • 1
    The code you linked doesn’t compile. But even if it ran you’re probably dividing 0 by 0 which isn’t allowed. – Konrad Rudolph Jan 06 '11 at 17:39
  • 1
    I have given a description of a NAN but your actual bug is a function that returns double but has no return statement. – CashCow Jan 06 '11 at 17:39

1 Answers1

46

1#QNAN is a string representation for a "quiet NAN". A "NAN" is "not-a-number" and applies only to floats and doubles.

NANs can be very useful actually for representing "null" values (rather than picking some genuine number and hoping for the best you don't need that number for its natural meaning).

Some mathematical operations can return a NAN if the operation is "not valid" (eg taking the log of a negative number).

You can generate a QNAN from C++ using

double d = std::numeric_limits<double>::quiet_NaN();

Any comparison operation (==, <= etc) on a NAN returns false, even comparing its equality to itself, except for != which always returns true (even when comparing to itself).

(The actual bug in your code appears to be a function that returns double but has no return statement).

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • 1
    A good function for determining if a value is NAN would be: `template bool is_ieee_nan(const VAL& v) { return !(v == v); }` – John Dibling Jan 06 '11 at 17:40
  • Where do you get `!=` always returning true? – Ben Voigt Jan 06 '11 at 18:34
  • Sounds like a very odd and useless feature. If you can't detect NAN, then what good is it? – Jörgen Sigvardsson Jan 06 '11 at 22:05
  • @Ben: NaNs don't equal other NaNs, so the only time `v == v` should return `false` would be when `v` is not a number. And the `!` inverts the result (if `v` equals itself like a normal number, it is not NaN, otherwise it is. – Max Lybbert Jan 07 '11 at 10:01
  • 4
    @Jorgen: NaN isn't a C++ feature _per se_. It's an IEEE floating point arithmetic feature that is kind of exposed in C++ and several other languages. The main value is that you can signal that a math operation failed in a way that makes sense to a mathematician.. You can detect them. That's the point. But you can also use them in math operations (result is always NaN: 1/0+5*4-6 is NaN because of the "1/0" step) and determine things failed when you read the final result. – Max Lybbert Jan 07 '11 at 10:06
  • @Max: I failed to see that !(v == v) holds for NAN. – Jörgen Sigvardsson Jan 07 '11 at 11:11
  • @Max: I fully understand why `!(v == v)` returns true for NaNs, but CashCow said (second to last sentence) that `v != v` also returns true. He appears to be correct on gcc (http://ideone.com/eAptf), but I was curious where the standard calls for that behavior. (Note that the answer isn't simply `v != v` is always `!(v == v)` since NaNs break complements: `v >= v` differs from `!(v < v)` and `v <= v` differs from `!(v > v)` only for NaNs.) – Ben Voigt Jan 07 '11 at 14:19
  • 1
    The IEEE standard is that NaN isn't equal to anything, including NaN. You could write this as `NaN != NaN`. This is the only number that has this behavior, so `v != v` is only be true when `v` is NaN. Be careful though, because other "equivalent" statements don't work, eg., `0.3 != (0.1 + 0.2)`, but that's due to the fact that 0.1 + 0.2 is a calculated number and `0.3` is not. – Max Lybbert Jan 08 '11 at 07:15