Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Speed up a square root c++ prog

Speed up a square root c++ prog

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++performancequestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Craig McRae
    wrote on last edited by
    #1

    Hello All, Could you help me speed up a program? It's designed to find the square root of a number.

    #include

    using namespace std;

    int main()
    {
    float a;
    float c;
    cout << "Square root calculator!" << endl;
    cout << "Enter the number you want to square root" << endl;
    cin >> a;
    cout << "Enter an educated guess" << endl;
    cin >> c;
    cout << "The squareroot of " << a << " is: " << endl;
    do
    c = (c + (a/c))/2;

    while ( c != a/c );

    { cout << c << endl;
    } 
    

    return 0;
    }

    My problem is that it get remarkably slow around 55ish. Any advice it appreciated

    Richard Andrew x64R C J L 4 Replies Last reply
    0
    • C Craig McRae

      Hello All, Could you help me speed up a program? It's designed to find the square root of a number.

      #include

      using namespace std;

      int main()
      {
      float a;
      float c;
      cout << "Square root calculator!" << endl;
      cout << "Enter the number you want to square root" << endl;
      cin >> a;
      cout << "Enter an educated guess" << endl;
      cin >> c;
      cout << "The squareroot of " << a << " is: " << endl;
      do
      c = (c + (a/c))/2;

      while ( c != a/c );

      { cout << c << endl;
      } 
      

      return 0;
      }

      My problem is that it get remarkably slow around 55ish. Any advice it appreciated

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      I can see where it would come to a dead stop:

      while ( c != a/c );

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • C Craig McRae

        Hello All, Could you help me speed up a program? It's designed to find the square root of a number.

        #include

        using namespace std;

        int main()
        {
        float a;
        float c;
        cout << "Square root calculator!" << endl;
        cout << "Enter the number you want to square root" << endl;
        cin >> a;
        cout << "Enter an educated guess" << endl;
        cin >> c;
        cout << "The squareroot of " << a << " is: " << endl;
        do
        c = (c + (a/c))/2;

        while ( c != a/c );

        { cout << c << endl;
        } 
        

        return 0;
        }

        My problem is that it get remarkably slow around 55ish. Any advice it appreciated

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        c = sqrt(a);

        image processing toolkits | batch image processing

        1 Reply Last reply
        0
        • C Craig McRae

          Hello All, Could you help me speed up a program? It's designed to find the square root of a number.

          #include

          using namespace std;

          int main()
          {
          float a;
          float c;
          cout << "Square root calculator!" << endl;
          cout << "Enter the number you want to square root" << endl;
          cin >> a;
          cout << "Enter an educated guess" << endl;
          cin >> c;
          cout << "The squareroot of " << a << " is: " << endl;
          do
          c = (c + (a/c))/2;

          while ( c != a/c );

          { cout << c << endl;
          } 
          

          return 0;
          }

          My problem is that it get remarkably slow around 55ish. Any advice it appreciated

          J Offline
          J Offline
          jeron1
          wrote on last edited by
          #4

          I'm guessing your issue is the while(). Comparing two floats can be tricky.

          while ( c != a/c );

          this[^] link may help.

          1 Reply Last reply
          0
          • C Craig McRae

            Hello All, Could you help me speed up a program? It's designed to find the square root of a number.

            #include

            using namespace std;

            int main()
            {
            float a;
            float c;
            cout << "Square root calculator!" << endl;
            cout << "Enter the number you want to square root" << endl;
            cin >> a;
            cout << "Enter an educated guess" << endl;
            cin >> c;
            cout << "The squareroot of " << a << " is: " << endl;
            do
            c = (c + (a/c))/2;

            while ( c != a/c );

            { cout << c << endl;
            } 
            

            return 0;
            }

            My problem is that it get remarkably slow around 55ish. Any advice it appreciated

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #5

            That is the problem with the Babylonian method you have used. Paul Hsieh has a very good webpage on everything about square roots but were afraid to ask. http://www.azillionmonkeys.com/qed/sqroot.html[^] It includes details most of the techniques from the standard C library, to the faster integers version that NVidia and Quake use.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups