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
Square root SQRT
Moderator: Benj
- 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:
- 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
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.
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
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);
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 );
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- 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
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
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
- JohnCrow
- 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
Hi Jan
Nice routine, have made a note of this for future reference.
Nice routine, have made a note of this for future reference.
1 in 10 people understand binary, the other one doesn't !
-
- 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
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
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
- 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
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
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 325 times