talent-plan / tinysql

A course to build the SQL layer of a distributed database.
Apache License 2.0
1.7k stars 519 forks source link

fix default unsigned integers judgment #145

Closed Chasing1020 closed 1 year ago

Chasing1020 commented 2 years ago

What problem does this PR solve?

What is changed and how it works?

I have written a demo as follows.

func main() {
    fmt.Printf("runtime.GOARCH: %s\n", runtime.GOARCH)
    var x, y uint64 = 73, 1040
    fmt.Printf("x: %d, y: %d\n", x, y)
    fmt.Printf("int(math.Abs(float64(x-y))): %d\n", int(math.Abs(float64(x-y))))
    fmt.Printf("int(-math.Abs(float64(x-y))): %d\n", int(-math.Abs(float64(x-y))))
}

It will get different results depending on whether the system is arm64 or amd64.

// in GOARCH="amd64" GOOS="linux"
runtime.GOARCH: amd64
x: 73, y: 1040
int(-math.Abs(float64(x-y))): -9223372036854775808
int(math.Abs(float64(x-y))): -9223372036854775808

// in GOARCH="arm64" GOOS="darwin"
runtime.GOARCH: arm64
x: 73, y: 1040
int(-math.Abs(float64(x-y))): -9223372036854775808
// c.Assert(int(math.Abs(float64(x-y))), LessEqual, 2) will failed
int(math.Abs(float64(x-y))): 9223372036854775807

In order to avoid unnecessary misunderstandings among students using the MacBook with Apple silicon, I suggest adding a condition runtime.GOARCH == "arm64" to the judgment.