0

Have this task:

#include <iostream>
#include <clocale>
#include <math.h>

using namespace std;

int main()
{
using std::setlocale;
setlocale (LC_ALL,"");
double x, y, z, a, b, c, s;
cout << "Enter x: ";
cin >> x;
cout << "Enter y: ";
cin >> y;
cout << "Enter z:";
cin >> z;
a = sqrt(10*(pow(x,1/3)*(x,y+2)));
b = pow(asin(z),2);
c = fabs(x-y);
s = a*(b-c);
cout << "Result s = " << s << endl;

return 0;
}

Don't know why result is shown is NaN. Could you please help? My code is above.

Sample added:

miselking
  • 3,043
  • 4
  • 28
  • 39
  • 1
    Did you know that "1/3" is 0? It's not .33333... And whatever "(x,y+2)" is supposed to be, it's not what you think it is, either, and it's anybody's guess what you intended to do there. [Please get yourself a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Sep 30 '18 at 15:44
  • I have not, but still don't know how to fix it. – Anton Zhahalkovich Sep 30 '18 at 15:45
  • Well, the first part is easy: always use floating point values when you need floating point arithmetic: "1.0/3.0". But the second part nobody will be able to tell you, since it's unlikely that anyone can guess what (x,y+2) is supposed to be. So, this will still be wrong, but you will need to figure out by yourself what it should be, or at least come up with a description that everyone else can follow. – Sam Varshavchik Sep 30 '18 at 15:47
  • I see. Well, I wanted to make it as pow (x,y+2), but still the nan stuff appears T.T – Anton Zhahalkovich Sep 30 '18 at 15:50
  • Well then, it's time for you to run your debugger, and execute your debugger one line at a time, and examine the values of all intermediate variables, after executing each line. After entering your input, you will be able to follow your program's execution, one line at a time, examine the value of each variable, and determine if it is what you expect it to be, and if not, why not. Knowing how to use a debugger is a required skill for every C++ developer. This is what a debugger is for. – Sam Varshavchik Sep 30 '18 at 15:52
  • You also need to describe the INPUTS you are providing to your program. Assuming IEEE floating point, `sqrt()` will return a NaN if given a negative argument, andd `asin()` will return a NaN if given a value outside the range [-1,1]. Check the arguments BEFORE calling those functions - once you have the NaN it is often too late to recover anything sensible. Once a NaN has entered a calculation, it tends to propagate and cause other operations to produce NaNs. – Peter Sep 30 '18 at 16:12

1 Answers1

0

Here you go:

#include <iostream>
#include <clocale>
#include <cmath>

using namespace std; //to simplify code, you shouldnt be using this

int main()
{
   double x = 16.55*pow(10,-3);
   double y = -2.75;
   double z = 0.15;
   double h = 1.0/3.0;

   double s = sqrt(10*(pow(x,h)+pow(x,y+2)))*(pow(asin(z),2)-abs(x-y));
   cout<<s;
}

Just as Sam mentioned, 1/3 is not what it seems to be. In that state it's calculated as an int, which gives 0 as a result.

Raf.M
  • 43
  • 11