mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-02 05:04:08 +08:00
Matrix: correct print buffer size prediction
Apparently printf of %8.8g can result in 8 significant digits + dot + scientific notation e.g. "e+12" = 14 bytes
This commit is contained in:
parent
2cca35c8fe
commit
c6db357c92
@ -352,8 +352,8 @@ public:
|
||||
|
||||
void print(FILE *stream = stdout) const
|
||||
{
|
||||
// element: tab, point, 8 digits; row: newline; string: \0 end
|
||||
static const size_t n = 10*N*M + M + 1;
|
||||
// element: tab, point, 8 digits, 4 scientific notation chars; row: newline; string: \0 end
|
||||
static const size_t n = 15*N*M + M + 1;
|
||||
char * buf = new char[n];
|
||||
write_string(buf, n);
|
||||
fprintf(stream, "%s\n", buf);
|
||||
|
||||
@ -212,16 +212,22 @@ int main()
|
||||
|
||||
// check write_string()
|
||||
float comma[6] = {
|
||||
1.f, 12345.678f,
|
||||
12345.67891f, 12345.67891f,
|
||||
1112345.67891f, 12345.111111111f
|
||||
1.f, 12345.123f,
|
||||
12345.1228f, .1234567891011f,
|
||||
12345678910.123456789f, 1234567891011.123456789101112f
|
||||
};
|
||||
Matrix<float, 3, 2> Comma(comma);
|
||||
const size_t len = 10*2*3 + 2 + 1;
|
||||
const size_t len = 15*2*3 + 2 + 1;
|
||||
char buffer[len];
|
||||
Comma.print(); // for debugging in case of failure
|
||||
Comma.write_string(buffer, len);
|
||||
char output[] = "\t 1\t12345.678\n\t12345.679\t12345.679\n\t1112345.6\t12345.111\n";
|
||||
printf("%s\n", buffer); // for debugging in case of failure
|
||||
char output[] = "\t 1\t12345.123\n\t12345.123\t0.12345679\n\t1.2345679e+10\t1.234568e+12\n";
|
||||
printf("%s\n", output); // for debugging in case of failure
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if(buffer[i] != output[i]) { // for debugging in case of failure
|
||||
printf("%d: \"%c\" != \"%c\"", int(i), buffer[i], output[i]);
|
||||
}
|
||||
TEST(buffer[i] == output[i]);
|
||||
if (buffer[i] == '\0') {
|
||||
break;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user