Applying casts to the results of integer and floating point division: what's going on here?

后端 未结 4 1052
情话喂你
情话喂你 2021-01-18 17:49

I\'m a beginner and there\'s something that\'s not making much sense to me. Please could be so kind as to explain where I\'m going wrong. I\'m sorry if this has been asked b

4条回答
  •  心在旅途
    2021-01-18 18:18

    The cast is only applied to the expression immediately after it.
    Examples:

    (int)1/3  
    applies the cast to the 1. equivalent to ((int)1)/3
    
    (int)(1/3)
    the cast is applied to the result of the expression.
    
    (int)((double)1.333*(float)3.167)
    getting a little more complicated. 1.333 casted to double, 
    multiplied by 3.167 casted to a float, with the result casted to an int
    

    Hope that helps!

提交回复
热议问题