Square root program doesn't produce the right value

Discussion in 'C++' started by Protoman, Nov 10, 2005.

  1. Protoman

    Protoman Guest

    Can you help me? For 4, my square root funct gives 4 instead of 2;
    here's the code:

    #include <iostream>
    #include <cstdlib>
    using namespace std;

    template<class T>
    T Abs(T Nbr)
    {
    if( Nbr >= 0 )
    return Nbr;
    return -Nbr;
    }

    template<class T>
    T Sqrt(T Nbr)
    {
    long double Number = Nbr / 2;
    const long double Tolerance = 1.0e-7;
    do Number = (Number + Nbr / Number) / 2;
    while( Abs(Number * Number - Nbr) > Tolerance);
    return Number;
    }

    int main()
    {
    cout << "Enter a number: " << endl;
    long double num;
    cin >> num;
    cout << "Sqrt(" << num << ")= " << Sqrt(num) << endl;
    system("PAUSE");
    return 0;
    }

    Can you help me? Thanks.
    Protoman, Nov 10, 2005
    #1
    1. Advertising

  2. Protoman

    Kai-Uwe Bux Guest

    Protoman wrote:

    > Can you help me? For 4, my square root funct gives 4 instead of 2;
    > here's the code:
    >
    > #include <iostream>
    > #include <cstdlib>
    > using namespace std;
    >
    > template<class T>
    > T Abs(T Nbr)
    > {
    > if( Nbr >= 0 )
    > return Nbr;
    > return -Nbr;
    > }
    >
    > template<class T>
    > T Sqrt(T Nbr)
    > {
    > long double Number = Nbr / 2;
    > const long double Tolerance = 1.0e-7;
    > do Number = (Number + Nbr / Number) / 2;
    > while( Abs(Number * Number - Nbr) > Tolerance);
    > return Number;
    > }
    >
    > int main()
    > {
    > cout << "Enter a number: " << endl;
    > long double num;
    > cin >> num;
    > cout << "Sqrt(" << num << ")= " << Sqrt(num) << endl;
    > system("PAUSE");
    > return 0;
    > }
    >
    > Can you help me? Thanks.


    Hm, on my machine, the program computes "Sqrt(4)= 2" just fine. The real
    problem is "Sqrt(0)= nan".

    Here is a fix for that:

    template<class T>
    T Sqrt(T Nbr)
    {
    long double Number = Nbr / 2;
    const long double Tolerance = 1.0e-7;
    while( Abs(Number * Number - Nbr) > Tolerance) {
    Number = (Number + Nbr / Number) / 2;
    }
    return Number;
    }



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 10, 2005
    #2
    1. Advertising

  3. Protoman wrote:
    >
    > Can you help me? For 4, my square root funct gives 4 instead of 2;
    > here's the code:
    >


    What's wrong with fireing up your debugger and stepping through the code
    to see why it does that?

    Figuring out problems in code and why it doesn't do what it should do
    is *vital* in becomming a programmer. So start early, develop your
    skills and learn how to use your tools.


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Nov 10, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luca
    Replies:
    1
    Views:
    1,040
    salman sheikh
    Apr 29, 2004
  2. Replies:
    0
    Views:
    1,222
  3. Christian

    Fix point square root

    Christian, Apr 25, 2005, in forum: VHDL
    Replies:
    5
    Views:
    6,661
    jeppe
    Mar 18, 2010
  4. Replies:
    5
    Views:
    462
    Luc The Perverse
    Oct 27, 2006
  5. Vasu
    Replies:
    2
    Views:
    596
    Knute Johnson
    Oct 18, 2008
Loading...

Share This Page