Begginer struggle.

Hello,so i have 2 codes that need to output the same number but the second one is giving me problems,when x is 3 or more it works like the first one but when its 2 or less it doesnt match.

1st is sqrt(x+3/x-3)
2nd is pow(x,2)+2x-3+(x+1)sqrt(pow(x,2)-9) / pow(x,2)-2x-3+(x-1)sqrt(pow(x,2)-9)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>
using namespace std;

int main()
{   
	double x;
	cout << "Enter number more or less than 3 ";
	cin >> x;
	double s = (x + 3) / (x - 3);
	double root = sqrt(fabs(s));
	cout << "Z1: " << root << endl;
	
	system("pause");

	return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	double x;
	cout << "Enter number more or less than 3 ";
	cin >> x;
	double p = pow(x, 2);
	double result1 = p - 9;
	double result2 = sqrt(fabs(result1));
	double result = (p + 2 * x - 3 + (x + 1)*result2) / ( p - 2 * x - 3 + (x - 1)*result2);
	cout << "Z1=" << result << endl;
	system("PAUSE");
	return 0;
}
The clue is ... Z !!!

Try the complex-number arithmetic (last two columns of output below). In your real-number arithmetic (2nd and 3rd columns of the output) you have had to put extra abs() functions in to deal with square roots of a negative number and abs(x2-9) is not the same as x2-9 when -3<x<3. There is no such problem with complex numbers.

Both arithmetics give a zero denominator (and hence NaN for the function) when the argument is precisely equal to 3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <iomanip>
#include <cmath>
#include <complex>
using namespace std;

int main()
{   
   const int nx = 21;
   const double x0 = 0.0, dx = 0.5;
   double x, y1, y2;
   complex<double> zx, zy1, zy2;

   for ( int i = 0; i < nx; i++ )
   {
      x = x0 + i * dx;    

      // Real arithmetic
      y1 = sqrt(  abs( (x+3) / (x-3) )  );

      y2 = ( x*x + 2*x - 3   +   (x+1) * sqrt(  abs( x*x-9 )  ) )
         / ( x*x - 2*x - 3   +   (x-1) * sqrt(  abs( x*x-9 )  ) );

      // Complex arithmetic
      zx = x;
      zy1 = sqrt( (zx+3.0) / (zx-3.0) );           // note the absence of "abs()"

      zy2 = ( zx*zx + 2.0*zx - 3.0   +   (zx+1.0) * sqrt(  zx*zx-9.0  ) )
          / ( zx*zx - 2.0*zx - 3.0   +   (zx-1.0) * sqrt(  zx*zx-9.0  ) );

      cout.setf( ios::fixed );
      cout.precision(6);
      cout << setw(12) << x << "   " << setw(16) << y1  << "   " << setw(16) << y2
                            << "   " << setw(16) << zy1 << "   " << setw(16) << zy2 << endl;
   }
}




    0.000000           1.000000          -0.000000   (0.000000,-1.000000)   (-0.000000,-1.000000)
    0.500000           1.183216          -0.513874   (0.000000,-1.183216)   (-0.000000,-1.183216)
    1.000000           1.414214          -1.414214   (0.000000,-1.414214)   (-0.000000,-1.414214)
    1.500000           1.732051          -3.568065   (0.000000,-1.732051)   (0.000000,-1.732051)
    2.000000           2.236068         -15.326238   (0.000000,-2.236068)   (0.000000,-2.236068)
    2.500000           3.316625          19.057209   (0.000000,-3.316625)   (0.000000,-3.316625)
    3.000000                inf                inf          (nan,nan)          (inf,nan)
    3.500000           3.605551           3.605551   (3.605551,0.000000)   (3.605551,0.000000)
    4.000000           2.645751           2.645751   (2.645751,0.000000)   (2.645751,0.000000)
    4.500000           2.236068           2.236068   (2.236068,0.000000)   (2.236068,0.000000)
    5.000000           2.000000           2.000000   (2.000000,0.000000)   (2.000000,0.000000)
    5.500000           1.843909           1.843909   (1.843909,0.000000)   (1.843909,0.000000)
    6.000000           1.732051           1.732051   (1.732051,0.000000)   (1.732051,0.000000)
    6.500000           1.647509           1.647509   (1.647509,0.000000)   (1.647509,0.000000)
    7.000000           1.581139           1.581139   (1.581139,0.000000)   (1.581139,0.000000)
    7.500000           1.527525           1.527525   (1.527525,0.000000)   (1.527525,0.000000)
    8.000000           1.483240           1.483240   (1.483240,0.000000)   (1.483240,0.000000)
    8.500000           1.445998           1.445998   (1.445998,0.000000)   (1.445998,0.000000)
    9.000000           1.414214           1.414214   (1.414214,0.000000)   (1.414214,0.000000)
    9.500000           1.386750           1.386750   (1.386750,0.000000)   (1.386750,0.000000)
   10.000000           1.362770           1.362770   (1.362770,0.000000)   (1.362770,0.000000)
Last edited on
Topic archived. No new replies allowed.