Hi,

i just fount out that the result to this (int) 5 / 2 * 2 was and is 4. Believe it or not i got chocked. How is it even possible this is 4? Should be 5??

# (int) 5 / 2 * 2 algorithm in C#

Page 1 of 1## 7 Replies - 356 Views - Last Post: 16 December 2018 - 07:32 AM

##
**Replies To:** (int) 5 / 2 * 2 algorithm in C#

### #2

## Re: (int) 5 / 2 * 2 algorithm in C#

Posted 12 September 2018 - 12:59 AM

It's called integer division - dividing two integers results in another integer. If you want floating point division, you have to make one of the operands a float.

### #3

## Re: (int) 5 / 2 * 2 algorithm in C#

Posted 12 September 2018 - 04:00 AM

Note that 5 / 2 * 2 already has type int, so the cast is redundant and does not affect the result.

### #4

## Re: (int) 5 / 2 * 2 algorithm in C#

Posted 12 September 2018 - 05:32 AM

daygamer, on 12 September 2018 - 02:57 AM, said:

Should be 5??

Why?

When you divide integers any overage is simply waste. To reason about the size of the overage you'd have to care about it. Think of integer division as "how many time does X go into Y." In this context, a fraction is nonsensical.

You can play with this in VS:

Microsoft (R) Roslyn C# Compiler version 2.9.0.63208 Loading context from 'CSharpInteractive.rsp'. Type "#help" for more information. > 5 / 2 2 > 5 / 2.0 2.5 > 16 / 9 1 > 16 / 9.0 1.7777777777777777 >

Indeed, think about how a program that might do integer division:

> Func<int, int, int> div = (value, divBy) => (divBy > value) ? 0 : (1 + div(value - divBy, divBy)); > div(5,2) 2 >

Hope this helps.

### #5

## Re: (int) 5 / 2 * 2 algorithm in C#

Posted 16 December 2018 - 06:02 AM

I am just weighing in some logic here. If you take

However just looking at it from the correct mathematical standpoint operations. If I am wrong then please explain to me why I would be wrong.

5 / 2*2the answer would be 1.25, However it is how you look at the Order of Operations (OO). In the OO it states that you must Multiplication, Division, Addition and Subtraction, in that order. So with this being an Algorithm, to me you should follow the correct sequence which would be,

2*2 = 4then you would take the

5 / 4 = 1.2, then with it being below

.50round it down to get your whole number.

5 / 2*2 = 1

However just looking at it from the correct mathematical standpoint operations. If I am wrong then please explain to me why I would be wrong.

### #6

## Re: (int) 5 / 2 * 2 algorithm in C#

Posted 16 December 2018 - 06:13 AM

C# has multiplication and division at the same precedence. For operations with the same precedence, they are done from left to right.

Any which way, the problem you are running into is integer vs. floating point (or fixed point) arithmetic. Notice the difference in results between:

and

Any which way, the problem you are running into is integer vs. floating point (or fixed point) arithmetic. Notice the difference in results between:

7 / 6

and

7 / 6.0

### #7

## Re: (int) 5 / 2 * 2 algorithm in C#

Posted 16 December 2018 - 06:21 AM

CampNerd, on 16 December 2018 - 02:02 PM, said:

I am just weighing in some logic here. If you take

5 / 2*2the answer would be 1.25

5/2*2 is 5 in regular maths (5/2=2.5 and 2.5*2=5) and 4 using integer arithmetic (5/2=2 and 2*2=4). Adding some extra spaces doesn't change that. Spaces have no semantics in either maths or C#. 5/(2*2) would be 1.25 in regular maths and 1 using integer arithmetic, but nobody asked about 5/(2*2).

Quote

However it is how you look at the Order of Operations (OO). In the OO it states that you must Multiplication, Division, Addition and Subtraction, in that order.

That's wrong. Multiplication and division have the same precedence. This is true in regular maths as well almost all programming languages.

Note that in regular maths (but not integer arithmetic), it'd also be okay to say that division has higher precedence than multiplication as that would lead to the same results in all cases. But giving multiplication higher precedence is just wrong because it changes the result compared to the normal rules.

### #8

## Re: (int) 5 / 2 * 2 algorithm in C#

Posted 16 December 2018 - 07:32 AM

And the topic is four months old, just felt the need to point that out

Page 1 of 1