[英]floating-point constant comparison - (0.0 ? 1 : 0)
在下面的示例中,如果取消注釋float f = 0.0;
,
並替換return(0.0 ? 1 : 0);
return(f ? 1 : 0);
。
輸出為NIL
。
這是我的代碼:
/* file main.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
cl -W4 -MTd -O2 -TC main.c -Fetest */
#include <stdio.h>
int my_func(void)
{
/* float f = 0.0; */
return(0.0 ? 1 : 0);
}
int main(void)
{
printf("%s\n", ( my_func() ? "ONE" : "NIL") );
return 0;
}
在32位Windows計算機上,使用Visual Studio,此代碼輸出:
ONE
my_func()
返回值true
(1)? (0.0 ? 1 : 0)
? 這看起來像是Microsoft編譯器中的一個錯誤,您應該將其提交給Connect 。 我能夠在Visual Studio Express 2010中復制它,但不能在gcc中復制它: http : //ideone.com/8qPRJd 。
任何求值為0
的整數值的表達式都應該等於false
。 這正是它如何使用float
變量,當我嘗試使用double
也是如此。
return(0.0?1:0)編譯為固定返回1.在另一種情況下,實際評估浮點變量,0.0不等於0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.