473,546 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cube root computation

Hello,

first of all sorry for crossposting, but I could not decide which group
is more appropriate. To my question: Recently I've came across
the code in GCC standard library, which computes the cube root
of a real (floating point) number. Could anyone explain me the math
behind the computation? Here's the snippet:

---8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<--

#define CBRT_2 1.2599210498948 731648
#define CBRT_2_SQR 1.5874010519681 994748

static const double cbrt_factors[5] =
{
1.0/CBRT_2_SQR,
1.0/CBRT_2,
1.0,
CBRT_2,
CBRT_2_SQR
};

double cbrt(double x)
{
double xm, ym, u, t2;
int xe;

xm = frexp(fabs(x), &xe);

u = (+0.35489576504 3919860
+ ((+1.5081919378 1584896
+ ((-2.1149949416737 1287
+ ((+2.4469312256 3534430
+ ((-1.8346927748361 3086
+ (+0.78493234497 6639262 -
0.1452638993854 86377*xm)*xm)
*xm))
*xm))
*xm))
*xm));
t2 = u*u*u;
ym = u*(t2 + 2.0*xm)/(2.0*t2 + xm)*cbrt_factor s[2 + xe%3];

return ldexp(x > 0.0 ? +ym : -ym, xe/3);
}

---8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<--

For those of you, who do not know frexp & ldexp functions,
here are their descriptions:

double frexp(double x, int *expptr)
The frexp function breaks down the floating-point value (x)
into a mantissa (m) and an exponent (n), such that
the absolute value of m is greater than or equal to 0.5
and less than 1.0, and x = m*2^n. The integer exponent
n is stored at the location pointed to by expptr.

double ldexp(double x, int exp)
The ldexp function returns the value of x*2^exp,
if successful.

Thanks for your concern,

John Walker jr.

Jan 16 '06 #1
2 9514
jo**********@po st.cz wrote:
Recently I've came across
the code in GCC standard library, which computes the cube root
of a real (floating point) number. Could anyone explain me the math
behind the computation? Here's the snippet:

---8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<--

#define CBRT_2 1.2599210498948 731648
#define CBRT_2_SQR 1.5874010519681 994748

static const double cbrt_factors[5] =
{
1.0/CBRT_2_SQR,
1.0/CBRT_2,
1.0,
CBRT_2,
CBRT_2_SQR
};

double cbrt(double x)
{
double xm, ym, u, t2;
int xe;

xm = frexp(fabs(x), &xe);

u = (+0.35489576504 3919860
+ ((+1.5081919378 1584896
+ ((-2.1149949416737 1287
+ ((+2.4469312256 3534430
+ ((-1.8346927748361 3086
+ (+0.78493234497 6639262 -
0.1452638993854 86377*xm)*xm)
*xm))
*xm))
*xm))
*xm));
t2 = u*u*u;
ym = u*(t2 + 2.0*xm)/(2.0*t2 + xm)*cbrt_factor s[2 + xe%3];

return ldexp(x > 0.0 ? +ym : -ym, xe/3);
}

I don't think gcc comes with such a function; perhaps it comes with your
glibc?
This looks fairly straightforward , but unnecessarily obscure and in need
of commenting. Certainly not very original, as Googling came right up
with a 30 year old Fortran version. The argument is analyzed as xm *
pow(2,xe) * sign(x). There is a polynomial approximation, evaluated by
Horner's method, for 1/cuberoot(2) times the cube root over the range
[.5,1.), followed by a disguised Newton iteration. This result is
multiplied by cube root of pow(2,xe). It would appear that accuracy
could be improved by making the Newton iteration adjustment at the end.
For efficiency, one would want to assure that that xe/3 is not
performed twice. Note avoidance of the standard C div() function.
Jan 16 '06 #2

jo**********@po st.cz wrote:
Hello,

first of all sorry for crossposting, but I could not decide which group
is more appropriate. To my question: Recently I've came across
the code in GCC standard library, which computes the cube root
of a real (floating point) number. Could anyone explain me the math
behind the computation? Here's the snippet:

---8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<--

#define CBRT_2 1.2599210498948 731648
#define CBRT_2_SQR 1.5874010519681 994748

