math.h

sqrt() function not working with variable arguments [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-26 14:37:08
问题 This question already has an answer here: Why am I getting “undefined reference to sqrt” error even though I include math.h header? [duplicate] 5 answers I don't know if I'm missing something obvious, but it appears that I'm unable to compute square roots of a variable in C; the sqrt() function only seems to work on constants. This is my code: #include <math.h> #include <stdio.h> int main() { double a = 2.0; double b = sqrt(a); printf("%f", b); return 0; } When I run this program, I get the

log(10.0) can compile but log(0.0) cannot with undefined reference?

醉酒当歌 提交于 2019-11-26 14:18:33
问题 For the following C source code: #include <math.h> int main(void) { double x; x = log(0.0); return 0; } When I compile with gcc -lm , I got: /tmp/ccxxANVH.o: In function `main': a.c:(.text+0xd): undefined reference to `log' collect2: error: ld returned 1 exit status But, if I replace log(0.0) with log(10.0) , then it can compile successfully. I don't quite understand this, since no matter they make mathematical sense or not, they should compile -- there is no syntax error. Could anyone

Why do you have to link the math library in C?

时间秒杀一切 提交于 2019-11-26 08:35:29
问题 If I include <stdlib.h> or <stdio.h> in a C program I don\'t have to link these when compiling but I do have to link to <math.h> , using -lm with gcc, for example: gcc test.c -o test -lm What is the reason for this? Why do I have to explicitly link the math library but not the other libraries? 回答1: The functions in stdlib.h and stdio.h have implementations in libc.so (or libc.a for static linking), which is linked into your executable by default (as if -lc were specified). GCC can be

What is the difference between atan and atan2 in C++?

为君一笑 提交于 2019-11-26 04:07:25
问题 What is the difference between atan and atan2 in C++? 回答1: std::atan2 allows calculating the arctangent of all four quadrants. std::atan only allows calculating from quadrants 1 and 4. 回答2: From school mathematics we know that the tangent has the definition tan(α) = sin(α) / cos(α) and we differentiate between four quadrants based on the angle that we supply to the functions. The sign of the sin , cos and tan have the following relationship (where we neglect the exact multiples of π/2 ):

Why does pow(n,2) return 24 when n=5, with my compiler and OS?

被刻印的时光 ゝ 提交于 2019-11-25 22:28:48
问题 #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int n,i,ele; n=5; ele=pow(n,2); printf(\"%d\",ele); return 0; } The output is 24 . I\'m using GNU/GCC in Code::Blocks. What is happening? I know the pow function returns a double , but 25 fits an int type so why does this code print a 24 instead of a 25 ? If n=4; n=6; n=3; n=2; the code works, but with the five it doesn\'t. 回答1: Here is what may be happening here. You should be able to confirm this by looking at your

Why do you have to link the math library in C?

夙愿已清 提交于 2019-11-25 22:00:17
问题 If I include <stdlib.h> or <stdio.h> in a C program I don\'t have to link these when compiling but I do have to link to <math.h> , using -lm with gcc, for example: gcc test.c -o test -lm What is the reason for this? Why do I have to explicitly link the math library but not the other libraries? 回答1: The functions in stdlib.h and stdio.h have implementations in libc.so (or libc.a for static linking), which is linked into your executable by default (as if -lc were specified). GCC can be