Алгебраические уравнения - Кубический корень

11 Nov in Math

Иногда в программе нужно вычислить кубический корень. Раньше для этой цели я пользовался парой функций логарифм-экспонента, а потом (13.05.2004) решил сделать отдельную функцию и назвал её cbrt. В результате получилась функция, которая немного быстрей и немного точней, чем exp ( log ( x ) / 3 ).

static double _cbrt ( double x )
{
double s = 1.;
while ( x < 1. )
{
x *= 8.;
s *= 0.5;
}
while ( x > 8. )
{
x *= 0.125;
s *= 2.;
}
double r = 1.5;
for ( int i = 6; --i >= 0; )
{
r -= 1./3. * ( r - x / ( r * r ) );
}
return r * s;
}

double cbrt ( double x )
{
if ( x > 0 ) return _cbrt ( x ); else
if ( x < 0 ) return-_cbrt (-x ); else
return 0.;
}
Вначале аргумент приводится к диапазону [1,8], затем вычисляется корень методом Ньютона.

 

Comments

Post new comment

 
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.
By submitting this form, you accept the Mollom privacy policy.
Developed by NStudioCorp.com All trademarks and copyrights on this site are owned by their respective owners.
Comments are owned by the Poster. The Rest © 2000-2011 Firstov.com

About Firstov.com |  Terms of Service |  Support |  Contact Us |  Advertise