Extracting cube root
Newzed
Posts: 2,503
I was sitting around sort of bored, so I decided I would write a program to extract the cube root of a number.· I got pretty close, but not close enough.· So then I decided to go a different route.· I laboriously calculated the cube roots of a bunch of numbers and incorporate them into a program.· The resultant effort produced more of a reference library than it did an honest-to-goodness program, but for what it's worth, here it is.
The program will display the cube root of any number from 2 to 125 to three decimal places.· As near as I can tell, accuracy is better than
.05 percent, which is pretty close.
If just one person finds a use for this, it will have been worth the effort.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
4 digit serial LED
http://hometown.aol.com/newzed/index.html
The program will display the cube root of any number from 2 to 125 to three decimal places.· As near as I can tell, accuracy is better than
.05 percent, which is pretty close.
If just one person finds a use for this, it will have been worth the effort.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
4 digit serial LED
http://hometown.aol.com/newzed/index.html
bs2
4K
Comments
Extracting cube roots when you are bored? What's next!?
Just a suggestion on the program, instead of separate IF statements for each value...
IF n1 = 0 THEN n2 = 0000
IF n1 = 1 THEN n2 = 1000
IF n1 = 2 THEN n2 = 1261
IF n1 = 3 THEN n2 = 1443
IF n1 = 4 THEN n2 = 1588
... and so on and so forth.
DEBUG "Cube Root = ",DEC n2/1000, ".", DEC3 n2, cr, cr
It could be a lookup:
DATA word 0, word 1000,word 1261, word 1443,word 1588
and so on and so forth,
with a READ in the main program...
READ n1*2, word n2
DEBUG "Cube Root = ",DEC n2/1000, ".", DEC3 n2, cr, cr
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
It was a pain entering all the DATA statements but I managed.·Finally figured out a more or less easy way to do it. ·I'm working on increasing the limit from 125 to 216, and maybe 343.
Thanks
Sid
Cube_C.bs2 is attached.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
4 digit serial LED
http://hometown.aol.com/newzed/index.html
1. set x = N/2
2. calculate q = N/x
3. set x = (x + q)/2
4. repeat 2 and 3 until the difference between q and x is smaller than some limit, which in principle will be the accuracy of the result x.
This is a very simple yet fast and efficient algorithm. What happens is that if the current value x is smaller than sqrt(N), the value of q will be larger than sqrt(N), hence "pushing" the average·(x+q)/2 closer to sqrt(N). Very neat.
Cheers
/Jakob
I tried that algorithm and it did not yield the accuracy I wanted.· That is why I went to the DATA format program.· I have updated my last program to work up to 343 and will be posting it.· As near as I can tell accuracy of the derived cube root is about .02 percent, or .0002, of the true cube root.
Thanks for the suggestion, anyway.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Do you have a Stamp Tester?
http://hometown.aol.com/newzed/index.html
·
Anyway, it should be possible to implement in BASCOM some way...well I'll leave it to you, since I haven't used PBASIC (is that the proper name?) for a long time...
Sid, one way to create a lookup table for DATA statements like this, without pain!, is with EXCEL. Enter all of your x values (say from 0 to 512) in a column in EXCEL on the PC. Then in the next column let EXCEL calculate the y values, the cube root of column one. Then in the third column create a string variable that consists of the word DATA and the number from the second column. Copy the third column, and paste it to your Stamp program.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
The Excel way is the way I went.· However, when I posted it to my Stamp all the numbers were in a column, so then I just highlighted each number and dragged it to its proper position in the DATA statement.· I had a column in Excel for WORD·so I just copied the whole thing to Stamp.· After I had dragged them to their proper position, all I had to do was add a comma after each number.
Excel is a great tool - I think a lot of people overlook its mathematical capabilities.
Sid