× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: Re: Cube root
  • From: Jim Langston <jimlangston@xxxxxxxxxxxxxxxx>
  • Date: Fri, 16 Feb 2001 08:35:59 -0800
  • Organization: Pacer International

Hmm.. Good question.  I can find the C function SQRT, but that calculates
the square root of a number.

I guess you could do in mathematically, but the only way I could figure out
how to calculate the square root would take a bit of time.  I guess it depends
on how accurate you need it to be, and how fast you need it to be.  An algorithm
would be something like:

If Number = 0 return 0
If Number < 0 return -1 error (can't return root of negative number)
Answer = Number
LastAnswer = Number
HighAnswer = Number
LowAnswer = 0

dow   Answer * 3 <> Number

If    Answer * 3 < Number
eval  Answer = Answer + (HighAnswer - Answer) / 2  (Split difference)
Eval  HighAnswer = LastAnswer
Else
Eval  Answer = Answer - (Answer - LowAnswer) / 2  (Split Difference)
Eval  LowAnswer = LastAnswer
EndIf

If   LastAnswer = Answer return Answer (Close as we can get with precision)

Eval  LastAnswer = Answer

endDo

Return Answer

This is off the top of my head and I'm sure this pseudo code contains bugs, but
you should get the idea.  Also, you can find the arbitrary root of a number this
way also.  Just replace the * 3 with * Root and pass it the root too.

I am not sure how many loops will occur, but using this method a number can be
found from 1 to 100 in a maximum of 7 splits.  That's 3 digits.  But it's a
geometric progression, not a linear one, so from 1 to 1000 takes only 10 splits,
1 to 10000 takes 15 splits, etc..

I am not suggesting you do it this way, by any means, just that this way should
work if you can't find another solution.

Regards,

Jim Langston

> Walter Hesius wrote:
> 
> Hi all,
> 
> I would like to know a way to calculate the cube root of a number. We are on 
>V4R4.
> 
> Thanx.
+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.