首页 > 代码库 > 如何对double型变量进行memset获得极大值或极小值

如何对double型变量进行memset获得极大值或极小值

我们都知道,如果想对int型变量清极大值或极小值,我们一般选择memset(a,0x3f,sizeof a);或者memset(a,0xef,sizeof a);。然而,如果对double型清0x3f,我们经常会得到一个连1都不到的小数。那么对double清极值是否智能手动或者fill(a+1,a+n,0x7fffffff);呢?

首先我们运行以下程序

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    double x;
    freopen("1.txt","w",stdout);
    for(int i=1;i<=0xff;i++)
        memset(&x,i,sizeof x),printf("%x %.2lf\n",i,x);
}

得到这个表:

1 0.00

2 0.00

3 0.00

4 0.00

5 0.00

6 0.00

7 0.00

8 0.00

9 0.00

a 0.00

b 0.00

c 0.00

d 0.00

e 0.00

f 0.00

10 0.00

11 0.00

12 0.00

13 0.00

14 0.00

15 0.00

16 0.00

17 0.00

18 0.00

19 0.00

1a 0.00

1b 0.00

1c 0.00

1d 0.00

1e 0.00

1f 0.00

20 0.00

21 0.00

22 0.00

23 0.00

24 0.00

25 0.00

26 0.00

27 0.00

28 0.00

29 0.00

2a 0.00

2b 0.00

2c 0.00

2d 0.00

2e 0.00

2f 0.00

30 0.00

31 0.00

32 0.00

33 0.00

34 0.00

35 0.00

36 0.00

37 0.00

38 0.00

39 0.00

3a 0.00

3b 0.00

3c 0.00

3d 0.00

3e 0.00

3f 0.00

40 32.50

41 2261634.51

42 156842099844.52

43 10843961455707782.00

44 747708026454360460000.00

45 51429126632076458000000000.00

46 3529536965341344500000000000000.00

47 241737052174609690000000000000000000.00

48 16525713066466281000000000000000000000000.00

49 1127805529726474100000000000000000000000000000.00

4a 76846329228755865000000000000000000000000000000000.00

4b 5228514198248326500000000000000000000000000000000000000.00

4c 355259342137649310000000000000000000000000000000000000000000.00

4d 24108255004517747000000000000000000000000000000000000000000000000.00

4e 1634089943872471700000000000000000000000000000000000000000000000000000.00

4f 110639270315220550000000000000000000000000000000000000000000000000000000000.00

50 7556001431015456300000000000000000000000000000000000000000000000000000000000000.00

51 525663347308138420000000000000000000000000000000000000000000000000000000000000000000.00

52 36446967223631734000000000000000000000000000000000000000000000000000000000000000000000000.00

53 2519470002541514500000000000000000000000000000000000000000000000000000000000000000000000000000.00

54 173693439909239170000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

55 11945305291614955000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

56 819687411242631950000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

57 56133372798172279000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

58 3836982815172030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

59 261830011167902370000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

5a 17838867517321418000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

5b 1213624708152897500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

5c 82454865162444373000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

5d 5595045655437669700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

5e 379212874880737930000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

5f 25673651826636406000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

60 1756580577739302900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

61 122176384420437770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

62 8469446924576528300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

63 585363771868790560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

64 40348802750122149000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

65 2774480017624350000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

66 190359856625529320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

67 13034546188519292000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

68 890874679343703260000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

69 60785776373046065000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

6a 4141035668152201600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

6b 281700905511843030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

6c 19137488320965074000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

6d 1298492692778582300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

6e 88001147761747392000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

6f 5957502789848767900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

70 408354876418796630000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

71 28396262443943004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

72 1968084071671644300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

73 135999696916777530000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

74 9372895550667632100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

75 644409915093636340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

76 44207816532211711000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

77 3026687417964748600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

78 206842847014058100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

79 14111782168453345000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

7a 961276249046606020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

7b 65386757613253666000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

7c 4441723041807660500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

7d 301351536995410160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

7e 20421693454608858000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

7f 1382417208487871500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

