-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.xml
272 lines (209 loc) · 19 KB
/
rss.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>ywdxz</title>
<link>http://ywdxz.github.io/</link>
<description>一天天的</description>
<generator>Hugo 0.79.0 https://gohugo.io/</generator>
<language>zh-CN</language>
<managingEditor>[email protected] (xz)</managingEditor>
<webMaster>[email protected] (xz)</webMaster>
<copyright>[CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh)</copyright>
<lastBuildDate>Fri, 11 Dec 2020 14:53:37 +0800</lastBuildDate>
<atom:link rel="self" type="application/rss+xml" href="http://ywdxz.github.io/rss.xml" />
<item>
<title>关于math.NaN() 问题记录</title>
<link>http://ywdxz.github.io/posts/2020-12-11-3/</link>
<guid isPermaLink="true">http://ywdxz.github.io/posts/2020-12-11-3/</guid>
<pubDate>Fri, 11 Dec 2020 14:40:49 +0800</pubDate>
<author>[email protected] (xz)</author>
<copyright>[CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh)</copyright>
<description><h2 id="问题">问题</h2>
<p>看 map 源码看到一个测试case,<strong>为什么 k == k</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">func</span> <span class="nf">testMapNan</span><span class="p">(</span><span class="nx">t</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">,</span> <span class="nx">m</span> <span class="kd">map</span><span class="p">[</span><span class="kt">float64</span><span class="p">]</span><span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nx">m</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">3</span> <span class="p">{</span>
<span class="nx">t</span><span class="p">.</span><span class="nf">Error</span><span class="p">(</span><span class="s">&#34;length wrong&#34;</span><span class="p">)</span>
<span class="p">}</span>
<span class="nx">s</span> <span class="o">:=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">m</span> <span class="p">{</span>
<span class="k">if</span> <span class="nx">k</span> <span class="o">==</span> <span class="nx">k</span> <span class="p">{</span> <span class="c1">// why ?????
</span><span class="c1"></span> <span class="nx">t</span><span class="p">.</span><span class="nf">Error</span><span class="p">(</span><span class="s">&#34;nan disappeared&#34;</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">v</span> <span class="o">&amp;</span> <span class="p">(</span><span class="nx">v</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">!=</span> <span class="mi">0</span> <span class="p">{</span>
<span class="nx">t</span><span class="p">.</span><span class="nf">Error</span><span class="p">(</span><span class="s">&#34;value wrong&#34;</span><span class="p">)</span>
<span class="p">}</span>
<span class="nx">s</span> <span class="o">|=</span> <span class="nx">v</span>
<span class="p">}</span>
<span class="k">if</span> <span class="nx">s</span> <span class="o">!=</span> <span class="mi">7</span> <span class="p">{</span>
<span class="nx">t</span><span class="p">.</span><span class="nf">Error</span><span class="p">(</span><span class="s">&#34;values wrong&#34;</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="c1">// nan is a good test because nan != nan, and nan has
</span><span class="c1">// a randomized hash value.
</span><span class="c1"></span><span class="kd">func</span> <span class="nf">TestMapAssignmentNan</span><span class="p">(</span><span class="nx">t</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">m</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="kt">float64</span><span class="p">]</span><span class="kt">int</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="nx">nan</span> <span class="o">:=</span> <span class="nx">math</span><span class="p">.</span><span class="nf">NaN</span><span class="p">()</span>
<span class="c1">// Test assignment.
</span><span class="c1"></span> <span class="nx">m</span><span class="p">[</span><span class="nx">nan</span><span class="p">]</span> <span class="p">=</span> <span class="mi">1</span>
<span class="nx">m</span><span class="p">[</span><span class="nx">nan</span><span class="p">]</span> <span class="p">=</span> <span class="mi">2</span>
<span class="nx">m</span><span class="p">[</span><span class="nx">nan</span><span class="p">]</span> <span class="p">=</span> <span class="mi">4</span>
<span class="nf">testMapNan</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span> <span class="nx">m</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="结论">结论</h2>
<p>eg:</p>
<pre><code>func main() {
m := make(map[float64]int)
m[1.4] = 1
m[2.4] = 2
m[math.NaN()] = 3
m[math.NaN()] = 3
for k, v := range m {
fmt.Printf(&quot;[%v, %d] &quot;, k, v)
}
fmt.Printf(&quot;\nk: %v, v: %d\n&quot;, math.NaN(), m[math.NaN()])
fmt.Printf(&quot;k: %v, v: %d\n&quot;, 2.400000000001, m[2.400000000001])
fmt.Printf(&quot;k: %v, v: %d\n&quot;, 2.4000000000000000000000001, m[2.4000000000000000000000001])
fmt.Println(math.NaN() == math.NaN())
}
</code></pre><p>输出:</p>
<pre><code>[2.4, 2] [NaN, 3] [NaN, 3] [1.4, 1]
k: NaN, v: 0
k: 2.400000000001, v: 0
k: 2.4, v: 2
false
</code></pre><p>结论:
NaN 不等于 NaN,<strong>为什么NaN!=NaN呢?</strong></p>
</description>
</item>
<item>
<title>环境变量整理</title>
<link>http://ywdxz.github.io/posts/2020-12-11-1/</link>
<guid isPermaLink="true">http://ywdxz.github.io/posts/2020-12-11-1/</guid>
<pubDate>Fri, 11 Dec 2020 11:38:25 +0800</pubDate>
<author>[email protected] (xz)</author>
<copyright>[CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh)</copyright>
<description><p>经常会用到需要手工设置linux环境变量的情况,每次都是google一下,这边把结果整理一下作为知识储备。</p>
<h3 id="临时设置">临时设置</h3>
<ul>
<li>直接执行 export GOBACTRACE=crash: 仅对当前shell生效</li>
<li>env GOBACTRACE=crash</li>
<li>set GOBACTRACE=crash</li>
</ul>
<p><strong>set、env、export</strong></p>
<ul>
<li>set:显示/设置 shell变量,包括私有变量和用户变量</li>
<li>env:显示/设置 用户变量</li>
<li>export:显示/设置 当前导出成用户变量的shell变量</li>
</ul>
<p>linux变量分为shell变量和用户变量,export是一个命令,可以将shell变量导出成用户变量。这些变量都可以通过unset来清除掉。</p>
<h3 id="永久设置">永久设置</h3>
<p>在如下的文件中加上 export GOBACTRACE=crash,然后source一下该文件就可以永久生效。一般选项用bash_profile或者bashrc</p>
<ul>
<li>/etch/profile:对全部用户生效</li>
<li>~/.bash_profile:对当前用户生效</li>
<li>~/.bashrc:启动新的shell是读取并执行</li>
<li>~/.bash_login:shell登录是读取并执行</li>
<li>~/.bash_logout:shell退出登录时读取并执行</li>
</ul>
<p>shell的初始化顺序如下:</p>
<ul>
<li>\1. 执行 /etc/profile 的命令,如果存在的话</li>
<li>\2. 执行 ~/.bash_profile 的命令,如果存在的话</li>
<li>\3. 执行 ~/.bash_login 的命令,如果存在的话</li>
<li>\4. 执行 ~/.bashrc 的命令,如果存在的话</li>
</ul>
<p>ps:通过在如上的文件中设置一些命令,可以在用户登录时就执行一些操作,如cd到指定目录</p>
</description>
</item>
<item>
<title>浮点数整理</title>
<link>http://ywdxz.github.io/posts/2020-12-11-2/</link>
<guid isPermaLink="true">http://ywdxz.github.io/posts/2020-12-11-2/</guid>
<pubDate>Mon, 30 Nov 2020 23:18:10 +0800</pubDate>
<author>[email protected] (xz)</author>
<copyright>[CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh)</copyright>
<description><h1 id="浮点数整理">浮点数整理</h1>
<h2 id="标准">标准</h2>
<p>根据国际标准IEEE 754,任意一个二进制浮点数可以表示成下面科学计数的形式:</p>
<pre><code>value = (-1)^sign * fraction * 2^exponent
</code></pre><h2 id="内存布局">内存布局</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-go" data-lang="go"> <span class="o">-----------------------</span>
<span class="p">|</span> <span class="nx">sign</span><span class="p">|</span><span class="nx">exponent</span><span class="p">|</span><span class="nx">fraction</span><span class="p">|</span>
<span class="o">-----------------------</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="sign">sign</h2>
<p>符号位,1 byte.( 0 表示正数,1 表示负数)</p>
<h2 id="exponent">exponent</h2>
<p>指数位,单精度 8 byte,双精度 23 byte</p>
<p>exponent 是一个无符号整数,但 exponent 需要表示负数。所以IEEE 754规定,exponent 的真实值必须再减去一个中间数。对于单精度,中间数为:<strong>127</strong>;对于双精度,中间数为:<strong>1023</strong>。</p>
<p><strong>Top</strong>:</p>
<ol>
<li>exponent 二进制全为 1 时:fraction 全为 0,表示无穷大(±INF),否则表示不是一个数 (NaN)</li>
<li>exponent 二进制全为 0 时:exponent 真实值为(1-127/1-1023),fraction xxxxx 表示的真实值为 0.xxxxx.(方便表示±0,以及接近于0很小的数字)</li>
</ol>
<h2 id="fraction">fraction</h2>
<p>有效数字,单精度 11 byte,双精度 52 byte</p>
<p>(1 &lt;= fraction &lt; 2 ),默认这个数总是 1.xxxx 的形式,因此1可以舍去,只保存后面的xxxxxx部分</p>
<p><strong>Top</strong>: exponent 二进制全为 0 时,默认这个数总为 0.xxxx 的形式,因此0可以舍去,只保存后面的xxxxxx部分</p>
<h2 id="十进制小数转换为二进制小数">十进制小数转换为二进制小数</h2>
<p> 十进制小数转换成二进制小数采用&quot;乘2取整,顺序排列&quot;法。</p>
<p> 具体做法是:用2乘十进制小数,可以得到积,将积的整数部分取出,再用2乘余下的小数 部分,又得到一个积,再将积的整数部分取出,如此进行,直到积中的小数部分为零,或者达到所要求的精度为止。</p>
<p>eg: 把(0.8125)转换为二进制小数</p>
<p>0.8125 *2 = 1.625 ==&gt; 1</p>
<p>0.625 * 2 = 1.25 ==&gt; 1</p>
<p>0.25 * 2 = 0.5 ==&gt; 0</p>
<p>0.5 * 2 = 1 ==&gt; 1</p>
<p>所以:(0.8125)10 ==&gt;(0.1101)2</p>
<h2 id="浮点数编码演示">浮点数编码演示</h2>
<p>eg: float32(0.5)</p>
<p>sign = 0</p>
<p>exponent = 127</p>
<p>fraction = 1</p>
</description>
</item>
</channel>
</rss>