log2

This is a discussion on log2 within the C++ Programming forums, part of the General Programming Boards category; I'm unable to get log2 to work in my program. does anyone have an idea to get this to work. ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    log2

    I'm unable to get log2 to work in my program.
    does anyone have an idea to get this to work.
    i have read the following
    Code:
    #include<cmath>
    
    double logResult=0;
    
    logResult=log2(128);
    
    cout<<logResult;
    i get an error of
    log2 undeclared identifier
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    There is no log2 in the c standard. It's either log or log10. If you want to get a base two log value, you'd have to
    Code:
    log(128.0)/log(2.0);

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,528
    It is normally a good idea to check if a function exists before you attempt to use it. log2() is not a standard function in C or C++.

    The only standard functions are log() which computes the natural logarithm (to base 2.71828...) and log10() which computes the logarithm to base 10.

    The log of x in some arbitrary base can be computed as log(x)/log(base).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Typecasting and math function
    By wozaa in forum C++ Programming
    Replies: 6
    Last Post: 03-18-2009, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21