# Calculating the log of a number

P

#### Protoman

How do I calculate the log of a number? Don't tell me to use log in
cmath; I'm trying to learn complex math in C++. Got any tips/ideas on
how to do this? Thanks for the help!!!

K

#### kevin.hall

Go read a math book about how to calculate logarithms by hand. Than go
implement that in code.

P

#### Protoman

That's an ebook I have to purchase; I'd like a link to a place that's
free.

J

#### John Harrison

Protoman said:
That's an ebook I have to purchase; I'd like a link to a place that's
free.

The library?

john

P

#### Protoman

I don't go there that often; it's so far away. I'd a like to another
site show how to do it, step by step, by hand, then I'll code it.

J

#### John Harrison

Protoman said:
I don't go there that often; it's so far away. I'd a like to another
site show how to do it, step by step, by hand, then I'll code it.

Fourtunately I have a vast library at my disposal.

log(z) = log|z| + i*phase(z)

phase(z) = atan2(imag(z), real(z))

There is actually some choice about how you calculate this sort of
thing, stuff to do with branch cuts etc. So the above isn't the only way
to do it. Nor should you read any mathematical expertise into my answer.
I'm just quoting from a book.

john

R

#### Risto Lankinen

Protoman said:
How do I calculate the log of a number? Don't tell me to use log in
cmath; I'm trying to learn complex math in C++. Got any tips/ideas on
how to do this? Thanks for the help!!!

Normalize number into range 1<=x<2 by dividing/multiplying
by 2 repeatedly. Count how many times and call it exponent
(make it negative if normalization required dividing).

Next, repeatedly square the normalized number. Each time the
result >= 2 divide it by 2 and generate "1"; otherwise leave the
result as it is and generate "0". The generated stream of bits are
the fractional part of the exponent in base-2.

Then, the original number == 2^[exponent.fraction] . Multiply
by a suitable constant to get bases other than 2.

- Risto -

P

#### Protoman

Can you make that just a little clearer?

J

#### John Harrison

John said:
Fourtunately I have a vast library at my disposal.

log(z) = log|z| + i*phase(z)

phase(z) = atan2(imag(z), real(z))

Hmm, I misread your post, I thought you wanted to calculate the log of a
complex number.

Risto's answer sounded promising, and seemed pretty clear to me. Why not
try coding it and see how far you get.

john

O

#### osmium

Protoman said:
That's an ebook I have to purchase; I'd like a link to a place that's
free.

Do you have any idea what I see on my screen? I see:

"That's an ebook I have to purchase; I'd like a link to a place that's
free."

How in hell is anyone supposed to know WTF you are talking about?

In the off chance you are talking to me, I suggest this. Actually CLICK on
the link. Don't hypothesize what might happen if you click, actually CLICK.
Can you do that? Will you do that?

Jeez!

-- Osmium

K

#### Kai-Uwe Bux

Protoman said:
That's an ebook I have to purchase; I'd like a link to a place that's
free.

a) quote what you are refering to:
You could do it as the way shown here:

http://www.convertit.com/go/conv ertit/reference/ams55.asp?Res=150&Page=65

b) You don't have to purchase that pdf-file. You can freely navigate that
document. It's all online. Before complaining, have a closer look.

c) That document is really useful. Have a closer look, for instance at item
4.1.27 on page 68. Here is a link:
http://www.convertit.com/go/convertit/reference/ams55.asp?Res=150&Page=68&Submit=Go

d) You might want to play around with the following code that is based on
that piece of math:

// natural logarithm
// =================

#include <iostream>
#include <boost/lexical_cast.hpp>

int main ( unsigned argn, char * args[] ) {
if ( argn > 1 ) {
double z = boost::lexical_cast< double >( args[1] );
double x = (z-1)/(z+1);
double x_square = x*x;
double sum = 0;
for ( unsigned short i = 1; i < 20; i = i+2 ) {
sum += x/i;
x = x * x_square;
std::cout << 2*sum << '\n';
}
}
}

Try to understand why and how this code matches the series expansion from

Best

Kai-Uwe Bux

K

#### Karl Heinz Buchegger

osmium said:
Do you have any idea what I see on my screen? I see:

