Go to Post Do you know how, say, Native American cultures were kept? They had storytellers, sages, and the like. They passed on their legends and culture to the next generation. So will we. - EricH [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Spotlight this post!  
Unread 15-02-2004, 09:41
kiracofe8 kiracofe8 is offline
Registered User
#1317 (Digital Fusion)
Team Role: Engineer
 
Join Date: Nov 2001
Location: Columbus, OH
Posts: 30
kiracofe8 will become famous soon enough
Re: Square Root Function not working

Quote:
Originally Posted by bludstayne
Why isn't this working? I always get a value that's too high. 4 would give me the correct value, but 9 up always goes over. It's kind of weird too, every perfect square makes it go over a little less, you can check it out and see what I mean.

Code:
float sqrt(unsigned int tar, float accr)
{
    float i;
    for (i=0.0f;i<tar;i+=accr)
    {
        if (i*i >= tar)
            return i;
    }
}
Why this goes over has already been answered, but I should also point out that this algorithm will be incredibly slow, and not particularly accurate. Two things:

First off, it is a cardinal sin to have a floating point value as a loop index. The problem is that floating point numbers always have some round off error. And, each time you add, the round off error increases. So, for example, if tar = 1, and accr = 0.1, after 10 iterations, you may get i = 0.9999999 or you may get i = 1.0000001. If you get the former, you'll end up with the square of 1 = 1.1 b/c you'll go one step too far. If you get the later, you'll get the right answer. But there is no way to know which you'll get ahead of time.

The problem of floating point loop indexes can cause such subtle bugs that some languages do not allow it at all (e.g. recent versions of FORTRAN). The following is the recommended replacement:

Code:
float sqrt(unsigned int tar, float accr)
{
    int i;
    float j;

    j = 0;

    for (i=0;j<tar;i++)
    {
        j = i * accr;
        if (j*j >= tar)
            return j;
    }
    return -1; //signal an error.  should never happen, but it is bad form to
                  //simply not return any value.    
}
Secondly: the algorithm itself converges slowly. E.g. if you try to take the square root of, say, 100 with accr = 0.1, it will take a LONG time (1000 iterations) to get a correct answer. Try a different method. The most common method to numerically compute square roots is called Newton's method. Do a search on google for "Newton's method square root" and you should find some good example code. Newton's method will converge to a reasonable value after only a few iterations.
__________________
/* Daniel */
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mulit-postioning with pnuematics this year? KenWittlief Pneumatics 101 01-02-2005 21:54
Square Root bludstayne Programming 3 14-02-2004 14:48
Aspartame shyra1353 Chit-Chat 8 20-01-2004 22:41
I'm working :) Jack FIRST Scouting Network 2 03-01-2004 01:26
Whose working this summer? Frank(Aflak) Chit-Chat 20 28-05-2003 11:35


All times are GMT -5. The time now is 06:06.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi