7 Replies - 356 Views - Last Post: 16 December 2018 - 07:32 AM Rate Topic: -----

#1 daygamer   User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • 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   User is offline

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

Reputation: 971
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2726
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


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

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

Posted 12 September 2018 - 05:32 AM

View Postdaygamer, 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.
Was This Post Helpful? 0
  • +
  • -

#5 CampNerd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6763
  • View blog
  • 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


Was This Post Helpful? 0
  • +
  • -

#7 sepp2k   User is offline

  • D.I.C Lover
  • member icon

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

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

Posted 16 December 2018 - 06:21 AM

View PostCampNerd, on 16 December 2018 - 02:02 PM, said:

I am just weighing in some logic here. If you take
5 / 2*2 
the 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.
Was This Post Helpful? 0
  • +
  • -

#8 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 198
  • View blog
  • 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 ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1