[Bug]: Duration double scaling loses finite precision and misrounds negative halfway cases
Describe the issue
absl::Nanoseconds(-1) * 1000000000000.0 returns -1000.00012207025 seconds instead of exactly -1000 seconds. Dividing the same duration by 8.0, or multiplying by 0.125, returns zero. The exact result is -0.125ns, a halfway case at the quarter-nanosecond resolution, so per the arithmetic contract it should round away from zero to -0.25ns.
Steps to reproduce the problem
Save these two files.
repro.cc:
#include <iostream>
#include "absl/time/time.h"
int main() {
// The exact result is -1/8 ns. The implementation's resolution is 1/4 ns,
// so this is a halfway case that must round away from zero to -1/4 ns.
const auto expected = -absl::Nanoseconds(1) / 4; // Exact integer arithmetic.
const auto divided = absl::Nanoseconds(-1) / 8.0;
const auto multiplied = absl::Nanoseconds(-1) * 0.125;
const auto amplified = absl::Nanoseconds(-1) * 1000000000000.0;
const auto amplified_expected = absl::Seconds(-1000);
std::cout << "expected: " << absl::FormatDuration(expected) << '\n'
<< "division: " << absl::FormatDuration(divided) << '\n'
<< "multiplication: " << absl::FormatDuration(multiplied) << '\n'
<< "scaled expected: " << absl::FormatDuration(amplified_expected)
<< '\n'
<< "scaled actual: " << absl::FormatDuration(amplified) << '\n';
return divided == expected && multiplied == expected &&
amplified == amplified_expected
? 0
: 1;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(duration_negative_half 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::time)
Then 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
Both builds print this and exit 1.
expected: -0.25ns
division: 0
multiplication: 0
scaled expected: -16m40s
scaled actual: -16m40.00012207025s
The reproduction files above are provided under CC0.
What version of Abseil are you using?
Version: d292c59c4b88018a8e0532bf5bd3d173513f4921 (master on 2026-10-12)
What operating system and version are you using?
Ubuntu 24.04, x86-64.
What compiler and version are you using?
GCC 13.3.0 and Clang 18.1.3, C++17 Release, no fast-math.
What build system are you using?
CMake 3.28.3.
Additional context
ScaleDouble scales the seconds and the fractional ticks separately and recombines them in binary64. -1ns is -1 second plus 3999999996 ticks, so the halfway cases reach about -0.49999998585903427 ticks before std::round. Open PR 2184 touches this function but only guards non-finite partials.
— ChipLab