Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 浮点比较
#include<stdio.h> #include<conio.h> int main() { float i=0.7; clrscr(); if(i < 0.7) printf("If Block"); else printf("Else Block"); getch(); return 0; }
我不明白为什么输出会是“如果块”.....请帮助为什么执行 if 部分?
实际上i是0.69999999998在它的浮动表示中。
i
0.69999999998
当您i=0.7在内存中分配时,0.7 不能double precision像您想象的那样表示。
i=0.7
double precision
float因此,和之间的比较double导致类型提升,在这种情况下i小于0.7哪个是双倍的。
float
double
0.7