I have a program written in C ++ that generates C source code for mathematical calculations. I noticed that constants take up a lot of space in the generated code and are looking for a more compact representation.
To generate constants, I now use:
double v = ...
cfile << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1) << v;
I am sure that this performance is lossless, but it is also very bloated. For example, zero and one will be represented as something like 0.0000000000000000e + 00 and 1.0000000000000000e + 00. And "0." or "1." carries as much information as possible.
Is there a way to print constants for a file in a more compact but lossless way? It doesn’t need to look good for a human reader, just compile it if it is present in simple C code (if C99, I would prefer it to be also valid C ++). Hexadecimal may be ok if it is portable.
EDIT: Removed std::fixedin code snippet.
source
share