all groups > dotnet windows forms > february 2004 >
You're in the

dotnet windows forms

group:

Division Confusion


Re: Division Confusion Ed Kaim [MSFT]
2/7/2004 3:27:00 PM
dotnet windows forms:
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.

[quoted text, click to view]

Re: Division Confusion Frank Oquendo
2/7/2004 5:28:33 PM
[quoted text, click to view]

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)

Re: Division Confusion Peter van der Goes
2/7/2004 5:50:18 PM

[quoted text, click to view]
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.

--
Peter [MVP Academic]

Division Confusion Dave Brown
2/7/2004 11:24:22 PM
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

Re: Division Confusion Dave Brown
2/7/2004 11:37:06 PM
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
[quoted text, click to view]

Re: Division Confusion hirf-spam-me-here NO[at]SPAM gmx.at
2/8/2004 12:29:49 AM
* "Dave Brown" <dave.AT.dbws.net> scripsit:
[quoted text, click to view]

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.

--
Herfried K. Wagner [MVP]
Re: Division Confusion Martin Maat [EBL]
2/8/2004 1:26:26 AM
[quoted text, click to view]

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

Re: Division Confusion Morten Wennevik
2/8/2004 11:17:13 AM
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;

--
The hotmail account will most likely not be read, so please respond only
Re: Division Confusion Lord Crc
2/8/2004 5:01:31 PM
On Sun, 8 Feb 2004 01:26:26 +0100, "Martin Maat [EBL]"
[quoted text, click to view]

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

AddThis Social Bookmark Button