user-0 提问: 死循环

回复:
user: user-1
content: for(i = 0;i <= 10; i +=0.1)

user: user-2
content: 仅供参考#include <stdio.h> #include <stdlib.h> #include <string.h> float f; double d; void showf() {     char bs[65];     char b[65];     unsigned char *p;     char e[12];     char *t;     int ex;      printf("\nf=%.6f\n",f);     p=(unsigned char *)&f;     printf("hex=%02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]);     ltoa(*(long *)&f,b,2);     sprintf(bs,"%032s",b);     printf("bin=%s\n",bs);     printf("bin=%.1s %.8s   %s\n",bs,bs+1,bs+9);     strncpy(e,bs+1,8);e[8]=0;     ex=strtol(e,&t,2);     printf("    %c %-4d-127 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9);     ex-=127;     printf("    %c %-8d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9); } void showd() {     char bs[65];     char b[65];     unsigned char *p;     char e[12];     char *t;     int ex;      printf("\nd=%.15lf\n",d);     p=(unsigned char *)&d;     printf("hex=%02X %02X %02X %02X %02X %02X %02X %02X\n",p[7],p[6],p[5],p[4],p[3],p[2],p[1],p[0]);     _i64toa(*(__int64 *)&d,b,2);     sprintf(bs,"%064s",b);     printf("bin=%s\n",bs);     printf("bin=%.1s %.11s   %s\n",bs,bs+1,bs+12);     strncpy(e,bs+1,11);e[11]=0;     ex=strtol(e,&t,2);     printf("    %c %-6d-1023 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12);     ex-=1023;     printf("    %c %-11d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12); } int main() {      for (f=0.0f;f<=2.2f;f+=0.2f) showf();     printf("------------\n");     f=2.0f;showf();     printf("-----------------------------------\n");     for (d=0.0 ;d<=2.2 ;d+=0.2 ) showd();     printf("------------\n");     d=2.0 ;showd();      return 0; } // //f=0.000000 //hex=00 00 00 00 //bin=00000000000000000000000000000000 //bin=0 00000000   00000000000000000000000 //  + 0   -127 1.00000000000000000000000 //  + -127     1.00000000000000000000000 // //f=0.200000 //hex=3E 4C CC CD //bin=00111110010011001100110011001101 //bin=0 01111100   10011001100110011001101 //  + 124 -127 1.10011001100110011001101 //  + -3       1.10011001100110011001101 // //f=0.400000 //hex=3E CC CC CD //bin=00111110110011001100110011001101 //bin=0 01111101   10011001100110011001101 //  + 125 -127 1.10011001100110011001101 //  + -2       1.10011001100110011001101 // //f=0.600000 //hex=3F 19 99 9A //bin=00111111000110011001100110011010 //bin=0 01111110   00110011001100110011010 //  + 126 -127 1.00110011001100110011010 //  + -1       1.00110011001100110011010 // //f=0.800000 //hex=3F 4C CC CD //bin=00111111010011001100110011001101 //bin=0 01111110   10011001100110011001101 //  + 126 -127 1.10011001100110011001101 //  + -1       1.10011001100110011001101 // //f=1.000000 //hex=3F 80 00 00 //bin=00111111100000000000000000000000 //bin=0 01111111   00000000000000000000000 //  + 127 -127 1.00000000000000000000000 //  + 0        1.00000000000000000000000 // //f=1.200000 //hex=3F 99 99 9A //bin=00111111100110011001100110011010 //bin=0 01111111   00110011001100110011010 //  + 127 -127 1.00110011001100110011010 //  + 0        1.00110011001100110011010 // //f=1.400000 //hex=3F B3 33 34 //bin=00111111101100110011001100110100 //bin=0 01111111   01100110011001100110100 //  + 127 -127 1.01100110011001100110100 //  + 0        1.01100110011001100110100 // //f=1.600000 //hex=3F CC CC CE //bin=00111111110011001100110011001110 //bin=0 01111111   10011001100110011001110 //  + 127 -127 1.10011001100110011001110 //  + 0        1.10011001100110011001110 // //f=1.800000 //hex=3F E6 66 68 //bin=00111111111001100110011001101000 //bin=0 01111111   11001100110011001101000 //  + 127 -127 1.11001100110011001101000 //  + 0        1.11001100110011001101000 // //f=2.000000 //hex=40 00 00 01 //bin=01000000000000000000000000000001 //bin=0 10000000   00000000000000000000001 //  + 128 -127 1.00000000000000000000001 //  + 1        1.00000000000000000000001 //------------ // //f=2.000000 //hex=40 00 00 00 //bin=01000000000000000000000000000000 //bin=0 10000000   00000000000000000000000 //  + 128 -127 1.00000000000000000000000 //  + 1        1.00000000000000000000000 //----------------------------------- // //d=0.000000000000000 //hex=00 00 00 00 00 00 00 00 //bin=0000000000000000000000000000000000000000000000000000000000000000 //bin=0 00000000000   0000000000000000000000000000000000000000000000000000 //  + 0     -1023 1.0000000000000000000000000000000000000000000000000000 //  + -1023       1.0000000000000000000000000000000000000000000000000000 // //d=0.200000000000000 //hex=3F C9 99 99 99 99 99 9A //bin=0011111111001001100110011001100110011001100110011001100110011010 //bin=0 01111111100   1001100110011001100110011001100110011001100110011010 //  + 1020  -1023 1.1001100110011001100110011001100110011001100110011010 //  + -3          1.1001100110011001100110011001100110011001100110011010 // //d=0.400000000000000 //hex=3F D9 99 99 99 99 99 9A //bin=0011111111011001100110011001100110011001100110011001100110011010 //bin=0 01111111101   1001100110011001100110011001100110011001100110011010 //  + 1021  -1023 1.1001100110011001100110011001100110011001100110011010 //  + -2          1.1001100110011001100110011001100110011001100110011010 // //d=0.600000000000000 //hex=3F E3 33 33 33 33 33 34 //bin=0011111111100011001100110011001100110011001100110011001100110100 //bin=0 01111111110   0011001100110011001100110011001100110011001100110100 //  + 1022  -1023 1.0011001100110011001100110011001100110011001100110100 //  + -1          1.0011001100110011001100110011001100110011001100110100 // //d=0.800000000000000 //hex=3F E9 99 99 99 99 99 9A //bin=0011111111101001100110011001100110011001100110011001100110011010 //bin=0 01111111110   1001100110011001100110011001100110011001100110011010 //  + 1022  -1023 1.1001100110011001100110011001100110011001100110011010 //  + -1          1.1001100110011001100110011001100110011001100110011010 // //d=1.000000000000000 //hex=3F F0 00 00 00 00 00 00 //bin=0011111111110000000000000000000000000000000000000000000000000000 //bin=0 01111111111   0000000000000000000000000000000000000000000000000000 //  + 1023  -1023 1.0000000000000000000000000000000000000000000000000000 //  + 0           1.0000000000000000000000000000000000000000000000000000 // //d=1.200000000000000 //hex=3F F3 33 33 33 33 33 33 //bin=0011111111110011001100110011001100110011001100110011001100110011 //bin=0 01111111111   0011001100110011001100110011001100110011001100110011 //  + 1023  -1023 1.0011001100110011001100110011001100110011001100110011 //  + 0           1.0011001100110011001100110011001100110011001100110011 // //d=1.400000000000000 //hex=3F F6 66 66 66 66 66 66 //bin=0011111111110110011001100110011001100110011001100110011001100110 //bin=0 01111111111   0110011001100110011001100110011001100110011001100110 //  + 1023  -1023 1.0110011001100110011001100110011001100110011001100110 //  + 0           1.0110011001100110011001100110011001100110011001100110 // //d=1.600000000000000 //hex=3F F9 99 99 99 99 99 99 //bin=0011111111111001100110011001100110011001100110011001100110011001 //bin=0 01111111111   1001100110011001100110011001100110011001100110011001 //  + 1023  -1023 1.1001100110011001100110011001100110011001100110011001 //  + 0           1.1001100110011001100110011001100110011001100110011001 // //d=1.800000000000000 //hex=3F FC CC CC CC CC CC CC //bin=0011111111111100110011001100110011001100110011001100110011001100 //bin=0 01111111111   1100110011001100110011001100110011001100110011001100 //  + 1023  -1023 1.1100110011001100110011001100110011001100110011001100 //  + 0           1.1100110011001100110011001100110011001100110011001100 // //d=2.000000000000000 //hex=3F FF FF FF FF FF FF FF //bin=0011111111111111111111111111111111111111111111111111111111111111 //bin=0 01111111111   1111111111111111111111111111111111111111111111111111 //  + 1023  -1023 1.1111111111111111111111111111111111111111111111111111 //  + 0           1.1111111111111111111111111111111111111111111111111111 // //d=2.200000000000000 //hex=40 01 99 99 99 99 99 99 //bin=0100000000000001100110011001100110011001100110011001100110011001 //bin=0 10000000000   0001100110011001100110011001100110011001100110011001 //  + 1024  -1023 1.0001100110011001100110011001100110011001100110011001 //  + 1           1.0001100110011001100110011001100110011001100110011001 //------------ // //d=2.000000000000000 //hex=40 00 00 00 00 00 00 00 //bin=0100000000000000000000000000000000000000000000000000000000000000 //bin=0 10000000000   0000000000000000000000000000000000000000000000000000 //  + 1024  -1023 1.0000000000000000000000000000000000000000000000000000 //  + 1           1.0000000000000000000000000000000000000000000000000000 //

