logo elektroda
logo elektroda
X
logo elektroda

Calculate Natural Logarithm in C++: Using math.h Library and Function Names

MARIO159 28138 8
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 1944165
    MARIO159
    Level 12  
    Posts: 61
    Help: 1
    Questions as in the topic how can I calculate the natural lagarithm from some number. I didn't notice the function in math.h. and what is the name of the function :)
  • ADVERTISEMENT
  • #2 1944301
    Sam Sung
    Level 33  
    Posts: 2014
    Help: 227
    Rate: 583
    There is a function in
    called log :)
    Quote:
    EXP(3) Linux Developer's Manual EXP(3)
    (...)
    SYNTAX
    #include
    (...)
    double log(double x);
    (...)
    DESCRIPTION
    (...)
    The log() function returns the natural logarithm of x.
  • ADVERTISEMENT
  • #3 1944306
    tsoltys
    Level 15  
    Posts: 82
    Help: 12
    Rate: 4
    MARIO159 wrote:
    Questions as in the topic how can I calculate the natural lagarithm of some number. I didn't notice the function in math.h. and what is the name of the function :)


    It is - you just need to look at the math.h documentation.

    Quote:
    NAME
    log - natural logarithm function
    SYNOPSIS
    #include
    double log(double x);
    DESCRIPTION
    The log() function computes the natural logarithm of x, loge(x). The value of x must be positive.
  • #4 4871030
    johnyu
    Level 1  
    Posts: 1
    And is there a logarithm function with two parameters in cmath or math.h (I don't know if they are different)? (logarithm number and base)
  • #5 4871076
    krzychoocpp
    VIP Meritorious for electroda.pl
    Posts: 1866
    Help: 387
    Rate: 58
    As far as I know, there isn't. Because there is no need.

    $$\frac{\log_c b}{\log_c a} = \log_a b$$
  • ADVERTISEMENT
  • #6 5808376
    marekkiek
    Level 14  
    Posts: 141
    Help: 4
    Rate: 2
    I have this problem in one expression I need to calculate log base 2, but I can't use the logarithm function, I have to write it myself. Is it possible to see the code of a library function and to write something based on its syntax?

    Regards
  • #7 5808490
    Dr.Vee
    VIP Meritorious for electroda.pl
    Posts: 1784
    Help: 307
    Rate: 76
    You didn't say what numbers you mean (floating point, fixed point). Should the result be rounded or not?

    I wouldn't look at library function code because it's usually too much optimized to read ;)

    Instead, read e.g. on wikipedia http://en.wikipedia.org/wiki/Logarithm#Series_for_calculating_the_natural_logarithm

    Regards,
    Dr. Vee
  • #8 5809845
    Sam Sung
    Level 33  
    Posts: 2014
    Help: 227
    Rate: 583
    Maybe it's something like this - it's not a single expression, but a statement:
    for (wynik = 0; x > 1; wynik++, x >>= 1)
      ;
    (ix result of type unsigned int.)
    In a single expression of the C language in the literal sense, it probably can't be done.
    Suspecting a library function is unlikely to help, because it meets other requirements.
  • ADVERTISEMENT

Topic summary

✨ The discussion centers around calculating the natural logarithm in C++ using the math.h library. The primary function for this purpose is identified as "log". Users inquire about the existence of a logarithm function with two parameters for different bases, but it is clarified that such a function is not necessary. One user expresses a need to implement a logarithm base 2 function manually and seeks guidance on writing the code. Suggestions include consulting external resources like Wikipedia for logarithm series and a specific site for integer logarithm calculations. The conversation emphasizes the importance of understanding the types of numbers involved and the potential complexity of library function implementations.
Generated by the language model.
ADVERTISEMENT