Type Declarations and Division Accuracy?

Hello, all. I have a very simple problem, but it's making me crazy, and I'd appreciate it if anyone could shed some light on this for me:

float quotient;
quotient = 44100 / 1000;
cout << quotient;

Why does this print "44" instead of "44.1"? I can print 44.1 with:

float quotient;
quotient = 44.1
cout << quotient;

So what's the difference? It's so aggravating...

Thanks in advance.

-David Hernandez

PS: I'm using Ubuntu x86, and g++ 4.4.1
44100 and 1000 are of type int. Therefore an int divide is performed, which does not have a decimal component. This int result is assinged to quotient, which is a float.

The correct implementation is: float quotient = 44100F / 1000F;

44.1 is of type double. So it already has a floating point component, but it's wider than a float, so your compiler should warn you about loss of precision.
kbw, thanks for your rapid and enlightening response.

With my version of g++, "44100F / 1000F" gives, " error: invalid suffix 'F' on integer constant," but your clarification regarding the way that C++ handles division inspired me to try:

float quotient = 44100.0 / 1000.0;

And that yields 44.1.

Thanks again for your help, and have a good one.

-David Hernandez
Topic archived. No new replies allowed.