Tuesday 6 March 2012

The Base 10 Log Function in C & C++


I am making may way through all the different functions in the various standard header files that form part of C and C++. I’m still working on the contents of the <math.h> or <cmath> header files. Today is about the base 10 logarithmic function.

Say, ten, raised to the power of x, is N then the base 10 log of N is x. Example, 10 squared is 100. The base 10 log of 100 is 2 since 10 to the 2 is 100.


The exp() and log() functions are related since the return value of one can be passed to the other in a circle. There is no sister function for log10(), you have to use the power function with base 10.

Here is a snippet from the C standard (C11 N1570).


What does a domain error mean? First, it only occurs in relation to the functions in the math library. Second, it means you passed an argument to the function which is outside of the range of arguments allowed. The log10 function only allows numbers greater than zero.

From subclause 7.12.1 of the C11 standard:

For all functions, a domain error occurs if an input argument is outside the domain over which the mathematical function is defined. The description of each function lists any required domain errors; an implementation may define additional domain errors, provided that such errors are consistent with the mathematical definition of the function.

The return value for a domain error is dependent on the compiler. In Visual C++ 2010, the return value is -1.#IND000000000000. I suspect the IND stands for indeterminate. Try a google search on that return value.

A pole error indicates the return value approaches zero or positive or negative infinity under the limit functions in calculus. The implement of a pole error is compiler dependent and in Visual C++ 2010, the return value is -1.#INF000000000000 or negative infinity.

Passing a non-positive argument to the function does not result in a compile or run-time error. It’s up to the programmer to ensure proper argument values are passed to the function.


Test Code.

I tested the functions in Visual C++ 2010 as an console application.

Interestingly, I discovered something about the printf function. It prints out zeros when there is a mismatch between a format specifier and the argument. I used “%d” with a float variable and “%f” with other floats. In all cases zero was displayed even when there variables weren’t zero. More on that in another blog, but once I matched them, it worked properly.


// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("Logarithmic Base 10 Function\n\n");
     printf("\tn\t   log10(n)\t10^log10((n)) \n\n");

     // Counter.
     int i;
     // Argument.
     double n;

     for (i = 0; i < 8; ++i)
     {
           n = pow(10.0, i);
           printf("%9.0f\t%10.4f\t%10.0f\n", n, log10(n), pow(10.0,log10(n)));
     }
     printf("\n\n");

     // Negative argument.
     printf("Negative argument: %f ", log10(-1.0));
     printf("\n\n");

     // Zero value argument.
     printf("Zero value argument: %f ", log10(0.0));
     printf("\n\n");

     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main


Output.



No comments:

Post a Comment