Page 1 of 1

Square root SQRT

Posted: Thu Jun 09, 2011 4:37 pm
by Jan Lichtenbelt
Please find an itteration process to calculate the square root of a variable. This general ittertaion process can be used for any function, see flowcode.

Or is there a faster/better method for the square root value?

Kind regards

Jan Lichtenbelt

Re: Square root SQRT

Posted: Thu Jun 09, 2011 6:00 pm
by Benj
Hello Jan,

BoostC has a built in Integer square root function that can be accessed via C code.

Here is the text from the manual.

unsigned char sqrt( unsigned short val )
(Function) Integer square root. Function that returns square root from an unsigned 16 bit integer.

Using C this would look like this where you have Flowcode Byte or INT variables a and b.

Code: Select all

FCV_A = sqrt(FCV_B);
There is also a float square root function.

float float32_sqrt( float a );
This function returns the square root of the supplied value.

Using C this would look like this where you have Flowcode Float variables C and D

Code: Select all

FCV_C = float32_sqrt( FCV_C );

Re: Square root SQRT

Posted: Thu Jun 09, 2011 7:29 pm
by Jan Lichtenbelt
Dear Ben,

Thanks a lot. That is a much easier way of programming you suggest. However, it takes a lot of more program words

1) FCV_A = float32_sqrt( FCV_A ); used 7629 of 8192 program words
2) My itteration process for sqrt: 4248 of 8192 program words.


Kind regards

Jan

Re: Square root SQRT

Posted: Thu Jun 09, 2011 7:49 pm
by JohnCrow
Hi Jan

Nice routine, have made a note of this for future reference.

Re: Square root SQRT

Posted: Sun Aug 21, 2011 11:09 pm
by benp
Your program simulate OK but can't work on PIC
You sould use:
float_lt(SQRT.Rel_error,0)
and not
SQRT.Rel_error<0

For float, I think the Heron's method should be quicker:
a(n+1)=1/2(an+A/an)
Explained here as Babylonian method:
http://en.wikipedia.org/wiki/Methods_of ... uare_roots

Re: Square root SQRT

Posted: Sat Aug 27, 2011 8:43 pm
by Jan Lichtenbelt
Dear Benp,

Indeed the Heron's method is a much quicker method for square root calculations. It even uses less memory: 4158 out of 8192.
My mentioned 'half' method is a more general method to find the zero of a function F(x)=0.

Kind regards

Jan lichtenbelt