473,783 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5708
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.n et> wrote in message
news:ev******** ******@TK2MSFTN GP11.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****@acadxpi n.com> wrote in message
news:e7******** ******@TK2MSFTN GP10.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.n et> wrote in message
news:eH******** *****@TK2MSFTNG P12.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****@acadxpi n.com> wrote in message
news:e7******** ******@TK2MSFTN GP10.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.n et> wrote in message
news:eH******** *****@TK2MSFTNG P12.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***@somewher e.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
51051
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 field is float. The datatype for the other fields are int. I have tried testing by dividing 7 by 10000, which should give me 0.0007. The result in the database is 0.
24
19394
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
2526
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 arithmetic.
17
2429
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 replacement" and would generally like 5/2 ==2.5 So, I was contemplating to default all my modules/scripts to start with "from __future__ import division" but if it is never coming (in this decade, that is) then it would be a
10
3197
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 = 2/3 'after assignment, x is 1, whereas a sane person would say it should be 0 Does Microsoft have a reason for this design decision? I understand that this type of rounding can reduce the overall error in long computation chains by reducing the...
22
5826
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 until there is only a remainder left
2
3873
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 result of the division be truncated to integer. However, today I reviewed the method to be used in Python to get true division, and this gave
13
7127
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 this way: unsigned int dividend : divident store low 32 bits of big integer, dividend store high 32 bits of big integer.
2
4310
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 darwin Type "help", "copyright", "credits" or "license" for more information. Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for /: 'datetime.timedelta' and 'int'
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.