Thread: log10(long long) ?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    log10(long long) ?

    I have a long long type that i am trying to find the log10 of but I can't seem to get around an error..any solutions?

    thanks

    (n2 is long long)
    Code:
    unsigned long long L2 = log10(n2) /  log10(2);
    Code:
    Error	1	error C2668: 'log10' : ambiguous call to overloaded function	c:\documents and settings\all users\desktop\3216968-src.cpp	14


    log10 - C++ Reference

    takes double and long double..should I be trying to do a conversion?

    (and if it matters, the value i am holding in n2 is slightly under 1million)

    thanks
    Last edited by rodrigorules; 01-27-2010 at 02:38 PM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    I think you must use some explicit recast from long long
    may be
    log10((real)n2);

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    If you're using "using namespace std"... then try "::log10" in stead of log10. Hooray for namespace pollution.
    DON'T use using namespace std.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Be warned that long long and unsigned long long are just compiler extensions at this point of time, although they will likely be part of the next version of C++.

    Quote Originally Posted by rodrigorules
    takes double and long double..should I be trying to do a conversion?
    That would indeed resolve the ambiguity:
    Code:
    unsigned long long L2 = static_cast<unsigned long long>(log10(static_cast<double>(n2)) / log10(2.0));
    Quote Originally Posted by EVOEx
    If you're using "using namespace std"... then try "::log10" in stead of log10. Hooray for namespace pollution.
    This is not reliable: it may well be the case that there are overloads of log10 in the global namespace with parameter types that match a long long equally well.

    Quote Originally Posted by EVOEx
    DON'T use using namespace std.
    In this context, such a using directive should be okay.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    thank you all

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If you're working with long long, you may consider using long double for the log input, for more precise calculation.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Quote Originally Posted by King Mir View Post
    If you're working with long long, you may consider using long double for the log input, for more precise calculation.
    How is that so?

    long long has 64 bits precision. Double "wastes" space for exponent.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Since you are looking for a result that is integral (i.e. have to round up or down anyway) it isn't hard to compute the logarithm directly. For example (assuming your compiler supports long long types);
    Code:
    #include <stdexcept>
    long long unsigned log_10(long long unsigned x)
    {
         if (x == 0) throw std::out_of_range("Attempted to compute log of zero");
         long long unsigned result_rounded_down = 0;
         long long unsigned power = 10;
         while (power < x)
         {
              ++result_rounded_down;
              power *= 10;
         }
         return result_rounded_down;
    }
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 04:46 PM
  3. Problem in Converting Unsigned long to String
    By cprogrammer_18 in forum C Programming
    Replies: 8
    Last Post: 01-14-2006, 08:57 AM
  4. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM
  5. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM