Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Sunday, 25 March 2012

Modulus With Negative Numbers in C & C++


What it is correct answer when calculating the modulus of two numbers when one of the numbers is negative? When it comes to computers, the answer is: it depends.

The starting point is basic division of integers. Take a dividend, a, and divide it by the divisor, b, to get some result that is a quotient, q, and a remainder, r, if any. The modulus of a and b is the remainder, r. This division theorem works for all numbers that are integers, provided b is not zero.

 
If the a and b have the same sign (both positive or both negative), the quotient is positive. If their signs are opposite (one negative, one positive), the quotient is negative. What about the modulus? It’s easy to think the same process applies, but does it? No.

To calculate r, use the division equation for known values of a, b and q.

The following code in Visual C++ 2010 shows the four different possibilities pos+/neg- values for a and b.

 // Header.
printf("Modulus With Negative Numbers\n\n");
printf("a x b = q + r OR b x q + r = a\n\n");

int a = 7;
int b = 2;
printf("a = %d, b = %d \n", a, b);
printf("q: %d / %d = %d \n", a, b, a / b);
printf("r: %d mod %d = %d \n", a, b, a % b);
// 2 x 3 + 1 = 7
printf("%d x %d + %d = %d \n\n", b, a / b, a % b, a);

a = -7;
b = 2;
printf("a = %d, b = %d \n", a, b);
printf("q: %d / %d = %d \n", a, b, a / b);
printf("r: %d mod %d = %d \n", a, b, a % b);
// 2 x -3 - 1 = -7
printf("%d x %d + %d = %d \n\n", b, a / b, a % b, a);

a = 7;
b = -2;
printf("a = %d, b = %d \n", a, b);
printf("q: %d / %d = %d \n", a, b, a / b);
printf("r: %d mod %d = %d \n", a, b, a % b);
// -2 x -3 + 1 = 7
printf("%d x %d + %d = %d \n\n", b, a / b, a % b, a);

a = -7;
b = -2;
printf("a = %d, b = %d \n", a, b);
printf("q: %d / %d = %d \n", a, b, a / b);
printf("r: %d mod %d = %d \n", a, b, a % b);
// -2 x 3 - 1 = -7
printf("%d x %d + %d = %d \n\n", b, a / b, a % b, a);

Output.



You will get the same results in Visual Basic.

When it comes to using the modulus operator, test it with a range of expected parameters to make sure it gives the result you expect and want. Not all versions of C or C++ or other languages behave the same. If all else fails, create your own modulus function to work the way you want.

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.