473,597 Members | 2,039 Online

# Is integer division always truncated

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
Jul 21 '05 #1
24 19326
Yes. You can also get the remainder that results in an integer division.

If you need rounding, use floating point division and round the result.

"Teis Draiby" <te************ *@draiby.com> wrote in message
news:u8******** ******@TK2MSFTN GP11.phx.gbl...
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

Jul 21 '05 #2
Ah! That must be the Math.IEEERemain der()

Thanks, Teis
Jul 21 '05 #3
I don't think so. At first I would have said "yes" agreed with you. But in machine language .99999999999999 99 is 1
not 0 16 bit here. In other words in pure mathematics .99999... = 1 but the machine cannot run out to infinity it can only
run out to the bits allowed allowed for integers. In this case ..99999....9 = 1. We'd have to test that to be sure but that
seems reasonable.

--
George Hester
_______________ _______________ ___
"Marina" <so*****@nospam .com> wrote in message news:eT******** ******@tk2msftn gp13.phx.gbl...
Yes. You can also get the remainder that results in an integer division.

If you need rounding, use floating point division and round the result.

"Teis Draiby" <te************ *@draiby.com> wrote in message
news:u8******** ******@TK2MSFTN GP11.phx.gbl...
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

Jul 21 '05 #4

"Teis Draiby" <te************ *@draiby.com> wrote in message
news:u8******** ******@TK2MSFTN GP11.phx.gbl...
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

Um, I would have to disagree with those that say yes.
I haven't taken it to the IL or assembly level, but I believe as written the
values will be cast to doubles, divided, then the result rounded to get your
Integer. Even if that is not exactly what happens, you will NOT get 1.

The key here is the operator you are using ( / ) this is a floating point
division operator.
What you will want to employ is the often overlooked ( \ ) operator, which
is an integer division operator.
Aside from being much faster then ( / ), you should get your desired
results.
Put this into a function and step through it, monitoring the values of each
variable:

Dim intA, intB As Integer
Dim dblA, dblB As Double
Dim int1, int2, int3, int4 As Integer
Dim dbl1, dbl2, dbl3, dbl4 As Double

intA = 99
intB = 50
dblA = 99.0#
dblB = 50.0#

int1 = intA / intB '=2
int2 = intA \ intB '=1
int3 = dblA / dblB '=2
int4 = dblA \ dblB '=1

dbl1 = intA / intB '=1.98
dbl2 = intA \ intB '=1.0
dbl3 = dblA / dblB '=1.98
dbl4 = dblA \ dblB '=1.0

Gerald
Jul 21 '05 #5
Amazing. I was expecting a simple yes or no answer. Thank you very much,
Teis

"Gerald Hernandez" <Cablewizard@sp *********@Yahoo .com> wrote in message
news:eD******** ******@TK2MSFTN GP10.phx.gbl...

"Teis Draiby" <te************ *@draiby.com> wrote in message
news:u8******** ******@TK2MSFTN GP11.phx.gbl...
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

Um, I would have to disagree with those that say yes.
I haven't taken it to the IL or assembly level, but I believe as written

the values will be cast to doubles, divided, then the result rounded to get your Integer. Even if that is not exactly what happens, you will NOT get 1.

The key here is the operator you are using ( / ) this is a floating point
division operator.
What you will want to employ is the often overlooked ( \ ) operator, which
is an integer division operator.
Aside from being much faster then ( / ), you should get your desired
results.
Put this into a function and step through it, monitoring the values of each variable:

Dim intA, intB As Integer
Dim dblA, dblB As Double
Dim int1, int2, int3, int4 As Integer
Dim dbl1, dbl2, dbl3, dbl4 As Double

intA = 99
intB = 50
dblA = 99.0#
dblB = 50.0#

int1 = intA / intB '=2
int2 = intA \ intB '=1
int3 = dblA / dblB '=2
int4 = dblA \ dblB '=1

dbl1 = intA / intB '=1.98
dbl2 = intA \ intB '=1.0
dbl3 = dblA / dblB '=1.98
dbl4 = dblA \ dblB '=1.0

Gerald

Jul 21 '05 #6
Gerald Hernandez <Cablewizard@sp am_remove> wrote:
Um, I would have to disagree with those that say yes.
I haven't taken it to the IL or assembly level, but I believe as written the
values will be cast to doubles, divided, then the result rounded to get your
Integer. Even if that is not exactly what happens, you will NOT get 1.

The key here is the operator you are using ( / ) this is a floating point
division operator.

Well, maybe so if the OP is using VB.NET. Not if they're using C#.
Unfortunately, we have no way of knowing from their post...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7
Jon,

You can use in C# this for what you ask.

int a = (int) 99 / (int) 50;

I hope this helps?

Cor
Jul 21 '05 #8

"Teis Draiby" <te************ *@draiby.com> wrote in message
news:u8******** ******@TK2MSFTN GP11.phx.gbl...
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

Language dependent, not framework dependent.
For example in C++ the behavior is exactly as you describe and can only be
altered by casting one of the operands to a floating point type: double
answer = 99 / 50.0; will result in a real number answer.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Jul 21 '05 #9

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Gerald Hernandez <Cablewizard@sp am_remove> wrote:
Um, I would have to disagree with those that say yes.
I haven't taken it to the IL or assembly level, but I believe as written the values will be cast to doubles, divided, then the result rounded to get your Integer. Even if that is not exactly what happens, you will NOT get 1.

The key here is the operator you are using ( / ) this is a floating point division operator.

Well, maybe so if the OP is using VB.NET. Not if they're using C#.
Unfortunately, we have no way of knowing from their post...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Thanks for the additional input. Unfortunately, I forgot to consider C#.
This seems pretty unfriendly, on the part of VB.

Gerald
Jul 21 '05 #10

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

### Similar topics

 23 480 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 0 7965 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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of... 0 7885 by: Hystou | last post by: Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,... 0 8258 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 6686 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 5847 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 5426 by: conductexam | last post by: I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();... 0 3923 by: adsilva | last post by: A Windows Forms form does not have the event Unload, like VB6. What one acts like? 1 1493 by: muto222 | last post by: How can i add a mobile payment intergratation into php mysql website. 0 1231 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.