Menu

sqrt function acts weird

Anubis208
2007-09-03
2012-09-26
  • Anubis208

    Anubis208 - 2007-09-03

    4.9.9.2
    WinXP
    I'm trying to use this equation I found to find the distance between two points on a two dimensional plane: sqrt((x2-x1)^2+(y2-y1)^2). I wrote a small dos application to see if this would work. Here is the code.

    include <cstdlib>

    include <iostream>

    include <math.h>

    using namespace std;

    int main(int argc, char *argv[])
    {
    float d;
    d = sqrt((1-0)^2+(1-0)^2);
    cout<<d;
    cin.get();
    }

    it compiles just fine, but when I run it it outputs 0. When I add in another set of numbers 0s for the z coordinates [d = sqrt((1-0)^2+(1-0)^2+(0-0)^2)]it outputs 1.41421. when you add 0 to 2 its still 2 and shouldn't it still have the same square root?

     
    • Wayne Keen

      Wayne Keen - 2007-09-03

      What do you think a ^ operator does? I don't think it it doing what you think.

      I think you need to look into the "pow" operator.

      Wayne

       
    • Wayne Keen

      Wayne Keen - 2007-09-03

      The only use I know for the "^" operator in C++ involves bitwise exclusive OR - it is not
      an exponent operator.

      Wayne

       
    • Anonymous

      Anonymous - 2007-09-03

      A slight correction: "pow" is not an C/C++ operator, but a library function.

      Old Newbie

       
    • Anonymous

      Anonymous - 2007-09-03

      You 'found' that equation!? You mean you never did Pythagoras' Theorem (Pythagorean Theorem for Americans, apparently) at school? "The square on the hypotenuse is equal to the sum of the squares on the other two sides" - sound familiar?

      Many programming languages use the ^ glyph for exponentiation, but in C/C++ and related languages it is the bitwise XOR operation. ^ is also commonly used in ASCII representations of mathematical equations for exponentiation.

      For x ^ y, translate to pow(x, y). For x ^ 2 specifically it is simpler (and probably more efficient) to simply use x * x.

      And of no particular importance in this instance but nonetheless, it is not a DOS program, it is a Win32 console mode program (unless possibly you are not using Dev-C+ with the default compiler). There's a whole world (and about 12 years) of a difference.

      Clifford

       
    • Anubis208

      Anubis208 - 2007-09-03

      I know the Pythagorean Theorem but we just started covering that in school (I'm a lot younger than you think). I was looking into trigonometry because I haven't had that in school yet but I need to know it for programming, and I found this distance formula on the site and decided to test it (I'm not that good at applying the math I know to stuff like this, it's something I'm working on). I know the difference between a dos and Win32 Console application, but dos is shorter and I had a time limit for using that computer. I guess I got that operator confused with a language I know that only applies to one program. If I use pow it works just as it should. Thanks for helping me clarify that.

       
    • Anonymous

      Anonymous - 2007-09-03

      >> (I'm a lot younger than you think)
      Fair enough; my apologies.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.