Float numbers in Java

后端 未结 6 1653
傲寒
傲寒 2020-12-07 03:10

Could anyone please me why the output of the following programme is not \" different different\"?

public static void main(String[] args)
{

float f1=3.2f;
fl         


        
6条回答
  •  长情又很酷
    2020-12-07 03:12

    Because 3.2f is a float value and 3.2 is a double value. Floating point numbers are always slightly inaccurate because binary representation cannot implement them accurately, so comparing them for exact equality is a bad idea. Particularly comparing floats with doubles. Expressions such as 3.2f == 3.2f are usually okay, but even those can fail in some languages, e.g. if they represent numbers in registers more accurately than in memory.

提交回复
热议问题