开方后再平方,输出的值和原值相等,但和原值比较时值却不相等!好疑惑,试了试C语言结果也一样!
a = 2.0 b = 4.0 x = math.sqrt(a) y = math.sqrt(b) print x,y print a,x*x print b,y*y print x*x==a print y*y==b 结果: 1.41421356237 2.0 2.0 2.0 4.0 4.0 False True
int main(void) { double a = 2.0,b = 4.0; double c,d; c = sqrt(a); d = sqrt(b); printf("%f,%f\n",a,b); printf("%f,%f\n",c*c,d*d); if(a == c*c) printf("TURE"); else printf("FALSE"); printf(","); if(b == d*d) printf("TURE"); else printf("FALSE"); return 0; } 结果: 2.000000,4.000000 2.000000,4.000000 FALSE,TURE