简体   繁体   English

CPP 和 Python csv 精度

[英]CPP and Python csv Precision

First time posting so hopefully I can relate what I'm trying to ask.第一次发帖,希望我能把我想问的问题联系起来。

I have cpp code that records timestamps down to the nanosecond level from an fpga.我有 cpp 代码,可以将时间戳从 fpga 记录到纳秒级。 It is writing this value to a csv.它将这个值写入 csv。 In the same csv I am calculating the difference between consecutive timestamps.在同一个 csv 中,我正在计算连续时间戳之间的差异。

When I export it to python and do the timestamp difference, I get mostly 1s (its based off a PPS), but also random impulse points.当我将它导出到 python 并进行时间戳差异时,我得到的主要是 1s(它基于 PPS),但也有随机脉冲点。 Any idea why I get all 1s in cpp but mostly 1s and occasionally 1 +- 3E-14?知道为什么我在 cpp 中得到所有 1,但主要是 1,偶尔是 1 +- 3E-14?

any info or guidance would be appreciated.任何信息或指导将不胜感激。 From using search, it seems like it could be due to floating points?从使用搜索来看,似乎可能是由于浮点数? but shouldnt that happen for both?但这不应该发生在两者身上吗?

@tdelaney's and user4581301's comments hit the nail on the head. @tdelaney 和 user4581301 的评论一针见血。 It appears that it was due to the precision of python's floating type.看来这是由于python的浮动类型的精度。 I replaced it with the mentioned decimal.Decimal and it kept the original CSVs precision and calculated the data with no rounding.我用上面提到的 decimal.Decimal 替换了它,它保持了原始的 CSV 精度并计算了没有四舍五入的数据。

Decimal doesn't rely on binary fractions and the only downside is that it is slower (which in my case is fine).十进制不依赖于二进制分数,唯一的缺点是它速度较慢(在我的情况下这很好)。

The difference between my cpp and python code was that my cpp code was doing the math as uints and I was calculating it manually vs just AB.我的 cpp 和 python 代码之间的区别在于,我的 cpp 代码将数学作为 uint 进行计算,而我是手动计算而不是 AB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM