I need to write a natural logarithm with just limited available operations. This is to be implemented as a function into a C program. The problem is, our product is security relevant so the source code has to be well documented and tested. The testing process is why we are writing all code from scratch, as the C standard librarys are not well documented and writing our own tests for it that cover the whole library would take longer as just developing what we need our self.
Long story short:
I reviewed some opensource math librarys and just encoutnered for every ln()-function/method a quite big source with some nested dependencies to other even biger math-functions.
double log(double x)
{
int e;
double y, z;
/* Test for domain */
if( x <= 0.0 )
return 0;
/* separate mantissa from exponent */
/* Note, frexp is used so that denormal numbers
* will be handled properly.
*/
x = frexp( x, &e );
/* logarithm using log(x) = z + z**3 P(z)/Q(z),
* where z = 2(x-1)/x+1)
*/
if( (e > 2) || (e < -2) )
{
if( x < SQRTH )
{ /* 2( 2x-1 )/( 2x+1 ) */
e -= 1;
z = x - 0.5;
y = 0.5 * z + 0.5;
}
else
{ /* 2 (x-1)/(x+1) */
z = x - 0.5;
z -= 0.5;
y = 0.5 * x + 0.5;
}
x = z / y;
/* rational form */
z = x*x;
z = x * ( z * polevl( z, R, 2 ) / p1evl( z, S, 3 ) );
y = e;
z = z - y * 2.121944400546905827679e-4;
z = z + x;
z = z + e * 0.693359375;
goto ldone;
}
/* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
if( x < SQRTH )
{
e -= 1;
x = ldexp( x, 1 ) - 1.0; /* 2x - 1 */
}
else
{
x = x - 1.0;
}
/* rational form */
z = x*x;
y = x * ( z * polevl( x, P, 5 ) / p1evl( x, Q, 5 ) );
if( e )
y = y - e * 2.121944400546905827679e-4;
y = y - ldexp( z, -1 ); /* y - 0.5 * z */
z = x + y;
if( e )
z = z + e * 0.693359375;
ldone:
return( z );
}
This is code was allready hardened by me. But it is still depending on 4 other math functions (ldexp, polevl, p1evl and frexp) which I would have to implement my self aswell.
My math knowledge is allready overstrained with this code, so I couldn't verify even this is working correctly after I trimmed the code not mentioning implementing the additional implementations or the moment I had to document and jsutify the tests.
So why I'm asking here and not on StackOverflow:
I want to know, are there any easyer implementations or approximation procedures to determine the ln in a not that complex way, so I don't have to waste alot of resources for studying the maths behind?



logas proofing some one elselogworks flawless, wouldn't it? – Zaibis Sep 16 '15 at 14:40logwhile I can accept a certain inaccuracy – Zaibis Sep 16 '15 at 14:42