Square root SQRT

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Square root SQRT

Post 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
Attachments
SQRT_V1.fcf
(10 KiB) Downloaded 390 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Square root SQRT

Post 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 );

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Square root SQRT

Post 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

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: Square root SQRT

Post by JohnCrow »

Hi Jan

Nice routine, have made a note of this for future reference.
1 in 10 people understand binary, the other one doesn't !

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

Re: Square root SQRT

Post 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
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Square root SQRT

Post 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
Attachments
SQRT_V3.fcf
(9.5 KiB) Downloaded 324 times

Post Reply