Showing posts with label log functions. Show all posts
Showing posts with label log functions. Show all posts

Saturday, 17 March 2012

Using Logarithms


I was watching a documentary on the history of modern computers and came across a few interesting items. I say modern computers because prior to the 1940s, a computer was a person, not a thing. He, primarily, was a person who computed numbers hours after hour. It wasn’t necessarily accounting figures, but anything, like artillery tables, navigational guides, or census statistics. Lots and lots of numbers. Numbers computed without aid of any mechanical devices.

If they wanted to add up a series of numbers, they had to add them up. If they wanted to multiply two numbers, they would consult tables to find the natural logarithmic of the numbers, add those numbers then find that sum in another table to give you the multiplied value. Every step of the process was an opportunity to create an error including the values in the tables. These books of tables were after all created by humans and contained errors. Imagine trying to copy out a telephone book by hand and not make a mistake.

Sometime in the 19th century Charles Babbage had the idea of creating a machine to eliminate the human error these computers created. It’s suggested his initial concept was the start of computers we know today. Perhaps. One of his difficulties was his choice to use the decimal numbering system instead of binary. Perhaps it wasn’t a choice, just standard operating procedure. Not thinking outside the box. If he had worked on a binary system, he may have built the prototype he had in mind.

And those tables? Here is an example. Say I want to know what 3.3 times 4.4 is because those are the dimensions of some object. I could do the multiplication by hand, double check my figures and report the result. What if I had to do that fifty times in a day? Or more? Look for a faster approach.

The natural logarithm of 3.3 is 1.19392247 and for 4.4, it’s 1.48160454. These log numbers come from a table. As with most people, I can add those numbers together faster than I can multiply them. The result is 2.67552701. Now I find what e to that number is and I come up with 14.52 which is 3.3 times 4.4.

So,

e(ln A + ln B) = A x B

The same works for division.

e(ln A - ln B) = A / B

I haven’t figured out how slide rulers work. No need, just curious.

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.



Sunday, 4 March 2012

The Natural Logarithmic Functions 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 natural logarithmic functions.

Even though I can’t see myself using these functions, I think it’s important to understand what they do. It’s just part of understanding the limits of the language.

Here’s a brief primer on e and the exp and log functions in C.



A graph of e to the xth power as a function of x.



Here is a snippet from the C standard (C99 N1244).




The exp(n) function returns a float equal to e to the nth power. The log function reverses the process and returns n for a given value of e to the nth power.


Test Code.

I wrote some sample code to test the functions. No unexpected results.

C often uses ++n to increment a counter. It’s shorter version of n = n + 1. But I wanted to increment by 0.5. I could use n = n + 0.5, but C programmers prefer to use: n += 0.5.

Also note in the for loop I assigned -5.0 to n, not -5. One is a float, the other an integer and n was declared as a float.

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


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

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

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

int main()
{
     float n;

     // Header.
     printf("Exponential & Logarithmic Functions\n\n");
     printf("n\t  exp(n) \t log(exp(n)) \n\n");

     // Sample calcs.
     for (n = -5.0; n < 6; n += 0.5)
           printf("%5.2f\t%10.6f\t%10.6f\n", n, exp(n), log(exp(n)));

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

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


Output.