This is a typical case of "integer division used to make a float value". If x
, x1
and x2
are all integers, then the compiler should, according to the rules of C and C++ use integer calculations to calculate x-x1
and x2-x1
, and then divide the differences of those as integer division. If x2-x1
is greater than x-x1
, then the result is zero [assuming positive values like 7/14. -14 / -7 is obviously 2].
The easy fix is to convert the type of one expression to a floating point type:
double delta = static_cast<double>(x - x1) / (x2 - x1);