On the problem of roots and polyval functions in matlab, thank you ~

Floating-point numbers can't be accurately represented in computers, because computers use binary representations, such as 0. 1, and actually need to store an infinite number of binary digits (you have learned the method of rounding binary decimal representations by multiplying two by two on the basis of computers), so computer resources are limited, so this is impossible. So for floating-point numbers, there is only (single/double) precision, that is, only a limited number of binary bits are reserved. This is the source of error.

Question 1) You can call real(ans) to keep only the real part and remove the imaginary part.

Question 2) On the basis of the question 1, even if real is called, only the real part is kept, so that the result of roots seems to be-1.0000, and the result of calling polyval will not be 0. This is because matlab shows only a limited number of decimal places, which looks like-1.0000, but it may actually be-1.000000000000091,so using polyval will only get a small enough number (you have 65438+ here). And calling polyval with [-1-1] will get 0, because-1is an exact integer.