Calculating the log of a number

Discussion in 'C++' started by Protoman, Sep 16, 2005.

  1. Protoman

    Protoman Guest

    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!!!
    Protoman, Sep 16, 2005
    1. Advertisements

  2. Protoman

    kevin.hall Guest

    Go read a math book about how to calculate logarithms by hand. Than go
    implement that in code.
    kevin.hall, Sep 16, 2005
    1. Advertisements

  3. Protoman

    osmium Guest

    osmium, Sep 16, 2005
  4. Protoman

    Protoman Guest

    That's an ebook I have to purchase; I'd like a link to a place that's
    Protoman, Sep 16, 2005
  5. The library?

    John Harrison, Sep 16, 2005
  6. Protoman

    Protoman Guest

    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.
    Protoman, Sep 16, 2005
  7. 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 Harrison, Sep 16, 2005
  8. 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 -
    Risto Lankinen, Sep 16, 2005
  9. Protoman

    Protoman Guest

    Can you make that just a little clearer?
    Protoman, Sep 16, 2005
  10. 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 Harrison, Sep 16, 2005
  11. Protoman

    osmium Guest

    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

    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?


    -- Osmium
    osmium, Sep 16, 2005
  12. Protoman

    Kai-Uwe Bux Guest

    a) quote what you are refering to:

    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:

    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.


    Kai-Uwe Bux
    Kai-Uwe Bux, Sep 16, 2005
  13. 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, Sep 16, 2005
  14. I take everything back.
    I have not checked page 68 up to now
    Karl Heinz Buchegger, Sep 16, 2005
  15. Protoman

    osmium Guest

    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.
    osmium, Sep 16, 2005
  16. UL. Preliminary planning started in 1952, active work started 1956,
    first published 1964.
    I commend the Prefaces and Foreword to you. They contain dates ;-)
    Richard Herring, Sep 16, 2005
  17. Protoman

    mlimber Guest

    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
    mlimber, Sep 16, 2005
  18. Protoman

    Howard Guest

    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, 2005
  19. inline
    double abs(const doubleComplex& c) {
    return hypot(c.real(), c.imag()); }
    double arg(const doubleComplex& c) {
    return atan2(c.imag(), c.real()); }
    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

    in file svmtl/include/doubleComplex.h
    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 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.
    E. Robert Tisdale, Sep 16, 2005
  20. Protoman

    Jim Langston Guest

    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.
    Jim Langston, Sep 17, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.