Keep getting zero

Hey hey the answer iam am suppsoed to output is 4... but it seems no matter what i keep getting 0? any ideas?


#pragma intrinsic(sqrt, pow)
#include <iostream>
#include <math.h>
#include <cmath>
#include <iomanip>

using namespace std;

const float G = 6.674e-011;//0.00000000006674;
const float M = 5.974e+024;//5974000000000000000000000.0;
float g1 = 9.803;
float g2 = 9.792;
float height = 0.0;

float calculateHeight(const float G, const float M, float g1, float g2)
{
float height = 0.0;

height = sqrt(G*M)*((pow(1/g2,1/2))-(pow(1/g1,1/2)));
return height;
}

int main ()
{
height = calculateHeight(G, M, g1, g2);
cout << setprecision(30);
cout << "The Balloon is " << height << " meters High in the sky!" << endl;
system("pause");
return 0;
}
Try this code and see if you can work it out. This is the beginning of learning to debug, so that you can work these things out for yourself.


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
#include <iostream>
#include <math.h>
#include <cmath>
#include <iomanip>

using namespace std;

const float G = 6.674e-011;//0.00000000006674;
const float M = 5.974e+024;//5974000000000000000000000.0;
float g1 = 9.803;
float g2 = 9.792;
float height = 0.0;

float calculateHeight(const float G, const float M, float g1, float g2)
{
float height = 0.0;

 cout << "sqrt(G*M) = " << sqrt(G*M) << endl;
 cout << "pow(1/g2,1/2) = " << pow(1/g2,1/2) << endl;
cout << "pow(1/g1,1/2) = " << pow(1/g1,1/2) << endl;

height = sqrt(G*M)*((pow(1/g2,1/2))-(pow(1/g1,1/2)));
 cout << "h1 = " << height << endl;
return height;
}

int main ()
{
height = calculateHeight(G, M, g1, g2);
cout << setprecision(30);
cout << "The Balloon is " << height << " meters High in the sky!" << endl;
system("pause");
return 0;
}

Last edited on
Ah yeah that really shows what values are popping out. they keep equaling 1 and subtracting to zero. Why is that? Or is that... the answer?
I tried putting that Setprecision in there. Cause i believe it rounds it to 1
Don't include both math.h and cmath.

cmath is the C++ version of math.h, improved to be proper C++. Including both is insanity.

Try the sqrt function instead of pow to the half. You'll get closer...
closed account (zwA4jE8b)
your variables are global, so there is no need to pass them in the function.

you should declare them in main.
Try this :)


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
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

const float G = 6.674e-011;//0.00000000006674;
const float M = 5.974e+024;//5974000000000000000000000.0;
float g1 = 9.803;
float g2 = 9.792;


float calculateHeight(const float G, const float M, float g1, float g2)
{
  float height = 0.0;


 float a,b,c,d,e;
 a = sqrt(G*M);
 b = sqrt(1/g2);
 c=  sqrt(1/g1);
 d = b - c;
 e = a*d;

 cout << "sqrt(G*M) = " << a << endl;
 cout << "sqrt(1/g2) = " << b << endl;
cout << "sqrt(1/g1) " << c << endl;

 
 height = a * d;
float h2 = sqrt(G*M)*((pow(1/g2,1/2))-(pow(1/g1,1/2)));
cout << "height = " << e << endl;
cout << "Calc all at once = " << h2 << endl;
return e;
}

int main ()
{
  float height;
height = calculateHeight(G, M, g1, g2);
cout << setprecision(30);
cout << "The Balloon is " << height << " meters High in the sky!" << endl;

return 0;
}


Then go and read about how computers actually store numbers.
Last edited on
This is a common, and non-obvious problem for new programmers, particularly in C languages.

The following expressions are different:

1
2
double a = 1 / 2;
double b = 1.0 / 2;

The first line assignes the integer value "0" to the variable a.
The second line assignes the floating-point value "0.5" to the variable b.

If you only have integers on both sides of the / operator then it is an integer divide -- producing an integer result and discarding any remainder.

If you have a floating point value on at least one side of the / operator then you get a floating-point division -- producing a floating point result.

Hope this helps.
note:

((pow(1/g2,1/2))

1/2 is integral division. In C++, 1/2 will get you 0

Anything to the power of 0 is 1

Therefore:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// this line:
float h2 = sqrt(G*M)*((pow(1/g2,1/2))-(pow(1/g1,1/2)));

//reduces to this:
float h2 = sqrt(G*M)*((pow(1/g2,0))-(pow(1/g1,0)));

// reduces to this:
float h2 = sqrt(G*M)*(1-1);

// reduces to this:
float h2 = sqrt(G*M)*(0);

// reduces to this:
float h2 = 0;


That's why you get 0 every time.

Solutions would be to use sqrt() instead of pow, or use 0.5 instead of 1/2
Sorry I have not replied ealier. Thanks for the responds. Yes i am new to this as you can see. I will try these out.
Topic archived. No new replies allowed.