-3

I have to use certain variables to make some calculations, the problem I'm facing is that the final result is incorrect, I've stumbled upon the following problem.

I've discovered that when I assign the following line to float or double variables, which supposed to hold floating points:

float a=4/3; 
double b=4/3; 
cout<< a << b; 

The output I get is: 1 and 1, why is that? I thought that as floats&doubles meant to hold floating points variables they would calculcate what is 4 divided by 3 and give me some kind of output like: 1,33 - but that's not the case.

How can I use them to build the variables which would hold the result of dividing 2 numbers, since I don't have enough time to make any calcs on my own ;)

Thanks a lot for all anwers!

Cheers Max

1 Answers1

1

It is beacause you are dividing integers. You must cast number to floating point precision.

float a=4.0f/3; 
double b=4.0f/3; 
cout<< a << b; 
FireGlider
  • 81
  • 4