Articles contenant le tag integer arithmetic
Mid-point and linear interpolation done right
Posté par Olivier dans Trucs & Astuces le 22 mai 2025
How many times did we write code like: Although this code is correct, it may suffer from overflows. The typical way to avoid those is to cast to a wider type. But how can you do this when the start and stop values are already using the largest integer type (eg. int64_t) ? The C++ […]
Overflow-safe integer mul&div
Posté par Olivier dans Trucs & Astuces le 22 décembre 2023
When you do a multiply & divide operation, to ensure you don’t loose precision, you would start by the multiplication before the division. But in that case, it could be that the result of the multiplication overflows. The natural solution would go to use an intermediate storage of larger size. This uses concepts (C++20). For […]