[转]memset对于int、long long、float、double 的极值怎么清
http://blog.csdn.net/vmurder/article/details/46537613
链接:
#include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/46537613"); }
- 1
- 2
- 3
- 4
- 5
- 6
int
”较“的原则:加法不爆。
极大值:0x7f
较大值:0x3f
较小值:0xc0
极小值:0x80
long long
”较“的原则:加法不爆。
极大值:0x7f
较大值:0x3f
较小值:0xc0
极小值:0x80
float
”较“的原则:保证一定位精度。
7f以上一直到be都是-0 (实际上是一个很小的>-1.0的负数)
极大值:0x7f
较大值:0x4f
较小值:0xce
极小值:0xfe
0xff是 -1.#QNAN0000…… (-∞?)
double
”较“的原则:保证一定位精度。
极大值:0x7f
较大值:0x43
较小值:0xc2
极小值:0xfe
实验代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #define N 5 6 using namespace std; 7 int a[N]; 8 long long b[N]; 9 float c[N]; 10 double d[N]; 11 void _int() 12 { 13 puts(""); 14 puts("int:"); 15 16 puts(""); 17 memset(a,0x7f,sizeof a);printf("%d\n",a[0]); 18 memset(a,0x80,sizeof a);printf("%d\n",a[0]); 19 memset(a,0x81,sizeof a);printf("%d\n",a[0]); 20 21 puts(""); 22 memset(a,0xbf,sizeof a);printf("%d %d\n",a[0],a[0]*2); 23 memset(a,0xc0,sizeof a);printf("%d %d\n",a[0],a[0]*2); 24 memset(a,0xc1,sizeof a);printf("%d %d\n",a[0],a[0]*2); 25 26 puts(""); 27 memset(a,0x3e,sizeof a);printf("%d %d\n",a[0],a[0]*2); 28 memset(a,0x3f,sizeof a);printf("%d %d\n",a[0],a[0]*2); 29 memset(a,0x40,sizeof a);printf("%d %d\n",a[0],a[0]*2); 30 31 puts(""); 32 memset(a,0x7e,sizeof a);printf("%d\n",a[0]); 33 memset(a,0x7f,sizeof a);printf("%d\n",a[0]); 34 memset(a,0x80,sizeof a);printf("%d\n",a[0]); 35 } 36 void _long() 37 { 38 puts(""); 39 puts("long long:"); 40 41 puts(""); 42 memset(b,0x7f,sizeof b);printf("%I64d\n",b[0]); 43 memset(b,0x80,sizeof b);printf("%I64d\n",b[0]); 44 memset(b,0x81,sizeof b);printf("%I64d\n",b[0]); 45 46 puts(""); 47 memset(b,0xbf,sizeof b);printf("%I64d %I64d\n",b[0],b[0]*2); 48 memset(b,0xc0,sizeof b);printf("%I64d %I64d\n",b[0],b[0]*2); 49 memset(b,0xc1,sizeof b);printf("%I64d %I64d\n",b[0],b[0]*2); 50 51 puts(""); 52 memset(b,0x3e,sizeof b);printf("%I64d %I64d\n",b[0],b[0]*2); 53 memset(b,0x3f,sizeof b);printf("%I64d %I64d\n",b[0],b[0]*2); 54 memset(b,0x40,sizeof b);printf("%I64d %I64d\n",b[0],b[0]*2); 55 56 puts(""); 57 memset(b,0x7e,sizeof b);printf("%I64d\n",b[0]); 58 memset(b,0x7f,sizeof b);printf("%I64d\n",b[0]); 59 memset(b,0x80,sizeof b);printf("%I64d\n",b[0]); 60 } 61 void _float() 62 { 63 puts(""); 64 puts("float:"); 65 66 puts(""); 67 memset(c,0xfd,sizeof c);printf("%f\n",c[0]); 68 memset(c,0xfe,sizeof c);printf("%f\n",c[0]); 69 memset(c,0xff,sizeof c);printf("%f\n",c[0]); 70 71 puts(""); 72 memset(c,0xcd,sizeof c);printf("%f %f\n",c[0],c[0]*2); 73 memset(c,0xce,sizeof c);printf("%f %f\n",c[0],c[0]*2); 74 memset(c,0xcf,sizeof c);printf("%f %f\n",c[0],c[0]*2); 75 76 puts(""); 77 memset(c,0x4e,sizeof c);printf("%f %f\n",c[0],c[0]*2); 78 memset(c,0x4f,sizeof c);printf("%f %f\n",c[0],c[0]*2); 79 memset(c,0x50,sizeof c);printf("%f %f\n",c[0],c[0]*2); 80 81 puts(""); 82 memset(c,0x7e,sizeof c);printf("%.15f\n",c[0]); 83 memset(c,0x7f,sizeof c);printf("%.15f\n",c[0]); 84 memset(c,0x80,sizeof c);printf("%.15f\n",c[0]); 85 } 86 void _double() 87 { 88 puts(""); 89 puts("double:"); 90 91 puts(""); 92 memset(d,0xfd,sizeof d);printf("%lf\n",d[0]); 93 memset(d,0xfe,sizeof d);printf("%lf\n",d[0]); 94 memset(d,0xff,sizeof d);printf("%lf\n",d[0]); 95 96 puts(""); 97 memset(d,0xc1,sizeof d);printf("%lf %lf\n",d[0],d[0]*2); 98 memset(d,0xc2,sizeof d);printf("%lf %lf\n",d[0],d[0]*2); 99 memset(d,0xc3,sizeof d);printf("%lf %lf\n",d[0],d[0]*2); 100 101 puts(""); 102 memset(d,0x42,sizeof d);printf("%lf %lf\n",d[0],d[0]*2); 103 memset(d,0x43,sizeof d);printf("%lf %lf\n",d[0],d[0]*2); 104 memset(d,0x44,sizeof d);printf("%lf %lf\n",d[0],d[0]*2); 105 106 puts(""); 107 memset(d,0x7e,sizeof d);printf("%lf\n",d[0]); 108 memset(d,0x7f,sizeof d);printf("%lf\n",d[0]); 109 memset(d,0x80,sizeof d);printf("%lf\n",d[0]); 110 } 111 int main() 112 { 113 _int(); 114 _long(); 115 _float(); 116 _double(); 117 }