Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ch1-basic/ch1-03-array-string-and-slice#622

giscus[bot]bot started this conversation inGeneral
Mar 26, 2023· 6 comments· 6 replies
Discussion options

ch1-basic/ch1-03-array-string-and-slice

Go语言高级编程

https://chai2010.cn/advanced-go-programming-book/ch1-basic/ch1-03-array-string-and-slice.html

You must be logged in to vote

Replies: 6 comments 6 replies

Comment options

成功数 = copy(目标数据, 原数据)

You must be logged in to vote
0 replies
Comment options

SortFloat64FastV这种排序有问题,float64类型指数的起始数为011 1111 1111, -1.0内存里存的是1 011 1111 1111 0....,-2.0存的是1 100 0000 0000 0....,这就导致了排序后-2.0>-1.0

You must be logged in to vote
2 replies
@gocppluagiscus
Comment options

你的机器32位 还是 64位

@TwinklerGgiscus
Comment options

64位机器的int也是32位的

Comment options

func main() {
a := []float64{1.1, -1.1, 0, 1, 2, 3, 4, -10}
SortFloat64FastV2(a)
fmt.Println(a)
}

func SortFloat64FastV2(a []float64) {
// 通过 reflect.SliceHeader 更新切片头部信息实现转换
var c []int
aHdr := (*reflect.SliceHeader)(unsafe.Pointer(&a))
cHdr := (*reflect.SliceHeader)(unsafe.Pointer(&c))
*cHdr = *aHdr

// 以 int 方式给 float64 排序sort.Ints(c)

}

output: [-1.1 -10 0 1 1.1 2 3 4]

确实有问题

You must be logged in to vote
0 replies
Comment options

int 不是 32bit吗?为什么可以吧float64转换成int?

You must be logged in to vote
3 replies
@miyu-miyugiscus
Comment options

没注意到注释说用的是64位机器,当我没说过

@miziaibagiscus
Comment options

64位机器,int也是32位啊,long才是64位

@mck753giscus
Comment options

@miziaiba 64位机器,int不是64位吗

Comment options

写的还是很好的

You must be logged in to vote
0 replies
Comment options

用 for range 方式迭代的性能可能会更好一些

You must be logged in to vote
1 reply
@arvin-ydgiscus
Comment options

针对这个问题,特地做了一下测试,fori的性能更好一些

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
10 participants
@miziaiba@wan2435203490@PaiGack@gocpplua@mck753@miyu-miyu@Cyberpunk314@8vd8@TwinklerG@arvin-yd

[8]ページ先頭

©2009-2025 Movatter.jp