user: user-3
content: 男人要对自己好一点

user: user-4
content: 这个题目出得不错

user: user-5
content: 嗯，的确是个死的，浮点数一般不能精确存储，最好不要for(i=0;i!=10; i += 0.1)这么写

user: user-6
content: 引用 18 楼 a627088424 的回复:求汇编的   ==   实现。是否是精确的bit相同才算== 当然了

user: user-0
content: [quote=引用 20 楼 a627088424 的回复:] #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { 	float i=10.0f; 	char a[65]; 	char bin[65]; 	 	unsigned char *p=(unsigned char*)&i; 	printf("%f\n",i); 	printf("HEX: %02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]);  		 	ltoa(*(unsigned long*)&i,a,2); 	sprintf(bin,"%032s",a); 	printf("     s exponent fraction\n"); 	printf("BIN: %c %8.8s %s\n\n",bin[0],&bin[1],&bin[9]); 	 	float value=0; 	while(!( value-10>-0.00001 && value-10<0.00001 )) 		value += 0.1; 	 	p=(unsigned char*)&value; 	printf("%f\n",value); 	printf("HEX: %02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]);	 	ltoa(*(unsigned long*)&value,a,2); 	sprintf(bin,"%032s",a); 	printf("     s exponent fraction\n"); 	printf("BIN: %c %8.8s %s\n\n",bin[0],&bin[1],&bin[9]); } [/quote] 更正个小问题，"BIN: %c %8.8s %s\n\n",bin[0],&bin[1],&bin[8]——>"BIN: %c %8.8s %s\n\n",bin[0],&bin[1],&bin[9]  结贴之

