473,468 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calculating the log of a number

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!!!

Sep 16 '05 #1
21 8568
Go read a math book about how to calculate logarithms by hand. Than go
implement that in code.

Sep 16 '05 #2
"Protoman" writes:
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!!!


You could do it as the way shown here:

http://www.convertit.com/go/converti...es=150&Page=65
Sep 16 '05 #3
That's an ebook I have to purchase; I'd like a link to a place that's
free.

Sep 16 '05 #4
Protoman wrote:
That's an ebook I have to purchase; I'd like a link to a place that's
free.


The library?

john
Sep 16 '05 #5
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.

Sep 16 '05 #6
Protoman wrote:
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
Sep 16 '05 #7

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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 -
Sep 16 '05 #8
Can you make that just a little clearer?

Sep 16 '05 #9
John Harrison wrote:
Protoman wrote:
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))


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
Sep 16 '05 #10
"Protoman" writes:
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

Sep 16 '05 #11
osmium wrote:

"Protoman" writes:
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?


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 ....

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 16 '05 #12
Protoman wrote:
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/converti...e=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
that link.

Best

Kai-Uwe Bux
Sep 16 '05 #13
Karl Heinz Buchegger wrote:

osmium wrote:

"Protoman" writes:
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?


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
--
Karl Heinz Buchegger
kb******@gascad.at
Sep 16 '05 #14
"Karl Heinz Buchegger" writes:
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 ....


That's kind of what it does. The link I posted was the table of contents
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.
Sep 16 '05 #15
In message <3p************@individual.net>, osmium
<r1********@comcast.net> writes
"Karl Heinz Buchegger" writes:
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 ....


That's kind of what it does. The link I posted was the table of contents
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 ;-)

--
Richard Herring
Sep 16 '05 #16
osmium wrote:
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

Sep 16 '05 #17

"mlimber" <ml*****@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
osmium wrote:
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


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
Sep 16 '05 #18
Protoman wrote:
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++.
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++
Try comp.arch.arithmetic instead.
Sep 16 '05 #19

"osmium" <r1********@comcast.net> wrote in message
news:3p************@individual.net...
"Karl Heinz Buchegger" writes: 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.
Sep 17 '05 #20
"Richard Herring" writes:

Osmium:
That's kind of what it does. The link I posted was the table of contents
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 ;-)


After being chastised I read the front matter and then realized that I had
already read it sometime in the past, but reading it again didn't change my
opinion. But I didn't make myself clear, I actually had in mind the actual
tabular data, not the physical book with its organization and extra matter
(this thread is actually about the extra matter). IOW, I visualized the
firing tables for the big guns in WW II - artillery and naval - were
computed by these WPA log tables. I still think so. A search for wpa and
ams55 hits a pay site at the one nice hit. A search for wpa and "ams 55"
hits a power point presentation which contains this:

Math tables project (NY 1938-46)
o Works Project Administration
o 37 volumes issued, trig, exp, log, etc.

here's a link to the presentation. I viewed it as HTML so I didn't get all
the "good" out of it.

http://www.google.com/search?num=20&...a+%22ams+55%22

I also found there is a recent book on this very subject. Unfortunately my
library doesn't have it. And by the time they got it on loan I would be off
on some other even more wonderful tangent.

I tried a Usenet search too, and it yielded surprisingly little, but there
is one interesting thread, which is a spin off form the book I mentioned.
It degenerated into a discussion of women being treated so shabbily.
Sep 17 '05 #21
"mlimber" wrote:
osmium wrote:
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.


I have no intention of starting an argument. But. I based my comment on
the footnote at the bottom of Eqn. 4.1.44, which says "approximations in
terms of Chebyshev polynomials" .
Sep 17 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
4
by: John | last post by:
hey all..... alright, I am frusterated to the point of throwing my machine out the window (this board went down, trying to find stuff on google, this has been a nightmare) so I hope you guys can...
5
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int...
3
by: Ron Vecchi | last post by:
I need to calculate the age of a person based on a DateTime BirthDate I was thinking TimeSpan ts = DateTime.Now - BirthDate; //I can get the days but not years. // I could check each...
3
by: Miller | last post by:
Hi, Can someone tell me how to calculate MFLOPS for the following C# code (on a Pentium 4 2.0 Ghz)? for (i=0; i<n; i++) { for (j=0; j<n; j++) { for (k=0; k<n; k++)
0
by: hlam | last post by:
Help - Calculating the total of a column in a data grid -- when data grid is part of Master-Detail set-up I have setup a Master-Detail form using Visual Studio.Net. A ListBox is the (Master)...
10
by: Lisa | last post by:
In translating the formula for calculating lottery odds for various conditions into a Visual Basic Program, I have apparently missed something in that I get errors in the part of the calculation...
4
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Say I have a class like, class Sample { public decimal first = 10; public decimal second = 20; } I have initialized it
1
by: cmb3587 | last post by:
My code runs fine for the most part...the only time it fails is when I type in a negative to end the array. I don't want the negative number to be included in the array and I thought that is what...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.