Why is my number being rounded incorrectly?

后端 未结 5 1078
攒了一身酷
攒了一身酷 2021-01-18 05:00

This feels like the kind of code that only fails in-situ, but I will attempt to adapt it into a code snippet that represents what I\'m seeing.

float f = myFl         


        
5条回答
  •  余生分开走
    2021-01-18 05:27

    Float math can be performed at higher precision than advertised. But as soon as you store it in float f, that extra precision is lost. You're not losing that precision in the second method until, of course, you cast the result down to int.

    Edit: See this question Why differs floating-point precision in C# when separated by parantheses and when separated by statements? for a better explanation than I probably provided.

提交回复
热议问题