-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
slice: 聚合函数 Max, Min 和 Sum #82
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #82 +/- ##
==========================================
+ Coverage 95.90% 95.99% +0.09%
==========================================
Files 21 22 +1
Lines 879 899 +20
==========================================
+ Hits 843 863 +20
Misses 28 28
Partials 8 8
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
@longyue0521 你有什么建议吗?关于 types 的 |
func Max[T types.RealNumber](ts []T) T { | ||
res := ts[0] | ||
for i := 1; i < len(ts); i++ { | ||
if ts[i] > res { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
关于float类型我有点担心,使用">"和"<"比较不知道是否要处理下面的情况
var a float64 = 1.0000000000000001
var b float64 = 1.000000000000000001
var c float64 = 1.000000000000001
var d float64 = 1.0000000000000000001
fmt.Println(a == b) //true
fmt.Println(a > b) //false
fmt.Println(c == d) //false
fmt.Println(c > d) //true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我觉得我们处理不了这种精度问题,用户即便自己手写代码,如果他追求这种精确数字,那么实际上不能使用 float,要使用 decimal。
所以我觉得,一种方案是把 float 的丢掉,只处理 uint 和 int 族;另外一种就是我们不管,因为当用户使用 float 的时候就应该意识到这种不精确的风险。
下面的求和也是类似的。如果他们寻求精确表达,那么就不应该使用 float,如果不寻求的话,那么 Go 对 float 的支持就够了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flycash 明确意图,把float32和float64踢出去吧,把RealNumber改为Integer? 我觉得Number接口里可以放float32和float64.
testMinTypes[int16](t) | ||
testMinTypes[int32](t) | ||
testMinTypes[int64](t) | ||
testMinTypes[float32](t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
testMaxTypes[int16](t) | ||
testMaxTypes[int32](t) | ||
testMaxTypes[int64](t) | ||
testMaxTypes[float32](t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
float32和float64可能需要考虑带小数点的情况
testSumTypes[int16](t) | ||
testSumTypes[int32](t) | ||
testSumTypes[int64](t) | ||
testSumTypes[float32](t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
将挎包的公共接口,比如Comparable 接口, 直接放在ekit根目录下怎么样,文件名types.go和interface.go都行 |
6c94b5b
to
bdce2f3
Compare
挪过去了 |
@flycash constrain是动词,文件名改为constraints.go吧 |
float 的问题,我决定让用户为自己的行为负责。这就好比,在 Go 语法层面上支持 float32 的加减,那么就意味着 Go SDK 的人觉得,我们使用 float 应该要自己管理精度问题 |
另外一个值得讨论的是 types 包。这个我是准备用来放一些公共接口,和泛型约束。目前这里定义了两个泛型约束,
我预计将来在实现树结构的时候,这里会放 Comparable 接口;在实现 HashMap 的时候,会放 Hashable 接口... 这一类的接口,我预期中并不会在专门一个地方使用。例如 Comparable 接口,可以用在树形结构里面,也可以用在 SkipList 里面.
因此需要一个公共的地方来定义类似的接口。
我选择了 types。