Skip to content

Conversation

@kubsztal
Copy link

Corrected rounding errors by evaluating the number of significant digits in the real number and applying the .16g format only when more than 16 significant digits are present.

@jpgrayson
Copy link
Collaborator

Hi @kubsztal. Thank you for this PR, but I am not going to accept it.

From the VCD spec, IEEE 1364-2005, 18.2.1 Syntax of four-state VCD file:

A real number is dumped using a %.16g printf() format. This preserves the precision of that number by
outputting all 53 bits in the mantissa of a 64-bit IEEE 754 double-precision number. Application programs
can read a real number using a %g format to scanf().

The implementation in pyvcd is guided by that spec.

From #38:

the example 99.588 is generated incorrectly as r99.58799999999999

I don't think the formatted representation is incorrect; it is an artifact of the input and output being a base-10 representation, but the underlying floating point having a base-2 representation. A pretty good explanation of this can be found in the Python docs, here.

In short, 99.588 and 99.58799999999999 are both valid decimal representations of the same floating point value.

@kubsztal
Copy link
Author

Thanks for the feedback @jpgrayson! You're right, it doesn't make sense to implement something that goes against the spec - I didn't realize the precision was explicitly defined there. I'll reach out to the GTKWave team to see if they can loosen up their interpretation of real values, since it looks like they're sticking to %.16g as well instead of just %g (or %lg).

@kubsztal kubsztal closed this Jun 23, 2025
@kubsztal kubsztal deleted the feature/rounding-errors-fix branch June 23, 2025 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants