Why is floating point arithmetic in C# imprecise?

后端 未结 3 1220
感情败类
感情败类 2020-11-21 23:06

Why does the following program print what it prints?

class Program
{
    static void Main(string[] args)
    {
        float f1 = 0.09f*100f;
        float f         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-21 23:29

    The main thing is that this isn't just .Net: it's a limitation of the underlying system most every language will use to represent a float in memory. The precision only goes so far.

    You can also have some fun with relatively simple numbers, when you take into account that it's not even base ten. 0.1, for example, is a repeating decimal when represented in binary.

提交回复
热议问题