Why does integer division in C# return an integer and not a float?

前端 未结 8 914
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 04:32

Does anyone know why integer division in C# returns an integer and not a float? What is the idea behind it? (Is it only a legacy of C/C++?)

In C#:

fl         


        
8条回答
  •  孤独总比滥情好
    2020-11-21 05:10

    Since you don't use any suffix, the literals 13 and 4 are interpreted as integer:

    Manual:

    If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong.

    Thus, since you declare 13 as integer, integer division will be performed:

    Manual:

    For an operation of the form x / y, binary operator overload resolution is applied to select a specific operator implementation. The operands are converted to the parameter types of the selected operator, and the type of the result is the return type of the operator.

    The predefined division operators are listed below. The operators all compute the quotient of x and y.

    Integer division:

    int operator /(int x, int y);
    uint operator /(uint x, uint y);
    long operator /(long x, long y);
    ulong operator /(ulong x, ulong y);
    

    And so rounding down occurs:

    The division rounds the result towards zero, and the absolute value of the result is the largest possible integer that is less than the absolute value of the quotient of the two operands. The result is zero or positive when the two operands have the same sign and zero or negative when the two operands have opposite signs.

    If you do the following:

    int x = 13f / 4f;
    

    You'll receive a compiler error, since a floating-point division (the / operator of 13f) results in a float, which cannot be cast to int implicitly.

    If you want the division to be a floating-point division, you'll have to make the result a float:

    float x = 13 / 4;
    

    Notice that you'll still divide integers, which will implicitly be cast to float: the result will be 3.0. To explicitly declare the operands as float, using the f suffix (13f, 4f).

提交回复
热议问题