- 论坛徽章:
- 0
|
请问x是float类型,什么情况下x!=x为真
原帖由 "zhaoshj" 发表:
与你的不同,倒是与你第二次在x=x+1后面加了一个句子的汇编代码很象,why??
我又试了一遍,输出结果确实是那样的!
第一贴上的第一例汇编确实是你说的那样,我所给的汇编是以下代码的:
- /***********************************************
- *file name : float_accuracy.c
- *description : x是float类型,什么情况下x!=x为真
- *note : 若在判断x!=x前对x进行了访问,
- 则有可能使x!=x为真
- *author : kernelxu
- *resource : http://bbs.chinaunix.net/forum/
- viewtopic.php?t=575049&show_type=
- *date : 2005/07/10
- ***********************************************/
- #include <stdio.h>;
- /**********************************************
- *function name : main()
- ***********************************************/
- int main(void)
- {
- float x = (float)1e-3;
- x = x + 1;
- //printf("x = %f\n", x);/*比第一例多的,但注释掉了*/
- //printf("x != x is %d\n", (x != x)); /*比第一例多的,但注释掉了*/
- if (x != x)
- {
- printf("x is UNequal to x!!\n");
- //printf("x != x is %d\n", (x != x));
- }
- else
- {
- printf("x is equal to x!\n");
- //printf("x != x is %d\n", (x != x));
- }
-
- return 0;
- }/*END OF main()*/
复制代码
是偶尔几次得到的,我也不知为什么,有时得到的全部汇编代码为:
- 1:
- 2:
- 3: #include <stdio.h>;
- 4:
- 5: int main(void)
- 6: {
- 0040E9C0 push ebp
- 0040E9C1 mov ebp,esp
- 0040E9C3 sub esp,44h
- 0040E9C6 push ebx
- 0040E9C7 push esi
- 0040E9C8 push edi
- 0040E9C9 lea edi,[ebp-44h]
- 0040E9CC mov ecx,11h
- 0040E9D1 mov eax,0CCCCCCCCh
- 0040E9D6 rep stos dword ptr [edi]
- 7: float x = (float)1e-3;
- 0040E9D8 mov dword ptr [ebp-4],3A83126Fh
- 8:
- 9: x = x + 1;
- 0040E9DF fld dword ptr [ebp-4]
- 0040E9E2 fadd dword ptr [string "x is equal to x!" (0042401c)]
- 0040E9E8 fstp dword ptr [ebp-4]
- 10: printf("HELLO WORLD!\n");
- 0040E9EB push offset string "x = %f\n" (00425000)
- 0040E9F0 call printf (00401090)
- 0040E9F5 add esp,4
- 11: //printf("x = %f\n", x);
- 12: //printf("x != x is %d\n", (x != x));
- 13: if (x != x)
- 0040E9F8 fld dword ptr [ebp-4]
- 0040E9FB fcomp dword ptr [ebp-4]
- 0040E9FE fnstsw ax
- 0040EA00 test ah,40h
- 0040EA03 jne main+54h (0040ea14)
- 14: {
- 15: printf("x is UNequal to x!!\n");
- 0040EA05 push offset string "x is unequal to x!!\n" (00424030)
- 0040EA0A call printf (00401090)
- 0040EA0F add esp,4
- 16: //printf("x != x is %d\n", (x != x));
- 17: }
- 18: else
- 0040EA12 jmp main+61h (0040ea21)
- 19: {
- 20: printf("x is equal to x!\n");
- 0040EA14 push offset string "x is equal to x!\n" (00425018)
- 0040EA19 call printf (00401090)
- 0040EA1E add esp,4
- 21: }
- 22: //printf("x != x is %d\n", (x != x));
- 23:
- 24: return 0;
- 0040EA21 xor eax,eax
- 25: }
- 0040EA23 pop edi
- 0040EA24 pop esi
- 0040EA25 pop ebx
- 0040EA26 add esp,44h
- 0040EA29 cmp ebp,esp
- 0040EA2B call __chkesp (004011c0)
- 0040EA30 mov esp,ebp
- 0040EA32 pop ebp
- 0040EA33 ret
复制代码
有时候又是:
- 1: /***********************************************
- 2: *file name : float_accuracy.c
- 3: *description : x是float类型,什么情况下x!=x为真
- 4: *note : 若在判断x!=x前对x进行了访问,
- 5: 则有可能使x!=x为真
- 6: *author : kernelxu
- 7: *resource : http://bbs.chinaunix.net/forum/
- 8: viewtopic.php?t=575049&show_type=
- 9: *date : 2005/07/10
- 10: ***********************************************/
- 11:
- 12: #include <stdio.h>;
- 13:
- 14: /**********************************************
- 15: *function name : main()
- 16: ***********************************************/
- 17: int main(void)
- 18: {
- 0040E9C0 push ebp
- 0040E9C1 mov ebp,esp
- 0040E9C3 sub esp,44h
- 0040E9C6 push ebx
- 0040E9C7 push esi
- 0040E9C8 push edi
- 0040E9C9 lea edi,[ebp-44h]
- 0040E9CC mov ecx,11h
- 0040E9D1 mov eax,0CCCCCCCCh
- 0040E9D6 rep stos dword ptr [edi]
- 19: float x = (float)1e-3;
- 0040E9D8 mov dword ptr [ebp-4],3A83126Fh
- 20:
- 21: x = x + 1;
- 0040E9DF fld dword ptr [ebp-4]
- 0040E9E2 fadd dword ptr [__real@4@3fff8000000000000000 (00425008)]
- 0040E9E8 fst dword ptr [ebp-4]
- 22: //printf("x = %f\n", x);
- 23: //printf("x != x is %d\n", (x != x));
- 24: if (x != x)
- 0040E9EB fcomp dword ptr [ebp-4]
- 0040E9EE fnstsw ax
- 0040E9F0 test ah,40h
- 0040E9F3 jne main+44h (0040ea04)
- 25: {
- 26: printf("x is UNequal to x!!\n");
- 0040E9F5 push offset string "x != x is %d\n" (00425020)
- 0040E9FA call printf (00401090)
- 0040E9FF add esp,4
- 27: //printf("x != x is %d\n", (x != x));
- 28: }
- 29: else
- 0040EA02 jmp main+51h (0040ea11)
- 30: {
- 31: printf("x is equal to x!\n");
- 0040EA04 push offset string "x != x is %d\n" (0042401c)
- 0040EA09 call printf (00401090)
- 0040EA0E add esp,4
- 32: //printf("x != x is %d\n", (x != x));
- 33: }
- 34:
- 35: return 0;
- 0040EA11 xor eax,eax
- 36: }/*END OF main()*/
- 0040EA13 pop edi
- 0040EA14 pop esi
- 0040EA15 pop ebx
- 0040EA16 add esp,44h
- 0040EA19 cmp ebp,esp
- 0040EA1B call __chkesp (004011c0)
- 0040EA20 mov esp,ebp
- 0040EA22 pop ebp
- 0040EA23 ret
复制代码
搞不清啊?大家可以研究一下! |
|