[ET Trac] #2925: overflow in rat<int64> in Arith

Roland Haas trac-noreply at einsteintoolkit.org
Thu Mar 26 12:51:16 CDT 2026


#2925: overflow in rat<int64> in Arith

 Reporter: Roland Haas
   Status: new
Milestone: 
  Version: 
     Type: bug
 Priority: minor
Component: 

Comment (by Roland Haas):

using `uint64_t` rollover is indeed simpler though:

```
// TODO: need special code to handle the case if a or b is int64_min since -int64_min > int64_max (https://en.cppreference.com/w/cpp/numeric/math/abs#Notes)
int64_t save_mult(int64_t a, int64_t b) {
  int64_t sign = (a > 0) ^ (b > 0);
  uint64_t abs_a = std::abs(a);
  uint64_t abs_b = std::abs(b);
  uint64_t abs_ab = abs_a * abs_b;
  assert(abs_ab < std::numeric_limits<int64_t>::max());
  assert((abs_ab >= abs_a && abs_ab >= abs_b) || (abs_a == 0 || abs_b == 0));
  return sign ? -abs_ab : abs_ab;
}
```

--
Ticket URL: https://bitbucket.org/einsteintoolkit/tickets/issues/2925/overflow-in-rat-in-arith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.einsteintoolkit.org/pipermail/trac/attachments/20260326/d4c444a9/attachment.htm>


More information about the Trac mailing list