Thread: why different answers for this equation

  1. #1
    Registered User
    Join Date
    May 2002

    Post why different answers for this equation

    * Write a program that checks whether the equation

    (x+y)^2 = x^2 + 2xy + y^2

    * is true for all double values. If you find any two values for
    * which the equation is not true, then explain why this is so.

    #include <stdio.h>
    #include <stdlib.h>

    int main() {

    double x, y, LeftEq, RightEq;

    printf("Enter double value for x: ");
    scanf("%lf", &x);

    printf("Enter double value for y: ");
    scanf("%lf", &y);
    /*End of Input*/

    /*Assign the 2 equations to 2 variables*/
    LeftEq = (x+y) * (x+y);
    RightEq = (x*x) + (2*x*y) + (y*y);

    /*Display Results*/
    printf("\n\nCHECK and COMPARE");

    printf("The EQUATION (X+Y)^2 = %10.13f\n", LeftEq);

    /*Conditions to compare the 2 equations*/
    if(LeftEq == RightEq)
    printf("\n.........has the same value as.........\n\n");

    else if(LeftEq != RightEq)
    printf("\n.........has a different value from.........\n\n");

    printf("Invalid values obtained!!!");
    /*stop comparing*/

    printf("The EQUATION (X^2 + 2XY + Y^2) = %10.13f\n", RightEq);

    /* end of display */

    return EXIT_SUCCESS;


  2. #2
    Join Date
    Aug 2001
    Groningen (NL)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you're having problems with floats, you need to read the document Prelude found here

    Basically, you can't compare floats for equality, you can only say that they are "close enough".

    How close depends on what you're trying to do (see the link)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. differential equation??
    By kypronite in forum Game Programming
    Replies: 11
    Last Post: 09-03-2008, 05:25 PM
  2. Replies: 15
    Last Post: 11-04-2006, 04:06 AM
  3. Cubic equation program not giving me right answers
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2006, 05:42 PM
  4. IDEA: Equation solver
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 01-07-2003, 11:46 AM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 09:21 PM