Reputation: 4400
How to code defensively against the Microsoft C26451 (arithmetic overflow) warning in my example? I feel like this should be trivial to fix. So frustrating!
// Linear interpolation
// target - the target point, 0.0 - 1.0
// x... - two values {X1} {X2}
inline static double Linear(float target, double x1, double x2) {
return (target * x2) + ((1.0 - (double)target) * x1);
}
I read through Arithmetic overflow: Using operator '*' on a 4 byte value then casting the result to a 8 byte value but can't seem to fix my C26451 warning: "Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '-' to avoid overflow (io.2).
What would I do to remove the warning?
The Microsoft doc on their compile error did not really help. https://learn.microsoft.com/en-us/cpp/code-quality/c26451
Upvotes: 0
Views: 1011
Reputation: 117298
The compiler warning doesn't make sense and newer Visual Studio versions does not give the same warning. I'd disable it for this line only:
inline static double Linear(double target, double x1, double x2) {
#pragma warning( push )
#pragma warning( disable : 26451 )
return (target * x2) + ((1.0 - target) * x1);
#pragma warning( pop )
}
Upvotes: 2