Is integer division always truncated

T

Teis Draiby

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
 
M

Marina

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.
 
G

George Hester

I don't think so. At first I would have said "yes" agreed with you. But in machine language .9999999999999999 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.
 
G

Gerald Hernandez

Teis Draiby said:
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
 
J

Jon Skeet [C# MVP]

Gerald Hernandez said:
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...
 
C

Cor Ligthert

Jon,

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

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

I hope this helps?

Cor
 
P

Peter van der Goes

Teis Draiby said:
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.
 
G

Gerald Hernandez

Jon Skeet said:
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...

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

Gerald
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
You can use in C# this for what you ask.

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

I hope this helps?

I think you missed the point of my post.

In C#, 99/50 is 1, because the exact meaning of the / operator depends
on the type of the operands.

In VB.NET, 99/50 is 1.98, because / always means "floating point
division".

As the OP hasn't specified what language they're using, but many people
replying on this thread (including Gerald, whose post I was following
up to) have assumed one language or another. (Most have assumed C# or
C++, Gerald has assumed VB.NET.)
 
C

Cor Ligthert

Jon,

I know, however for the same sake could you have given this answer direct.

Cor
 
T

Teis Draiby

Thanks for all the informative posts on the art of dividing.
I use C#.
Usually you are told that the choice of .NET language is, to a great extend,
a matter of preference, thus I considered standard operations like division
to be the identical across .NET languages.

I assume the behaveiours in C#, managed C++ and C++ are identical. Is that
right?

In C#, is there any difference between:
int i = 99 / 50;
and: int i = (int) 99 / (int) 50;
Isn't the numbers already considered integers and therefore forcing an
integer division?


Thank you, Teis
 
J

Jon Skeet [C# MVP]

Teis Draiby said:
Thanks for all the informative posts on the art of dividing.
I use C#.
Usually you are told that the choice of .NET language is, to a great extend,
a matter of preference, thus I considered standard operations like division
to be the identical across .NET languages.

The operation is - but the syntax isn't. It's only a syntactic
difference here, just as another language could use "div" for integer
division and "fdiv" for floating point division. C# uses one operator
and overloads it depending on operand type; VB.NET uses two different
operators (presumably mainly for historical reasons).
I assume the behaveiours in C#, managed C++ and C++ are identical. Is that
right?

I believe so.
In C#, is there any difference between:
int i = 99 / 50;
and: int i = (int) 99 / (int) 50;
Isn't the numbers already considered integers and therefore forcing an
integer division?

Indeed.
 
C

Cor Ligthert

Jon,

I was me not aware of that, does that mean that we can say that VBNet is
more precise in calculating than C#?

Dim a As Double = 99 * 1 / 50
a = 1.98

double a = 99 * 1 / 50;
a = 1.0

Cor
 
G

Gerald Hernandez

Cor Ligthert said:
Jon,

I was me not aware of that, does that mean that we can say that VBNet is
more precise in calculating than C#?

Dim a As Double = 99 * 1 / 50
a = 1.98

double a = 99 * 1 / 50;
a = 1.0

Cor

heh, no. The precision is the same. The syntax is different.
Jon is just pointing out that C# chooses which method to use based on
overloading, whereas VB "/" uses Doubles, and "\" uses Integers.
From a VB background, I am used to the two different operators. With C style
syntax being different, I was not surprised that C# seemed to deal with both
by overloading.
But I was a little surprised that given your sample:
double a = 99 * 1 / 50;
a = 1.0
I would have made the assumption that because the return value was double,
the undecorated constants would have been treated as doubles. Clearly I
would be wrong. From a VB users point of view, this would cause confusion,
just as VB's method would cause a C# user to be confused.

FYI, given your example,
double a = 99 * 1 / 50;
it is evaluated as:
double a = (99 * 1) / 50;
a = 1.0

If it was evaluated as:
double a = 99 * (1 / 50);
Then a = 0.0

Who would have thought something so simple could be confusing?

Gerald
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
I was me not aware of that, does that mean that we can say that VBNet is
more precise in calculating than C#?

No. It just means that when the operands of an arithmetic operator are
both integeral types, so is the result of the operator.
Dim a As Double = 99 * 1 / 50
a = 1.98

double a = 99 * 1 / 50;
a = 1.0

So cast one of the sides appropriately:

double a = 99 * (double)1/50;
a=1.98
 
C

Cor Ligthert

Jon,

I was before aware of almost the exact answer I would get, however could not
resist this one.

And I can give comments on that, however I don't

:)

Cor
 
J

Jon Skeet [C# MVP]

Gerald Hernandez said:
But I was a little surprised that given your sample:
I would have made the assumption that because the return value was double,
the undecorated constants would have been treated as doubles.

No - the use of the result of an operator never affects what the
operator does.
 
G

Gerald Hernandez

Jon Skeet said:
No - the use of the result of an operator never affects what the
operator does.

Lesson learned. Thank you for you pointing out the differences for me.

Gerald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top