user: user-0
content: 引用 21 楼 lm_whales 的回复:1）如果需要精确的计数次数，循环变量不要用浮点数。 2）浮点数加（减）法，当两个操作数差别较大时；       可能因为精度问题，而造成加（减）法失灵，结果是绝对值较大的那个数。 3）浮点数比较 == 和 ！= 不能用于精确比较计算结果。 这里的问题是 0.1 不能用浮点数精确表示。 所以每次不是精确地加上0.1了嗯，误差在过程中积累了

user: user-7
content: 1）如果需要精确的计数次数，循环变量不要用浮点数。 2）浮点数加（减）法，当两个操作数差别较大时；       可能因为精度问题，而造成加（减）法失灵，结果是绝对值较大的那个数。 3）浮点数比较 == 和 ！= 不能用于精确比较计算结果。 这里的问题是 0.1 不能用浮点数精确表示。 所以每次不是精确地加上0.1了

user: user-0
content: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { 	float i=10.0f; 	char a[65]; 	char bin[65]; 	 	unsigned char *p=(unsigned char*)&i; 	printf("%f\n",i); 	printf("HEX: %02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]);  		 	ltoa(*(unsigned long*)&i,a,2); 	sprintf(bin,"%032s",a); 	printf("     s exponent fraction\n"); 	printf("BIN: %c %8.8s %s\n\n",bin[0],&bin[1],&bin[8]); 	 	float value=0; 	while(!( value-10>-0.00001 && value-10<0.00001 )) 		value += 0.1; 	 	p=(unsigned char*)&value; 	printf("%f\n",value); 	printf("HEX: %02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]);	 	ltoa(*(unsigned long*)&value,a,2); 	sprintf(bin,"%032s",a); 	printf("     s exponent fraction\n"); 	printf("BIN: %c %8.8s %s\n\n",bin[0],&bin[1],&bin[8]); }

user: user-2
content: 15楼正解！

user: user-0
content: 求汇编的   ==   实现。是否是精确的bit相同才算==

user: user-8
content: 别把double当成int来理解，不能简单的判断两个浮点类型的相等和不等。

user: user-9
content: 浮点数不建议用==比较，除非你懂IEEE754。

user: user-10
content: double一般不能用==或者!=的,几乎必错

user: user-11
content: 浮点数别用 == 来判断 可以这样处理 if(fNumber  - fN < 0.0000001 )

user: user-12
content: for(int i=0;i  < 100;  ++i) printf("%lf\n", i/10.0);

user: user-12
content: for(i=0;i!=10; i += 0.1)  --》 for(i=0;i <= 10; i += 0.1)

user: user-13
content: double 类型的参数不适合这种比较，容易出现不可预测的问题。建议使用整形

user: user-14
content: 引用 3 楼 a627088424 的回复:[quote=引用 2 楼 a515360208 的回复:] double是有精度的，double的10,可能是10.000001， 你要循环100次，改为<=好了，干嘛非要要!=去判断不要用直觉回答[/quote] 不是直觉，本来double是有精度的，在精度范围内都是可以的，你查一查double的存储机制， 然后考虑一下如何判断一个double类型的数是否为0，