Here's a square root routine for GCB. It only involves bit shifting, addition and subtraction, which makes it pretty fast and efficient. It expects a word in and a word out, and will handle arguments of up to 4294, which should cover most applications.
More importantly, the results are accurate to 2 decimal places, which again should handle most situations.
Here is the code, below, for a demo program using it. If you don't feel like dragging out an LCD, just run it on a simulator to see what it can do.
I'll attach the include file in the next post, since we don't seem to be able to do attachments otherwise.
If tested and confirmed by others, this could perhaps make a nice addition to the MATHS.H file.
;Demo:Showthefirst100squarerootsto2decimalplaces.;ThisusestheSqrt.hincludefile.;-----Configuration#chip 16F88, 8 ;PIC16F88 running at 8 MHz#config mclr=off ;reset handled internally#config osc=int ;use internal clock#include"Sqrt.h";-----Constants#define LCD_IO 4 ;4-bit mode#define LCD_RS PortB.2 ;pin 8 is LCD Register Select#define LCD_Enable PortB.3 ;pin 9 is LCD Enable#define LCD_DB4 PortB.4 ;DB4 on pin 10#define LCD_DB5 PortB.5 ;DB5 on pin 11#define LCD_DB6 PortB.6 ;DB6 on pin 12#define LCD_DB7 PortB.7 ;DB7 on pin 13#define LCD_NO_RW 1 ;ground the RW line on LCD;-----VariablesdimlengthasbytedimiasworddimvalStr,outStrasstring;-----ProgramdirPortBout;alloutputstotheLCDfori=0to100;printfirst100squarerootsclsprint"sqrt("printiprint")="valStr=str(sqrt(i));formatdecimalnicelylength=len(valStr)selectcaselengthcase1:outStr="0.00";zerocasecase3:outStr=left(valStr,1)+"."+right(valStr,2)case4:outStr=left(valStr,2)+"."+right(valStr,2)case5:outStr=left(valStr,3)+"."+right(valStr,2)endselectprintoutStr;displayresultswait2Snexti
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I finally got around to trying your square root code. It worked great!
I have two questions:
What limits the maximum value to 4294?
In your square root Function, you have this statement:
"set sqrt_bitFlag.30 on"
What does the ".30" refer to?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2014-05-01
Hello MBB,
Thanks for the report.
The input value is scaled up by a million to provide three places of accuracy. (After rounding, this falls back to two places in the final result). 4294 times a million is the limit for the Long data type.
Bit 30 of the variable sqrt_bitFlag is set On.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here's a square root routine for GCB. It only involves bit shifting, addition and subtraction, which makes it pretty fast and efficient. It expects a word in and a word out, and will handle arguments of up to 4294, which should cover most applications.
More importantly, the results are accurate to 2 decimal places, which again should handle most situations.
Here is the code, below, for a demo program using it. If you don't feel like dragging out an LCD, just run it on a simulator to see what it can do.
I'll attach the include file in the next post, since we don't seem to be able to do attachments otherwise.
If tested and confirmed by others, this could perhaps make a nice addition to the MATHS.H file.
And here is the square root include file.
Thomas,
I finally got around to trying your square root code. It worked great!
I have two questions:
What limits the maximum value to 4294?
In your square root Function, you have this statement:
"set sqrt_bitFlag.30 on"
What does the ".30" refer to?
Hello MBB,
Thanks for the report.
The input value is scaled up by a million to provide three places of accuracy. (After rounding, this falls back to two places in the final result). 4294 times a million is the limit for the Long data type.
Bit 30 of the variable sqrt_bitFlag is set On.
Thanks for clarifying this.