Repository navigation
Description
Describe the issue
absl::StrFormat("%010.0e", 0x1p128) returns 3e+38 instead of 000003e+38. Scientific %g also omits the requested zero padding, for both 0x1p128 and 0x1p-128.
The StrFormat guide defines 0 as leading zero padding for floating conversions and width as a minimum. The precision exception applies to integer conversions. The pinned public header specifies POSIX printf syntax for %e and %g. C11 draft N1570, §7.21.6.1 ¶6 gives the same zero-padding rule.
Both inputs are exactly representable binary64 values. Exact integer/Fraction arithmetic gives the following results for %010.0e and %010.1g:
| Input | Expected | Observed with either format |
|---|---|---|
0x1p128 |
000003e+38 |
3e+38 |
0x1p-128 |
000003e-39 |
3e-39 |
The returned fields are five characters long despite width 10. If a caller relies on fixed-width output, this can shift subsequent columns. The rounded numeric value is unchanged in these examples; no downstream consumer was tested.
Steps to reproduce the problem
In a new directory, save these two files.
repro.cc:
#include <iostream>
#include <string>
#include "absl/strings/str_format.h"
int main() {
bool passed = true;
for (double value : {0x1p128, 0x1p-128}) {
// Exact rational rounding gives 3e+38 and 3e-39, respectively.
// Width 10 requires five leading zero characters.
const std::string expected = value > 1 ? "000003e+38" : "000003e-39";
const auto e = absl::StrFormat("%010.0e", value);
const auto g = absl::StrFormat("%010.1g", value);
std::cout << "expected='" << expected << "' e='" << e
<< "' g='" << g << "'\n";
passed = passed && e == expected && g == expected;
}
return passed ? 0 : 1;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(scientific_zero_padding LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(ABSL_BUILD_TESTING OFF CACHE BOOL "" FORCE)
set(ABSL_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
if(NOT ABSEIL_SOURCE_DIR)
message(FATAL_ERROR "Pass -DABSEIL_SOURCE_DIR=/path/to/pinned/abseil-cpp")
endif()
add_subdirectory("${ABSEIL_SOURCE_DIR}" abseil EXCLUDE_FROM_ALL)
add_executable(repro repro.cc)
target_link_libraries(repro PRIVATE absl::str_format)
With GCC 13.3.0 and Clang 18.1.3 available as g++ and clang++-18, run:
git clone https://github.com/abseil/abseil-cpp.git abseil-cpp
git -C abseil-cpp checkout --detach d292c59c4b88018a8e0532bf5bd3d173513f4921
git -C abseil-cpp rev-parse HEAD
cmake -S . -B build-gcc -DABSEIL_SOURCE_DIR="$PWD/abseil-cpp" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++
cmake --build build-gcc --target repro --parallel 3
./build-gcc/repro
cmake -S . -B build-clang -DABSEIL_SOURCE_DIR="$PWD/abseil-cpp" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=clang++-18
cmake --build build-clang --target repro --parallel 3
./build-clang/repro
The verifier used the same source and build settings. Its Clang executable was extracted under its work directory. The commands above use an installed clang++-18.
Both executables printed this and exited 1:
expected='000003e+38' e='3e+38' g='3e+38'
expected='000003e-39' e='3e-39' g='3e-39'
The reproduction files above are provided under CC0.
What version of Abseil are you using?
d292c59c4b88018a8e0532bf5bd3d173513f4921.
This was master when checked on 2026-10-12. The verifier built this commit. The drafting pass read upstream source and did not rebuild it.
What operating system and version are you using?
Ubuntu 24.04, Linux x86-64. glibc 2.39 (2.39-0ubuntu8.9).
What compiler and version are you using?
GCC 13.3.0 (13.3.0-6ubuntu2~24.04.1) and Clang 18.1.3 (18.1.3-1ubuntu1).
Both used C++17 Release builds without fast-math. The probes checked FE_TONEAREST.
What build system are you using?
CMake 3.28.3. Builds ran sequentially with --parallel 3.
Additional context
Source inspection points to FormatENegativeExpSlow and FormatEPositiveExpSlow. Both compute padding.zeros but omit its emission. Scientific %g delegates to these emitters. Source inspection suggests appending the computed zeros after any sign in both functions. This change has not been implemented or tested.
The verifier checked 50 directed cases per compiler. There were 26 mismatches and 24 passing controls. Controls covered space padding, left adjustment, no width, and smaller magnitudes using fast conversion. Tested variants also included %E, %G, sign flags, and alternate form. glibc agreed with all expectations computed using exact Python 3.12.3 arithmetic. The formatting probes used the C locale.
Only the reproducers, directed probes, and their library dependencies were built. The project-wide suite, other operating systems, other architectures, long-double inputs, and other rounding modes were not tested.
Found by ChipLab's AI verification agents and independently reproduced.
ChipLab ref: CLAB-2026-698
Activity
PR #2195: StrFormat: honor zero padding in slow scientific conversions
PR #2195: StrFormat: honor zero padding in slow scientific conversions
PR #2195: StrFormat: honor zero padding in slow scientific conversions