why different answers for this equation

This is a discussion on why different answers for this equation within the C Programming forums, part of the General Programming Boards category; * Write a program that checks whether the equation (x+y)^2 = x^2 + 2xy + y^2 * is true for ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    3

    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;

    /*Input*/
    printf("INPUT");
    printf("\n=====\n");
    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("\n=================\n");

    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");

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

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

    printf("\n");
    /* end of display */

    return EXIT_SUCCESS;

    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Roundings?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    If you're having problems with floats, you need to read the document Prelude found here
    http://www.cprogramming.com/cboard/s...threadid=16892

    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, 06: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, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21