C Language Write solution one-dollar two-time equation program experience

Source: Internet
Author: User

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:

    1. Read two entries, one-time and constant-term coefficients separately and calculate the delta

    1. No solution condition

At this time, the value of delta is output, less than 0, no solution.

    1. Only one root case.

When delta equals 0, there is only one real root, nothing good to say.

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.