PDA

View Full Version : gcc fixed division



Hacim
January 13th, 2007, 05:45 PM
Hello, I always thought that integer division return a floating point value,for example:

(32/ 512) == 0.0625
but unless I cast each number to a float first like this:

(float(32) / float(512))

it just drops the decimal.Am I remembering wrong or is there a compiler flag that could be set?

thanks you.

cylon359
January 13th, 2007, 06:58 PM
integer division returns an integer...

jblebrun
January 13th, 2007, 07:58 PM
Hello, I always thought that integer division return a floating point value,for example:

(32/ 512) == 0.0625
but unless I cast each number to a float first like this:

(float(32) / float(512))

it just drops the decimal.Am I remembering wrong or is there a compiler flag that could be set?

thanks you.

Yes, as cyclon said, if you specify two integers, the result of a division will be an integer. There is an easier way to force floating point division, though. If any of the numbers is determined to be floating point by the compiler then the expression will be evaluated as a floating point expression. So just write:

32.0/512