Division Confusion

  • Thread starter Thread starter Dave Brown
  • Start date Start date
D

Dave Brown

Hi all,
I'm having trouble understanding which data type to use to get results I
expect.
using float, decimal, and double I have tried the following, ..

float test = 55/60;

I always get 0 as the results.
I expect 0.916666

I dont understand why its rounding down. Any pointers anybody ?
Rgds,

Dave
 
Either the numerator or deniominator (or both) must be floating point values
in order to get a floating point result. For example, 55.0/60 or 55/60.0 or
55.0/60.0.
 
Dave said:
float test = 55/60;

I always get 0 as the results.
I expect 0.916666

You're divinding two ints, so you're performing integer division

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
* "Dave Brown said:
I'm having trouble understanding which data type to use to get results I
expect.
using float, decimal, and double I have tried the following, ..

float test = 55/60;

I always get 0 as the results.
I expect 0.916666

Type "55.0" and "60.0" instead of "55" and "60". "55" and "60" are interpreted as
'int's and no floating point division is done.
 
Wow fast replys, thanks guys.

so even though test is a float, it performs integer division, Hmmmph I can
see thats one to really look out for. the value 55 is being passed in as an
int so i just changed it to
float test = (float)minues / 60;

and yes the result is 0.91111.

Thanks again, seems strange behaviour though.

rgds,

Dave
 
Dave Brown said:
Wow fast replys, thanks guys.

so even though test is a float, it performs integer division, Hmmmph I can
see thats one to really look out for. the value 55 is being passed in as an
int so i just changed it to
float test = (float)minues / 60;

and yes the result is 0.91111.

Thanks again, seems strange behaviour though.

rgds,

Dave
The determination of the result type is based on the operand types, so no
matter what type of variable you store the result in, it's an integer result
in your example. As Ed points out, all you have to do in the case of
literals is add ".0" to one of them to "promote" it to a double. Then when
performing an arithmetic operation on dissimilar types, the lesser (integer)
is promoted to the greater's (double) type and the result is a double. You
then get the precision you originally expected.
 
Dave Brown said:
Wow fast replys, thanks guys.
so even though test is a float, it performs integer division, Hmmmph I can
see thats one to really look out for. the value 55 is being passed in as an
int so i just changed it to
float test = (float)minues / 60;

and yes the result is 0.91111.

Thanks again, seems strange behaviour though.

Not at all. Look at it this way: first the expression 55 / 60 is evaluated.
At that time the compiler doesn't know or care what you want to use the
result for. It is not that smart. So the only relevant part to the first
operation is

55 / 60

The compiler does have a preference for Int32 types so whenever it can it
will treat constants as Int32. And in this case it has no problem doing so.
The result of the operation is 0.

It is only after this step has been completed that the assignment takes
place. That's an easy one: converting an int to a float. Mission
accomplished.

Martin
 
As other people already mentioned, you need to tell the compiler to treat
the numbers as float.
Since they don't have a "." part they will be treated as integers.
A way to treat them as floats, without adding "." is:

float test = 55f/60f;
 
Not at all. Look at it this way: first the expression 55 / 60 is evaluated.
At that time the compiler doesn't know or care what you want to use the
result for.

However having separate integer and float division operators isn't
such a bad idea imho :) (ala basics / \ and pascals div /)

- Asbjørn
 
Back
Top