having trouble converting the following formula

This is a discussion on having trouble converting the following formula within the C Programming forums, part of the General Programming Boards category; I'm having trouble converting the following formula: Here's the image The sqrt function will be used obviously, but how are ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    having trouble converting the following formula

    I'm having trouble converting the following formula:

    Here's the image

    The sqrt function will be used obviously, but how are squared numbers represented?
    Last edited by Axel; 10-18-2005 at 01:43 AM.

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    The square of x is x * x.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    It's in C++ but the formula is the same.
    http://www.cprogramming.com/snippets...ount=30&page=0
    Woop?

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Here's my implementation (haven't looked at the C++ code, will do now)
    Code:
        double x1 = 4.40;
        double x2 = 3.30;
    	double y2 = 7.70;
    	double y1 = 8.80;
    
    
        printf("%l.2f", sqrt(x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    the answer should be 3.11 but it's printing 2.36

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    results of the C++ code(with the same values as above):

    sqrt( ( pow( (x1 - x2), 2) + pow( (y1 - y2), 2) ) );
    printf("%lf", sqrt( ( pow( (x1 - x2), 2) + pow( (y1 - y2), 2) ) ));

    1.555635

    hmm..

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    That is the correct answer. Check your calculations
    x1 = 4.4
    x2 = 3.3
    y1 = 8.8
    y2 = 7.7

    x1 - x2 = 1.1
    y1 - y2 = 1.1

    1.1 ^ 2 = 1.21

    1.21 + 1.21 = 2.42

    sqrt(2.42) = 1.555....
    Woop?

  7. #7
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    And your mistake in your first attempt were missing braces:

    sqrt(x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)

    should be:

    sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) )

  8. #8
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    oh, i see thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble converting a variable
    By KasMage in forum C++ Programming
    Replies: 6
    Last Post: 07-14-2008, 03:43 PM
  2. how to change char to formula
    By kosong in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 04:31 AM
  3. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM
  4. Need help with this formula -b/2a..Please read.
    By foofoo in forum C Programming
    Replies: 4
    Last Post: 06-04-2002, 11:59 PM
  5. Formula string conversion
    By Gr3g in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 08:28 PM

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