5

I wanted to know how computers calculate logarithms? I don't mean the related functions. For example, Python uses math.log() function. But I want to know what exactly does this function do? And can it be simulated again and more accurately? Is there a formula for it? Or an algorithm? (I don't think the computer has a log table!) Thanks

  • You will find answers to your question here: https://math.stackexchange.com/questions/61209/what-algorithm-is-used-by-computers-to-calculate-logarithms – rob mayoff May 05 '21 at 05:56
  • there are many ways to compute log ... for example see [Building a logarithm function in C without using float type](https://stackoverflow.com/a/42108287/2521214) ... you can use polynomial approximation, binary search of exp to obtain its inverse , IIRC also CORDIC, algebraic identitties ... anyway any decent FPU has log2,ln implemented directly as single instruction and you can convert any log base to any other very simply by multiplying it with constant – Spektre May 05 '21 at 07:36
  • @Unreal Studio There are myriad ways of computing `log()`, depending on hardware platform and design objectives. I demonstrated one possibility [here](https://scicomp.stackexchange.com/a/25126/20458) – njuffa May 05 '21 at 08:03

2 Answers2

7

The GNU C library, for example, uses a call to the fyl2x() assembler instruction, which means that logarithms are calculated directly from the hardware. Hence one should ask: what algorithm is used for calculating logarithms by computers? Depends on the CPU. For intel IA64, they use the Taylor series combined with a table.

More info can be found here and here.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
0

This is a hugely open, broad and "depends-on".

For every programming language, every different core library, every different system or so forth, different algorithms/mechanisms and machine-code instructions may be existing for performing mathematical (and any other type of) calculations.

Furthermore, even if all the programming languages, in this world, would have been using same algorithm "X", it still does not mean, that Computer calculates logarithm in way X, because, computer still will (most likely) be doing its machine-level job differently, in different circumstances, disregarding the point, that algorithms are the same (which is, very less likely).

Bear in mind, that Computer Architectures differ, Operating Systems differ, and Assembler Instructions can be very different from CPU to CPU.

I really think, that you should come up with more specific and concrete questions on this website.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66