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++.
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.