Why are floating point numbers printed so differently?

后端 未结 5 1356
有刺的猬
有刺的猬 2020-11-27 23:10

It\'s kind of a common knowledge that (most) floating point numbers are not stored precisely (when IEEE-754 format is used). So one shouldn\'t do this:

0.3 -         


        
5条回答
  •  隐瞒了意图╮
    2020-11-27 23:29

    As for php, output is related to ini settings of precision:

    ini_set('precision', 15);
    print 0.3 - 0.2; // 0.1
    
    ini_set('precision', 17);
    print 0.3 - 0.2; //0.099999999999999978 
    

    This may be also cause for other languages

提交回复
热议问题