Go中的“魔数”

src/math/bits.go:

const SmallestNormal = 2.2250738585072014e-308 // 2**-1022

Numeric limitations



src/math/cbrt.go

1
2
3
4
5
6
7
8
9
10
const (
B1 = 715094163 // (682-0.03306235651)*2**20
B2 = 696219795 // (664-0.03306235651)*2**20
C = 5.42857142857142815906e-01 // 19/35 = 0x3FE15F15F15F15F1
D = -7.05306122448979611050e-01 // -864/1225 = 0xBFE691DE2532C834
E = 1.41428571428571436819e+00 // 99/70 = 0x3FF6A0EA0EA0EA0F
F = 1.60714285714285720630e+00 // 45/28 = 0x3FF9B6DB6DB6DB6E
G = 3.57142857142857150787e-01 // 5/14 = 0x3FD6DB6DB6DB6DB7
SmallestNormal = 2.22507385850720138309e-308 // 2**-1022 = 0x0010000000000000
)



本地队列中的g被执行61次后,全局队列中的g得到一次被执行的机会


为什么是61?


最早是C写的,后来改成Go

https://codereview.appspot.com/10042044
最原始的提交就是61
检视中还讨论到了59[旺柴]

Issue 10042044: code review 10042044: runtime: improve scheduler fairness (Closed)

tech-talk之magicNumber-61



16294579238595022365

https://tieba.baidu.com/p/7592696329

https://www.google.com/search?q=16294579238595022365&oq=16294579238595022365&aqs=chrome..69i57j69i60.196j0j4&sourceid=chrome&ie=UTF-8


时间模块


1885年和「回到未来3」&希尔山谷,1977-5-25和「星球大战」

还有更出名的2006-01-02 15:04:05,Go团队对时间模块“魔数”的引入,各种天马行空和意想不到的彩蛋。用吴站长话说,“颇具浪漫主义气息啊”

有意思!Go 源代码中的那些秘密:为什么 time.minWall 是 1885?

time/time.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const (
// The unsigned zero year for internal calculations.
// Must be 1 mod 400, and times before it will not compute correctly,
// but otherwise can be changed at will.
absoluteZeroYear = -292277022399

// The year of the zero Time.
// Assumed by the unixToInternal computation below.
internalYear = 1

// Offsets to convert between internal and absolute or Unix times.
absoluteToInternal int64 = (absoluteZeroYear - internalYear) * 365.2425 * secondsPerDay
internalToAbsolute = -absoluteToInternal

unixToInternal int64 = (1969*365 + 1969/4 - 1969/100 + 1969/400) * secondsPerDay
internalToUnix int64 = -unixToInternal

wallToInternal int64 = (1884*365 + 1884/4 - 1884/100 + 1884/400) * secondsPerDay
internalToWall int64 = -wallToInternal
)