Ray's Knowledge Base
Unlock

Keep integer window arithmetic exact and check the result range

DecisionVerified 30 Sep 2026Holds project: dftracer-utils
Decision. The context, the choice, why, and the options that were turned down.

Context#

Casting uint64 input to int64 made running_sum, frame_sum and delta wrap above INT64_MAX.

Decision#

Accumulate integer values in __int128 and check stores. Return uint64 for unsigned running_sum and frame_sum, and int64 otherwise. Throw std::overflow_error naming the function when the result is out of range.

Why#

Unsigned sums must retain the unsigned range without silent wrap. Inbox c38c4c96cc records the implementation and result types. Transcript 8397caa9dd reports final-tree tests after the fix. The use of __int128 is a recorded implementation fact, not a general recommendation for portable C++20.

Rejected options#

Keeping int64 would reject valid unsigned sums at 2^63. Decimal results were also rejected.

Also found by searching

  • running_sum of uint64 column returns negative number above INT64_MAX
  • window sum over large unsigned values silently wraps around
  • how do I compute running sum frame_sum delta without overflow for uint64 input
  • cast uint64 to int64 overflow in window functions, accumulate in __int128 throw overflow_error
  • running_sum on uint64 column wraps negative above 9223372036854775807
  • window function frame_sum delta overflow silently after casting unsigned to int64
  • how should integer window sums handle overflow and unsigned inputs in C++?
  • uint64 values above INT64_MAX in running_sum give wrong results, __int128 accumulator, std::overflow_error