static const double cbrt_factors[5] =
{
1.0/CBRT_2_SQR,
1.0/CBRT_2,
1.0,
CBRT_2,
CBRT_2_SQR
};

double cbrt(double x)
{
double xm, ym, u, t2;
int xe;

xm = frexp(fabs(x), &xe);

u = (+0.35489576504 3919860
+ ((+1.5081919378 1584896
+ ((-2.1149949416737 1287
+ ((+2.4469312256 3534430
+ ((-1.8346927748361 3086
+ (+0.78493234497 6639262 -
0.1452638993854 86377*xm)*xm)
*xm))
*xm))
*xm))
*xm));
As already explained, this is a polynomial approximation
for the cube root of a number in what you say is the range
[0.5,1.0]. It's a fifth-order polynomial in nested form, which
is the most efficient way to evaluate a polynomial.

When you break a number down into mantissa and exponent,
x = xm * 2^xe, the cube root is given by cbrt(xm) * 2^(xe/3.0).
However, if xe is not an integer, then 2^(xe/3.0) is going
to require some computation.

The solution is to break it down into 2^(integer part of xe/3)*
2^(fractional part of xe/3). The table handles the possible
values of 2^(1/3) and 2^(2/3).
t2 = u*u*u;
ym = u*(t2 + 2.0*xm)/(2.0*t2 + xm)*cbrt_factor s[2 + xe%3];


The cbrt_factors lookup is to handle non-integer powers
of 2, as I said. I'm not sure of the purpose of the first part
of this expression. It may be a single-step Newton's
method to improve the estimate u.

- Randy

Jan 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2546
by: mrwoopey | last post by:
Hi, My OLAP data cube is giving me the following error when I am manipulating OLAP data cube views: "the data being processed is over the allowed limit" I know that this message is caused by the size limit of each form field that is retrieved in the Request object is 102,399 bytes. The error occurs when I exceed this limit.
0
3100
by: mrwoopey | last post by:
Hi, I am using the OLAP data cube in a web browser (using the code from the SQL 2000 toolkit). The OLAP services is on database server and the web interface is on the web server. If we do simple queries on the cube, it works fine. When we do complex queries the cube in the web browser loads very slow or errors. Also, I noticed that when it...
0
1803
by: Fan Ruo Xin | last post by:
I installed Stinger in my PC (w2k). I need to do a quick compare between DB2 OLAP server and DB2 Cube Views. And I only found DB2 Cube Views version8.1 trial code from IBM website. I didn't want to spend a lot of time on this. So I installed the Cube Views 8.1, without uninstall ESE V8.2 code first last Friday. It looks like everything was OK....
0
1514
by: DC01 | last post by:
I have added a new measure successfully into the normal cube. I then add it to the virtual cube and reprocess all cubes. I can browse the normal cube successfully. Then when I try and browse the virtual cube it says 'Retrieving Data' but then thats it, it just hangs like that. I am using AS2000. The other problem I have is that the data...
0
1211
by: Tim | last post by:
Hi Folks, I'm not certain if this is the correct group to post this question in, if there is a more appropriate one please advise. We have a cube that takes a little time to build, length of time varies by point in day and source SQL Server loading. While building the cube is 'off-line' and this is bothering the powers that be. As the...
2
7761
by: chitrapandy | last post by:
how to write a c program for this question. what is the concept behind this question. Assuming that a function f() and its derivative fprime() are provided, design a function to compute the roots of f(). Demonstrate how your function can be used to compute square root and cube root of a number n.
4
14660
by: sathyancse | last post by:
Hi, i found the new method of find cube root of the given value.i want to implement in the c++ program.ex: if you give the answer the certain value i find the cube value.(i.e)if you give the ans 1331.i find the cube root is 11.i want to implement in the program please give some tips...
7
3176
by: J-Burns | last post by:
Hello. Need some help here. I have a 4*4 cube. So the equation of the cube becoming: x + 4*y + 16*z Now i want to rotate this cube 90 degrees anticlockwise( a right rotation). How can i do that? The rotation must take place with the axis of rotation being the straight line through the center of the cube and perpendicular to the...
5
20297
by: Brock | last post by:
I routinely use sqrt(x) for getting a cube root in vb.net. does anyone know of a way of getting the cube root? I tried cbrt(x) and that didn't work. thanks!
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.