Why integer division in c# returns an integer but not a float? -


does know why integer division in c# returns integer not float? idea behind (is legacy of c/c++)?

in c#:

float x = 13 / 4;    //imagine used have overridden == operator here use epsilon compare if (x == 3.0)    print 'hello world'; 

result of code be:

'hello world' 

strictly speaking, there no such thing integer division (division definition operation produces rational number, integers small subset of which.)

while common new programmer make mistake of performing integer division when meant use floating point division, in actual practice integer division common operation. if assuming people use it, , every time division you'll need remember cast floating points, mistaken.

first off, integer division quite bit faster, if need whole number result, 1 want use more efficient algorithm.

secondly, there number of algorithms use integer division, , if result of division floating point number forced round result every time. 1 example off of top of head changing base of number. calculating each digit involves integer division of number along remainder, rather floating point division of number.

because of these (and other related) reasons, integer division results in integer. if want floating point division of 2 integers you'll need remember cast 1 double/float/decimal.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -