Need help in doing math calculation for natural logarithm (ln)
12,187
Solution 1
#include <math.h>
double fun(double x)
{
return 20 * log( x + 3 ); //base-e logarithm!
}
//usage
double y = fun(30);
For base-10 logarithm, use log10()
.
Solution 2
double myfunction(int x){
return (20* log(x+3) );
}
?
And you call it :
double y = myfunction(yourX);
Solution 3
#include <math.h>
double function(double x)
{
double y = 20 * log(x + 3.0);
return y;
}
Solution 4
The log function in the c library is performs a natural logarithm ('ln'). See this for more details: CPlusPlus - log
Solution 5
Although the question is tagged C++, questioner is asking for a C implementation:
#include <math.h>
double myFunction(double x) {
return 20.0 * log(x + 3.0);
}
Author by
Birdkingz
Updated on July 18, 2022Comments
-
Birdkingz almost 2 years
How do I write a function for this in C language?
y = 20 ln (x + 3) ?
How do I write the ln function?
-
Stephen Canon about 13 yearsThis is C++; the question asks for C.
-
R.. GitHub STOP HELPING ICE about 13 yearsRemove the
std::
and the useless temp variable. -
John Bode about 13 yearsDon't forget to
#include <math.h>
and to link against the math library if necessary. -
ronag about 13 yearsChanged it to C. I kept the variable to keep it concrete in relation to the OPs question.