1

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. It is writing this value to a csv. In the same csv I am calculating the difference between consecutive timestamps.

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. Any idea why I get all 1s in cpp but mostly 1s and occasionally 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?

bchang32
  • 27
  • 3
  • Could you provide more context on what a PPS is? – Aroic Jul 21 '20 at 17:31
  • 3
    Probably very tightly related: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – user4581301 Jul 21 '20 at 17:33
  • @Aroic I'm operating off of Pulse Per Second, but you're right. It could easily be Pizza Party Surprise. – user4581301 Jul 21 '20 at 17:34
  • How are you reading and procesing the CSV? Are these floating point numbers? The python `decimal.Decimal` package may give you better precision than `float`. – tdelaney Jul 21 '20 at 17:53
  • Perhaps not related but this issue might be that the smallest 'step' many timestamps take is a nanosecond, so if your timings are this fast their accuracy might get lost to 'just 1s' – Jay Jul 21 '20 at 21:24
  • @Aroic, a PPS is a pulse per second. Its what I'm using to keep the system in sync with other hardware. – bchang32 Jul 22 '20 at 11:55
  • @Jake, right, our timestamps are on the nanosecond level so being off by E-14,E-15 bewilders me. We're also reading the timestamp off a U64 and U32 register (and concatenating the values) before writing it to a csv, so it should be 'exact' and have no hidden/lost values. When I do the math in cpp, I get the perfect 1.0, but in python, its off by E-14,15 level but exponentially growing too. – bchang32 Jul 22 '20 at 11:56

1 Answers1

0

@tdelaney's and user4581301's comments hit the nail on the head. It appears that it was due to the precision of python's floating type. I replaced it with the mentioned decimal.Decimal and it kept the original CSVs precision and calculated the data with no rounding.

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 A-B.

bchang32
  • 27
  • 3