Math expression help

In the function header for fallingDistance(), i can only get the expression to work as a decimal point. I have tried multiple ways to do it with a fraction and parenthesis, but it doesn't seem to work.

I've tried....
distance = (1/2) * g * timeSqr
distance = ((1/2) * (g * timeSqr))

neither of those work.

Any suggestions?

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
  #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

//func prototypes ******************************************************
double fallingDistance(int);
//**********************************************************************
int main()
{
	double distance;

	//loops 10 times, for each second
	for (int time = 1; time <= 10; time++)
	{
		//time is the argument for calculation
		distance = fallingDistance(time);

		cout << showpoint << setprecision(2) << fixed;

		//displays the distance
		cout << "The distance at " << time << " seconds is "
			 << distance << " meters." << endl;
	}

	return 0;
}

//func headers *********************************************************
double fallingDistance(int value)
{
	double  distance,
			timeSqr,
			g = 9.8;

	//squaring time
	timeSqr = pow(value, 2);

	//calculates distance
	distance = 0.5 * g * timeSqr;

	return distance;
}
//********************************************************************** 
Last edited on
It is because you are using integer division when

1
2
distance = (1/2) * g * timeSqr
distance = ((1/2) * (g * timeSqr))



Take a look at this program for an example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main() {
    int x = 1;
    int z = 3;
    cout << x / z << endl;
    double j = 1;
    double k = 3;
    cout << j / k << endl;
    cout << 1 / 2 << endl;
    return 0;
}
0
0.333333
0

Process finished with exit code 0
So i need to assign it a data type and a variable? Why can't it just calculate it?
Yes, and It does "just calculate it" doing integer division because compiler assumes you mean integer when you type an integer. So the workaround other than creating a double explicitly is to do something like you already said you were doing.

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main() {
    cout << 1. / 2. << endl;
    return 0;
0.5
Process finished with exit code 0
Last edited on
Oh, so it was calculating it. It was just chopping off anything to the right of the decimal point.

Thanks. I forgot about that.

Another question, If you put a "large" negative number which included decimal points, would it automatically put it in a signed float? Similar to adding the decimal places turned it into a double.
Last edited on
If you put a "large" negative number which included decimal points, would it automatically put it in a signed float?


It would be a signed value, but I am not sure if it would be a float or a double.
If the above is your specification then you should just declare the variable type you need and move on. It may not look as "pretty" but you can be sure it is working as intended. That is your best move.

Also, if your number is really big. you might need a big number library like ( https://gmplib.org/ ) See below for built in c++ number limits.

http://www.cplusplus.com/reference/climits/
http://www.cplusplus.com/reference/limits/numeric_limits/
Last edited on
Thanks for all the help!
Another question, If you put a "large" negative number which included decimal points, would it automatically put it in a signed float? Similar to adding the decimal places turned it into a double.


Whatever number one inputs (via std::cin say) will be converted to the type of the variable that input is going into, provided there are no errors and realise there might be loss of precision.

The default for floating point numbers is double. double is preferable because the precision of float is easily exceeded. float can do 6 or 7 significant figures, while double can do 15 or 16. So only use float if a library requires it (typically a graphics library ) ; or if you have billions of them.

If it's a literal with either a decimal point or an exponent (e or E), then the default is double, so these are doubles:

1
2
3
std::cout << 22.0 /7.0 << "\n";
std::cout << 2.0 /7.3 << "\n";
std::cout << 0.1 /7.0 << "\n";


Note I always put digits both side of the decimal point which is always present. This reinforces the idea that it is a double.

Also note that the difference if those literals were forced to be floats: If more decimal places were displayed, the doubles will still be accurate to 15sf, while float is only 6sf

One can force a float by appending a 'f' to the literal:

1
2
3
4
5
6
7
8
float a = 10.0f; 
float b = 10.0 ; // 10.0  is a double, implicitly cast back to float again

double c = -123456789.123456; // fine
float d = -1234567.1; // loss of precision

double e = -1.23e300; // within range of double
float f = -1.23e39; // outside  range of float 


You can find out what the limits of different types are, have a play with the example code a the bottom of this link:
http://en.cppreference.com/w/cpp/types/numeric_limits
Topic archived. No new replies allowed.