gpt4 book ai didi

c++ - std::unordered_set::load_factor,为什么是 float 而不是 double?

转载 作者:太空狗 更新时间:2023-10-29 20:19:39 26 4
gpt4 key购买 nike

据我所知,floatdouble 之间最快的类型取决于 native ALU 实现,它通常基于 double 。当你根据反向精度进行计算时,ALU必须一直做相应的精度转换。

那么,为什么标准选择 float 来表示 load_factor?我想这是为了节省对哈希表容器的内存思考,但我想知道是否有更充分的理由。

最佳答案

这发生在 revision 3 of the original proposal 中:

Changed load factor operations to use float instead of double

稍后给出基本原理(在“E. 哈希调整大小的控制”下):

Should the floating-point parameter be of type float, or of type double? It makes very little difference. On the one hand, double is typically the "natural" floating-point type that is used in the absence of a strong reason to the contrary. On the other hand, float may allow the hash table implementation to save some space, and may alert users to the fact that the value will not be used in any context that involves high precision. I have chosen float.

基本上就是你所说的。

至于性能,这里提到了这一点,并且它在宏伟的计划中并不重要(尽管在捍卫浮点整数的使用的背景下):

The cost of a runtime floating-point parameter is one floating-point multiplication at every rehash (not every insertion). Even with incremental hashing, this is almost certain to be dwarfed by the cost of a rehash.

关于c++ - std::unordered_set::load_factor,为什么是 float 而不是 double?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57450286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com