Skip to content

Commit b9fcdfc

Browse files
committed
feat: Implement printing of near-zero floats as zero
This commit introduces a modification to the floating-point number printing logic. When a floating-point number's absolute value falls below a defined threshold, it will now be printed as exactly "0" instead of its very small non-zero representation. This change improves the readability of output by eliminating the visual clutter of extremely small numbers that are effectively zero for most practical purposes.
1 parent c1eefa0 commit b9fcdfc

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/TiledArray/tensor/print.ipp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ void NDArrayPrinter::printArray(const T* data, const std::size_t order,
5050

5151
for (size_t i = 0; i < extents[level]; ++i) {
5252
if (level == order - 1) {
53+
auto value = data[offset + i * strides[level]];
54+
if constexpr (std::is_floating_point_v<decltype(value)>) {
55+
value = std::abs(value) < (0.5 / (std::pow(10, precision))) ? 0 : value;
56+
}
5357
// At the deepest level, print the actual values
54-
os << std::fixed << std::setprecision(precision) << std::setw(width) << std::setfill(Char(' '))
55-
<< data[offset + i * strides[level]];
58+
os << std::fixed << std::setprecision(precision) << std::setw(width) << std::setfill(Char(' ')) << value;
5659
if (i < extents[level] - 1) {
5760
os << ", ";
5861
}

0 commit comments

Comments
 (0)