https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92978
Bug ID: 92978 Summary: std::gcd mishandles mixed-signedness Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: rs2740 at gmail dot com Target Milestone: --- From https://stackoverflow.com/q/59379703/2756719; the current gcd implementation is essentially template<typename _Mn, typename _Nn> constexpr common_type_t<_Mn, _Nn> __gcd(_Mn __m, _Nn __n) { return __m == 0 ? __detail::__abs_integral(__n) : __n == 0 ? __detail::__abs_integral(__m) : __detail::__gcd(__n, __m % __n); } __m % __n generally does the wrong thing if one of the operands is negative and signed and the other operand is unsigned. E.g., -120 % 10u is 6u, rather than zero.