C++ int float casting

Integer division occurs, then the result, which is an integer, is assigned as a float. If the result is less than 1 then it ends up as 0.

You’ll want to cast the expressions to floats first before dividing, e.g.

float m = static_cast<float>(a.y - b.y) / static_cast<float>(a.x - b.x);

Leave a Comment