How to use M_LN2 from Math.h
I'm trying to use Constant M_LN2 from the math.h library, but always seems to get a compiler error. Code:
#include <stdio.h>
#include <math.h>
int main(){
double x = M_LN2;
printf("%e",x);
return 0;
}
compiling with gcc on ubuntu
gcc -std=c99 lntester.c -o lntester -lm
getting output:
error: 'M_LN2' undeclared
any help in understanding why this is happening would be greatly appreciated.
As stated below, if def was not defined and using gcc and c99 was causing the problem. Below is the compilation code that solved the problem and allowed me to use c99.
gcc -std=c99 -D_GNU_SOURCE lntested.c -o lntester -lm
source to share
any help understanding why this is happening would be much appreciated.
You can open /usr/include/math.h
and try to find the definition M_LN2
. For me it is defined and certified by condition macros:
#if defined __USE_BSD || defined __USE_XOPEN
...
# define M_LN2 0.69314718055994530942 /* log_e 2 */
...
#endif
When you compile your code with the option -std=c99
is not defined nor __USE_BSD
the no __USE_XOPEN
, so all the variables that are wrapped in if define
, are not defined.
You can compile your code with -std=c99
or without option -std=gnu99
.
source to share