日更系列之c++的to |
您所在的位置:网站首页 › std::to_string › 日更系列之c++的to |
一、背景 做了一个根据搜索词计算embedding向量的服务,但是算法同学发现新服务打分精度变低了,原来能保存到小数点后16位的,现在打分只有小数点后6位。 二、单精度双精度浮点数看到这问题,首先怀疑的是double类型数据被强转float类型,导致精度丢失。 float类型大概精度就是到小数点之后6,7位,来做个实验 int main(int argc, char**argv){ float a = 0.0123456789012; std::cout vecitem(0).vecscore(i)打印出来,这个数据精度是小数点后16位没问题,但是context->m_query_embedding是小数点后6问题。显然是这个std::to_string出现了精读丢失的问题。果不其然,谷歌搜索到了这个帖子,https://stackoverflow.com/questions/16605967/set-precision-of-stdto-string-when-converting-floating-point-values 谈到to_stiring默认设置精度为6位,如果需要更高精度需要这样设置。 #include template std::string to_string_with_precision(const T a_value, const int n = 6) { std::ostringstream out; out.precision(n); out |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |