Math.log2 ?

Discussion in 'Ruby' started by Brian Schroeder, Sep 2, 2004.

  1. Hello Group,

    I wondered, why there is no log2 function in Math. I expected
    Math.log = log2 and
    Math.ln = log_e.

    And spend some time searching for the bug in my algorithm that used log2.

    Now I use
    module Math
    def log2(n)
    Math.log(n) * 1.0 / Math.log(2)
    end
    end

    But this seems quite ugly to me. Is there a reason that log2 is missing,
    or have I overlooked something.

    Regards,

    Brian

    --
    Brian Schröder
    http://www.brian-schroeder.de/
    Brian Schroeder, Sep 2, 2004
    #1
    1. Advertising

  2. Hi,

    In message "Math.log2 ?"
    on Thu, 2 Sep 2004 17:50:19 +0900, Brian Schroeder <> writes:

    |I wondered, why there is no log2 function in Math. I expected
    |Math.log = log2 and
    |Math.ln = log_e.
    |
    |And spend some time searching for the bug in my algorithm that used log2.

    Mostly because no one told me whether its portable among platforms.
    If enough information is provided, I'd love to add it among other math
    functions. Besides that, you can check extmath package from RAA now.

    matz.
    Yukihiro Matsumoto, Sep 2, 2004
    #2
    1. Advertising

  3. On Thu, 02 Sep 2004 18:10:17 +0900, Yukihiro Matsumoto wrote:

    > Hi,
    >
    > In message "Math.log2 ?"
    > on Thu, 2 Sep 2004 17:50:19 +0900, Brian Schroeder
    > <> writes:
    >
    > |I wondered, why there is no log2 function in Math. I expected Math.log =
    > |log2 and
    > |Math.ln = log_e.
    > |
    > |And spend some time searching for the bug in my algorithm that used log2.
    >
    > Mostly because no one told me whether its portable among platforms. If
    > enough information is provided, I'd love to add it among other math
    > functions. Besides that, you can check extmath package from RAA now.
    >
    > matz.


    That is interesting enough. Anybody here would like to explain to me, why
    log_2 is harder than log_10 or ln. I just assumed that anything binary
    would be nice for computers.

    Thanks,

    Brian

    --
    Brian Schröder
    http://www.brian-schroeder.de/
    Brian Schroeder, Sep 2, 2004
    #3
  4. Brian Schroeder wrote:
    > That is interesting enough. Anybody here would like to explain to me, why
    > log_2 is harder than log_10 or ln. I just assumed that anything binary
    > would be nice for computers.


    It's neither harder nor easier, it's just not that useful. Base 2
    logarithms aren't really needed in most math and engineering. Even in
    computer science and information theory, where the base 2 log is an
    important analytical concept, it's rarely employed in precision
    calculations. Those are not the kind of problems you attack with
    numerical methods.

    It takes a lot of care and skill to develop a math library; much more
    than naively coding power series expansions of transcendental functions.
    People tend to put that work where it's really needed.

    The good news is that for most purposes in computer science,

    log2(x) = ln(x) / ln(2)

    is plenty precise. :) Note that ln(2) is a constant.

    Steve
    Steven Jenkins, Sep 3, 2004
    #4
  5. Brian Schroeder

    Phil Tomson Guest

    In article <>,
    Steven Jenkins <> wrote:
    >Brian Schroeder wrote:
    >> That is interesting enough. Anybody here would like to explain to me, why
    >> log_2 is harder than log_10 or ln. I just assumed that anything binary
    >> would be nice for computers.

    >
    >It's neither harder nor easier, it's just not that useful. Base 2
    >logarithms aren't really needed in most math and engineering. Even in
    >computer science and information theory, where the base 2 log is an
    >important analytical concept, it's rarely employed in precision
    >calculations. Those are not the kind of problems you attack with
    >numerical methods.
    >
    >It takes a lot of care and skill to develop a math library; much more
    >than naively coding power series expansions of transcendental functions.
    >People tend to put that work where it's really needed.
    >
    >The good news is that for most purposes in computer science,
    >
    >log2(x) = ln(x) / ln(2)
    >
    >is plenty precise. :) Note that ln(2) is a constant.



    I do occassionally need Log base 2 operations. Mostly I need to figure
    out how many bits are needed to represent some range (for example, given a
    range of 0 to 7, I need a 3 bit counter) in a hardware representation. I
    also ran into a need for log2 when I was doing some Karnaugh map
    manipulations for a class project I did last quarter.

    You're right though, I don't need any sort of precision for these
    applications, integers only:

    log2(2) => 1
    log2(3) => 2 (it's actually something like 1.5849, apply ceiling op)
    log2(4) => 2
    log2(5) => 3
    log2(6) => 3
    log2(7) => 3
    log2(8) => 3
    log2(9) => 4
    .....

    As I recall, since I only needed up log2(32) (5 bits), and since it needed
    to be as fast as possible, I just defined my own method that uses a case
    statement to 'lookup' the desired value.


    Phil
    Phil Tomson, Sep 3, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ALuPin

    log2(N)

    ALuPin, Aug 27, 2004, in forum: VHDL
    Replies:
    4
    Views:
    15,892
    texvnirap
    Sep 17, 2009
  2. chirs
    Replies:
    18
    Views:
    757
    Chris Uppal
    Mar 2, 2004
  3. AciD_X
    Replies:
    4
    Views:
    8,092
    Jonathan Turkanis
    Apr 1, 2004
  4. Phrogz
    Replies:
    8
    Views:
    277
    Morton Goldberg
    Feb 8, 2007
  5. VK
    Replies:
    15
    Views:
    1,155
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page