"That's an ebook I have to purchase; I'd like a link to a place that's
free."

How in hell is anyone supposed to know WTF you are talking about?

In the off chance you are talking to me, I suggest this. Actually CLICK on
the link. Don't hypothesize what might happen if you click, actually CLICK.
Can you do that? Will you do that?

I did.
And I also checked page 67.
Now I wonder how this might help me to come up with a formula
to calculate log (or ln) of some number, using only elementary
arithmetik (and I guess this is what Protoman wanted). Something
like a Taylor expansion or ....

K

#### Karl Heinz Buchegger

Karl said:
I did.
And I also checked page 67.
Now I wonder how this might help me to come up with a formula
to calculate log (or ln) of some number, using only elementary
arithmetik (and I guess this is what Protoman wanted). Something
like a Taylor expansion or ....

I take everything back.
I have not checked page 68 up to now

O

#### osmium

Karl Heinz Buchegger said:
I did.
And I also checked page 67.
Now I wonder how this might help me to come up with a formula
to calculate log (or ln) of some number, using only elementary
arithmetik (and I guess this is what Protoman wanted). Something
like a Taylor expansion or ....

for a chapter. You can navigate forward, to p.67 IIRC and find a series.
I think this is a Tschebychev (sp?) expansion. AMS 55 Was a monumental US
work of the 1930's to keep "computers", back when computers were people,
employed during the depression. Urban Legend? You decide. I have the book
and it is physically very imposing.

R

#### Richard Herring

osmium said:
for a chapter. You can navigate forward, to p.67 IIRC and find a series.
I think this is a Tschebychev (sp?) expansion. AMS 55 Was a monumental US
work of the 1930's to keep "computers", back when computers were people,
employed during the depression. Urban Legend? You decide.

UL. Preliminary planning started in 1952, active work started 1956,
first published 1964.
I have the book
and it is physically very imposing.

I commend the Prefaces and Foreword to you. They contain dates ;-)

M

#### mlimber

osmium said:
I think this is a Tschebychev (sp?) expansion.

I believe that's actually a Laurent series, which is the complex
generalization of a Taylor series. Tschebychev, AFAIK, has some
polynomials and filter design techniques named after him, but not
series expansions. Perhaps a lurking mathematician can correct me.

Cheers! --M

H

#### Howard

mlimber said:
I believe that's actually a Laurent series, which is the complex
generalization of a Taylor series. Tschebychev, AFAIK, has some
polynomials and filter design techniques named after him, but not
series expansions. Perhaps a lurking mathematician can correct me.

Cheers! --M

There _are_ Chebyshev expansions (such as for e^(ax)), which have Chebyshev
polynomials as [part of] their terms. Don't ask me to write one out,
though... I've moved on to simpler but better paying work!

-Howard

E

#### E. Robert Tisdale

Protoman said:
How do I calculate the log of a number?
I'm trying to learn complex math in C++.

inline
double abs(const doubleComplex& c) {
return hypot(c.real(), c.imag()); }
inline
double arg(const doubleComplex& c) {
return atan2(c.imag(), c.real()); }
inline
doubleComplex log(const doubleComplex& c) {
return doubleComplex(log(abs(c)), arg(c)); }

The above definitions comes from
The C++ Scalar, Vector, Matrix and Tensor class Library

http://www.netwood.net/~edwin/svmtl/

in file svmtl/include/doubleComplex.h
Got any tips/ideas on how to do this?

Of course.

T. Lynch, A. Ahmed, M. Schulte, T. Callaway, R. Tisdale.
"The K5 transcendental functions,"
arith, vol. 00, no. , p. 163, 12th 1995.

Look for books on computer arithmetic

Search http://www.amazon.com/ for

computer arithmetic

You will find lots of stuff including

Computer Arithmetic Algorithms by Israel Koren

Algorithms are off-topic in comp.lang.c++

J

#### Jim Langston

osmium said:
: AMS 55 Was a monumental US
work of the 1930's to keep "computers", back when computers were people,
employed during the depression. Urban Legend? You decide.

I once had a dictionary (c) 1923. Out of curiosity I looked up the word
"computer" to see if they even conceived of them back then. The definition
was: n. One who computes.