2

I know that it may seem a duplicate question, but I could not find my answer in previous questions. I mean how to write a log base 10 function by simple loops and not using built in log function in c++.

hamed
  • 127
  • 1
  • 3
  • 9
  • 2
    By computing log2, and using the log change of base formula with a stored constant – StoryTeller - Unslander Monica Jan 01 '17 at 14:51
  • Both log2 and log10 is my question. How to write this function from beginning? – hamed Jan 01 '17 at 14:53
  • Do you have no idea at all how to start? This is a very general question, and simply writing the function for you is not very helpful. – Dave Costa Jan 01 '17 at 14:55
  • I wrote my code but it just work properly for numbers like 10, 100, 1000 and so on. I want the code to work properly for every number. – hamed Jan 01 '17 at 15:04
  • integer or floating-point log? either way there are tons of duplicates. see https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 – phuclv Jan 01 '17 at 15:05
  • You have to study how the log function can be approximated via power series or with other iterative methods. This knowledge is more related to mathematics than to programming. Start reading [here](https://en.wikipedia.org/wiki/Natural_logarithm#Derivative.2C_Taylor_series). – n. m. could be an AI Jan 01 '17 at 15:07
  • [Logarithm computing without math.h](http://stackoverflow.com/q/18961889/995714) – phuclv Jan 01 '17 at 15:13
  • Just use the builtin log function. It will be faster than any loop you write. – Raymond Chen Jan 01 '17 at 16:32

3 Answers3

2

The easiest way is to calculate the natural logarithm (ln) with a Taylor series. Once you have found the natural logarithm, just divide it by ln(10) and you get the base-10 log.

The Taylor series is quite simple to implement in C. If z is the number for which you are seeking the log, you just have to loop a few iterations multiplying an accumulator by (z-1) each time. To a limit, the more iterations you run, the more accurate your result will be. Check it a few times against the libC log10() version until you are happy with the precision.

This is a "numeric approach". There are other numeric solutions to finding the logarithm of a number which can give more accurate results. Some of them can be found in that Wikipedia link I gave you.

giusti
  • 3,156
  • 3
  • 29
  • 44
1

Assuming by "log base 10" you mean "the number of times n can be divided by 10 before resulting in a value < 10":

log = 0;
// Assume n has initial value N
while ( n >= 10 ) {
    // Invariant: N = n * 10^log
    n /= 10;
    log += 1;
}
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

You'll get faster convergence with Newton's Method. Use something like this (hand written not compiled or tested uses f(r) = 2**r - x to compute log2(x) ):

double next(double r, double x) {
    static double one_over_ln2 = 1.4426950408889634;
    return r - one_over_ln2 * (1 - x / (1 << static_cast<int>(r)));

double log2(double x) {
    static double epsilon = 0.000000001; // change this to change accuracy
    double r = x / 2;. // better first guesses converge faster
    double r2 = next(r, x);
    double delta = r - r2;
    while (delta * delta > epsilon) { 
        r = r2;
        r2 = next(r, x);
        delta = r - r2
    }
    return r2;
}

double log10(double x) {
    static double log2_10 = log2(10);
    return log2(x) / log2_10;
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54