80 -0.00

81 -0.00

82 -0.00

83 -0.00

84 -0.00

85 -0.00

86 -0.00

87 -0.00

88 -0.00

89 -0.00

8a -0.00

8b -0.00

8c -0.00

8d -0.00

8e -0.00

8f -0.00

90 -0.00

91 -0.00

92 -0.00

93 -0.00

94 -0.00

95 -0.00

96 -0.00

97 -0.00

98 -0.00

99 -0.00

9a -0.00

9b -0.00

9c -0.00

9d -0.00

9e -0.00

9f -0.00

a0 -0.00

a1 -0.00

a2 -0.00

a3 -0.00

a4 -0.00

a5 -0.00

a6 -0.00

a7 -0.00

a8 -0.00

a9 -0.00

aa -0.00

ab -0.00

ac -0.00

ad -0.00

ae -0.00

af -0.00

b0 -0.00

b1 -0.00

b2 -0.00

b3 -0.00

b4 -0.00

b5 -0.00

b6 -0.00

b7 -0.00

b8 -0.00

b9 -0.00

ba -0.00

bb -0.00

bc -0.00

bd -0.00

be -0.00

bf -0.12

c0 -8577.51

c1 -595821443.51

c2 -41255400998277.52

c3 -2848394305499268600.00

c4 -196154140339432450000000.00

c5 -13476555094338099000000000000.00

c6 -923923411592226570000000000000000.00

c7 -63219126011291991000000000000000000000.00

c8 -4318036447754703500000000000000000000000000.00

c9 -294449594579902470000000000000000000000000000000.00

ca -20048271934734512000000000000000000000000000000000000.00

cb -1363115719988382000000000000000000000000000000000000000000.00

cc -92559631349317831000000000000000000000000000000000000000000000.00

cd -6277438562204192500000000000000000000000000000000000000000000000000.00

ce -425255837650091480000000000000000000000000000000000000000000000000000000.00

cf -28777739825156522000000000000000000000000000000000000000000000000000000000000.00

d0 -1993854408381186000000000000000000000000000000000000000000000000000000000000000000.00

d1 -138470391314097430000000000000000000000000000000000000000000000000000000000000000000000.00

d2 -9586051653338756600000000000000000000000000000000000000000000000000000000000000000000000000.00

d3 -661737160148046550000000000000000000000000000000000000000000000000000000000000000000000000000000.00

d4 -45563434706068069000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

d5 -3129951052409979500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

d6 -214555482385487100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

d7 -14679178779048922000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

d8 -1002520540904329100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

d9 -68355779549259894000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

da -4653735800328269100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

db -316388621163970020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

dc -21482046286569577000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

dd -1456815990147462900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

de -98683099208633679000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

df -6677614145500822200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

e0 -463468064771798250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

e1 -32180465730379564000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

e2 -2227377823277027000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

e3 -153732818170537500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

e4 -10583553036438193000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

e5 -726930037227653690000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

e6 -49824159672519490000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

e7 -3408411520223639500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

e8 -232754178434599600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

e9 -15868539665109664000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

ea -1080249646601300100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

eb -73435622782400814000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

ec -4985717045666697800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

ed -338084306397821390000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

ee -22899894549927040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

ef -1549473939178550500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

f0 -107730874267432140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

f1 -7478635795308384200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

f2 -517539169214551540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

f3 -35714397827745240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

f4 -2458343745902715200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

f5 -168827860796461440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

f6 -11570099379335498000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

f7 -791405925073819980000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

f8 -54037958965559871000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

f9 -3683800791481180300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

fa -250751870841351620000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

fb -17044745290527055000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

fc -1157117770045540900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

fd -78459116763945423000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

fe -5314010372517807600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

ff -1.#R


看了这么大的表,不难发现,对于double型变量,当我们想要清极大值的时候,我们可以选择0x7f,当然如果觉得这个数字过于夸张,我们可以选择0x42或者0x43。同样,想清最小值的时候,可以选择0xfe或0xc2。

如何对double型变量进行memset获得极大值或极小值