Fix micro string formatting bug. Properly handle float fractions with no leading zeros.
PiperOrigin-RevId: 332065497 Change-Id: I2e8470ccd7082eee220268593ea3a100888567d1
This commit is contained in:
parent
a58bfeef15
commit
8aefe626a9
@ -192,13 +192,15 @@ char* FastFloatToBufferLeft(float f, char* buffer) {
|
||||
// works properly.
|
||||
*current = '0';
|
||||
|
||||
// Shift fraction values and prepent zeros.
|
||||
for (int i = 0; i < fraction_digits; i++) {
|
||||
current--;
|
||||
*(current + leading_zeros) = *current;
|
||||
*current = '0';
|
||||
// Shift fraction values and prepend zeros if necessary.
|
||||
if (leading_zeros != 0) {
|
||||
for (int i = 0; i < fraction_digits; i++) {
|
||||
current--;
|
||||
*(current + leading_zeros) = *current;
|
||||
*current = '0';
|
||||
}
|
||||
current += kMaxFractionalDigits;
|
||||
}
|
||||
current += kMaxFractionalDigits;
|
||||
|
||||
// Truncate trailing zeros for cleaner logs. Ensure we leave at least one
|
||||
// fractional character for the case when scaled_fraction is 0.
|
||||
|
@ -120,6 +120,15 @@ TF_LITE_MICRO_TEST(FloatFormatShouldPrintFractionCorrectly) {
|
||||
TF_LITE_MICRO_EXPECT_STRING_EQ(golden, buffer);
|
||||
}
|
||||
|
||||
TF_LITE_MICRO_TEST(FloatFormatShouldPrintFractionCorrectlyNoLeadingZeros) {
|
||||
const int kBufferLen = 24;
|
||||
char buffer[kBufferLen];
|
||||
const char golden[] = "Float: 1.6332993*2^-1";
|
||||
int bytes_written = MicroSnprintf(buffer, kBufferLen, "Float: %f", 0.816650);
|
||||
TF_LITE_MICRO_EXPECT_EQ(static_cast<int>(sizeof(golden)), bytes_written);
|
||||
TF_LITE_MICRO_EXPECT_STRING_EQ(golden, buffer);
|
||||
}
|
||||
|
||||
TF_LITE_MICRO_TEST(StringFormatOverrunShouldTruncate) {
|
||||
const int kBufferLen = 10;
|
||||
char buffer[kBufferLen];
|
||||
|
Loading…
Reference in New Issue
Block a user