Shop OBEX P1 Docs P2 Docs Learn Events
Extracting cube root — Parallax Forums

Extracting cube root

NewzedNewzed Posts: 2,503
edited 2004-12-28 19:56 in BASIC Stamp
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

Comments

  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-12-22 17:27
    Hi Sid,

    Extracting cube roots when you are bored? shakehead.gif 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
  • NewzedNewzed Posts: 2,503
    edited 2004-12-23 00:06
    That works, Tracyjumpin.gif

    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
  • NewzedNewzed Posts: 2,503
    edited 2004-12-23 18:58
    I have upgraded my program for extracting cube roots.· It will now handle numbers up 216.· In addition, I have switched to a DATA format.· The entire program now takes about 528 bytes, including the DATA storage.

    Cube_C.bs2 is attached.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    4 digit serial LED

    http://hometown.aol.com/newzed/index.html
  • Jakob SelbingJakob Selbing Posts: 8
    edited 2004-12-28 17:56
    There's a simple algorith for extracting cubic roots. Let's say we want the cubic root of the number N. The current guess for the cubic root will be called x.

    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.scool.gif

    Cheers
    /Jakob
  • NewzedNewzed Posts: 2,503
    edited 2004-12-28 18:49
    Hi, 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
    ·
  • Jakob SelbingJakob Selbing Posts: 8
    edited 2004-12-28 19:22
    Really? I tried it in MATLAB, and 12 iterations typically gave an accuracy of 1e-12 on a number of around 1e5. Of course, this was for floating point calculations...
    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...
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-12-28 19:45
    Hi Jakob, I think the algorithm you suggested extracts square roots, not the cube roots Sid is after here. Do you know a similar algorithm for the cube root? The Stamp does only integer arithmetic, so this kind of algorithm requires working in multiple word precision to get the accuracy needed when calculating the ratios.

    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
  • NewzedNewzed Posts: 2,503
    edited 2004-12-28 19:56
    Hi, Tracy

    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
Sign In or Register to comment.