Foreword: On the net to see a lot of solutions to the two-time equation of the small program, in use must have a large number of decimals, feeling very uncomfortable, so he wrote again.
First, recall the root formula of a two-time equation:
- Read two entries, one-time and constant-term coefficients separately and calculate the delta
- No solution condition
At this time, the value of delta is output, less than 0, no solution.
- Only one root case.
When delta equals 0, there is only one real root, nothing good to say.
- The case of two unequal real roots
The point is this part.
To avoid the problem of a large number of decimals, separate sqrt (delta) is an integer and a non-integer. (if it is an integer, the direct operation, if not an integer, preserves the root of the root)
(1) when sqrt (delta) is an integer
Now the question is how to judge an integer
In order to avoid mutual interference, two variables are used, SQR is used to determine whether an integer, if it is an integer, then use SQR1
How can I tell if it is an integer? You can take advantage of the nature of int and double shapes. the size of the SQR and (int)(Sqr + 0.9999999) is used to determine whether the SQR is an integer. [The double shape is generally accurate to 6 digits after the decimal point, so it uses 7 9]. Then cast to int, if an integer, (int) (SQR + 0.9999999) does not carry, x=y. Not an integer, and vice versa.
Then, if it is an integer, the sqrt (delta) descendants are calculated first.
(2) sqrt (delta) is not an integer
Yes, keep the root of the radicals!
PostScript: Write this section of the program is originally for a wide range of problems of the solution equation math problem, want to quickly finish homework ~ ~ So in that month black wind high evening after finishing homework by the way this article wrote.
C Language Write solution one-dollar two-time equation program experience