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

Page 1 of 1

## 7 Replies - 356 Views - Last Post: 16 December 2018 - 07:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=412638&amp;s=0fd9a88ccb188d2470804146c1068891&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 daygamer

Reputation: -5
• Posts: 110
• Joined: 21-August 18

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

Posted 12 September 2018 - 12:57 AM

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??
Is This A Good Question/Topic? 0

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

### #2 ndc85430

• I think you'll find it's "Dr"

Reputation: 971
• Posts: 3,828
• Joined: 13-June 14

## 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 sepp2k

• D.I.C Lover

Reputation: 2726
• Posts: 4,365
• Joined: 21-June 11

## 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 baavgai

• Dreaming Coder

Reputation: 7397
• Posts: 15,329
• Joined: 16-October 07

## 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
> 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 CampNerd

Reputation: 0
• Posts: 13
• Joined: 11-December 18

## 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
```5 / 2*2
```
the 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 = 4
```
then you would take the
```5 / 4 = 1.2
```
, then with it being below
``` .50
```
round 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 Skydiver

• Code herder

Reputation: 6763
• Posts: 23,063
• Joined: 05-May 12

## 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:
```7 / 6

```

and
```7 / 6.0

```

### #7 sepp2k

• D.I.C Lover

Reputation: 2726
• Posts: 4,365
• Joined: 21-June 11

## 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*2
```

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 Sheepings

• Senior Programmer

Reputation: 198
• Posts: 1,115
• Joined: 05-December 13

## 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