result not correct

Apr 29, 2019 at 9:18am
It give output as 2 though I declared y as float.I want it to give ans 2.5

1
2
3
4
5
6
7
8
9
10
11
12
  #include <iostream>
#include <cmath>
using namespace std;
int main()
{
	float y = 5/2;
	
	cout << y << endl;
		
	system("pause");
	return 0;
}
Apr 29, 2019 at 9:22am
1
2
3
4
5
6
7
8
9
10
11
12
  #include <iostream>
#include <cmath>
using namespace std;
int main()
{
	float y = 5/2.;
	
	cout << y << endl;
		
	system("pause");
	return 0;
}


You got the point? ;)
Apr 29, 2019 at 9:38am
float y = 5/2.
what did it do and why was my one wrong. I told C++ that y is a floating point so it should return 2.5
Apr 29, 2019 at 9:54am
For the compiler 5 is integer, 2 also, so it computes 5/2 the integer way. The result is saved to a "real" var.
The float y applies to y only, is not a directive for the complete clause.
Apr 29, 2019 at 10:07am
Hi,

It's doing integer division. I always write like this:

float y = 5.0/2.0;

because it makes it obvious that we are using floating point numbers not integers.

Mike's code makes it hard to see the decimal point (he was doing that on purpose, to make you think :+) ) That lack of visibility is the reason I don't do double literals like 2. instead of 2.0

Even better, use the double type, and braces:

double y{ 5.0/2.0};

if one tries to do this:

float y{5.0/2.0};

The compiler should warn about narrowing because the 5.0 and 2.0 are a literal double , there is a loss of precision when the value is converted to float.

double is preferable to float, because the precision of the latter is easily exceeded. float can only do 6 or 7 significant figures, while double can do 15 or 16.

Good Luck !!

Apr 29, 2019 at 10:10am
float y = 5/2;
As said, there are two steps. Yes, the new variable 'y' is initialized with a value, but that value is first computed from expression 5 / 2. Both 5 and 2 are integer values, so integer division is used to compute the value. The integer division produces an integer result.

In 5 / 2. the 2. is a double value. Therefore, the 5 will be implicitly converted into a double too, and the division uses floating point math to compute a double result from two double operands. In float y = 2.5; double (result of the division) is used to initialize a float variable, and thus there is a narrowing conversion too.
Apr 29, 2019 at 10:20am
Thank You!! I got it.....
Apr 29, 2019 at 11:16am
Mike's code makes it hard to see the decimal point (he was doing that on purpose, to make you think ...)
A single dot is hard to see, correct. Your conjecture in parentheses is a nonproven allegation.

Regarding code maintenance it is bad habit to "push" figures to floating-point type by a decimal point only. Take the time to make it unambiguous, not only for the compiler, also for you (or maybe colleagues). It is a negligible investigation what saves a lot when re-using or touching your code.

BTW, it was the wordplay "got the point?" why I "cured" your float y = 5/2 with just a single point.
Topic archived. No new replies allowed.