Arithmetic overflow: Using operator '*' on a 4 byte value then casting the result to a 8 byte value

前端 未结 3 1304
温柔的废话
温柔的废话 2021-01-24 08:08

I am trying to write a program to solve a quadratic equation whose coefficients\' values do not exceed 100 by absolute value and it is guaranteed that if any roots exist, they a

3条回答
  •  走了就别回头了
    2021-01-24 08:15

    The root cause here (pun very much accepted) is that the quadratic equation is not a Diophantine equation. It applies to real numbers, and in particular sqrt(d) is usually not an integer.

    In C++, the return type of sqrt(IntegralType) is double. Thus 2*a is converted to double too, but only after multiplying. And Visual Studio very reasonably notes that you're better off doing the conversion before multiplying. It just doesn't note that you can even make a,b,c all doubles from the start.

提交回复
热议问题