473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Division Confusion

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
Nov 15 '05 #1
8 5685
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 Brown" <dave.AT.dbws.net> wrote in message
news:ev**************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #2
Dave Brown wrote:
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)
Nov 15 '05 #3
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
"Frank Oquendo" <fr****@acadxpin.com> wrote in message
news:e7**************@TK2MSFTNGP10.phx.gbl...
Dave Brown wrote:
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)

Nov 15 '05 #4

"Dave Brown" <dave.AT.dbws.net> wrote in message
news:eH*************@TK2MSFTNGP12.phx.gbl...
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
"Frank Oquendo" <fr****@acadxpin.com> wrote in message
news:e7**************@TK2MSFTNGP10.phx.gbl...
Dave Brown wrote:
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)

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]
Nov 15 '05 #5
"Dave Brown" <dave.AT.dbws.net> wrote in message
news:eH*************@TK2MSFTNGP12.phx.gbl...
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
Nov 15 '05 #6
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
to the news group.
Nov 15 '05 #7
Hi Dave
This happens because the clr assumes it is an integer operation, since
you are using two integer number (55 & 60).
All these will solve your problem
Double test = 55.0/60;
Double test = 55.0/60.0;
Or you explicitly tell the clr that you are not doing an int operation.
Double test = (double) 55/60;// explicit type conversion
Float test = (float) 55/60; // explicit type conversion
Hope this explains it
Regards,
Mohamed Mossad
ITworx on behalf of Microsoft GTSC Developer support for Middle East

Nov 15 '05 #8
On Sun, 8 Feb 2004 01:26:26 +0100, "Martin Maat [EBL]"
<du***@somewhere.nl> wrote:
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
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: joel | last post by:
I have a table which I want to update by dividing one field into another. The update runs with no errors, but the results come out as only a positive integer number. The datatype for the result...
24
by: Teis Draiby | last post by:
In .NET, can I be sure that the result of a division between two integers always is truncated rather that rounded to nearest? Example: 99 / 50 = 1 regards, Teis
8
by: nineteen | last post by:
for example: 16/5: 3 -------- 5)16 15 -------- 1 the user input the dividend and divisor,the program should display the upright formula like the example. and , the program must use recursive...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
10
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer =...
22
by: jamestuck21 | last post by:
Hi, I'm trying to work out a binary division problem 1100 / 101010101010111 Here is what I have so far but I'm not sure if I'm doing it correctly and I'm suppose to continue the division...
2
by: kermit | last post by:
For a long time,, There has been a discussion of trueFor division versus integer division in Python. I myslef prefer that / be used for integer division since almost always, I want the...
13
by: jamesonang | last post by:
Supposed unsigned int(32 bits) is the largest number that computer can represent with a single variable. Now, i have a big integer ( less than 64 bit, but great than 32 bit) . i represent it by...
2
by: skip | last post by:
Running from Subversion, I see confusing (to me) behavior related to division of datetime.timedelta objects by integers: % python Python 2.6a0 (trunk:57277:57280M, Aug 28 2007, 17:44:49) on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.