math.h

When do I use fabs and when is it sufficient to use std::abs?

核能气质少年 提交于 2019-11-27 12:31:55
I assume that abs and fabs are behaving different when using math.h . But when I use just cmath and std::abs , do I have to use std::fabs or fabs ? Or isn't this defined? In C++, it's always sufficient to use std::abs ; it's overloaded for all the numerical types. In C, abs only works on integers, and you need fabs for floating point values. These are available in C++ (along with all of the C library), but there's no need to use them. It's still okay to use fabs for double and float arguments. I prefer this because it ensures that if I accidentally strip the std:: off the abs , that the

c math linker problems on Ubuntu 11.10

跟風遠走 提交于 2019-11-27 12:07:48
Some strange error appeared after I upgraded my Ubuntu from (10.11, 11.04 i dont know) to 11.10. I am getting an undefined reference to 'sqrt' while using math.h and linking with -lm I'm compiling with gcc -Wall -Werror -g -Iinclude/ -lm lib/matrix.c src/analyse.c -o bin/analyse.o both source-files use and include math.h. This code compiled without problems for and I didn't change much since the upgrade but now it won't work. Do you have any suggestions what I can do, to find the error? I'm sorry, if this question was asked before; there are so many posts on math linker errors and I didn't

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

不打扰是莪最后的温柔 提交于 2019-11-27 09:17:18
This question already has an answer here: Why am I getting “undefined reference to sqrt” error even though I include math.h header? [duplicate] 6 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 following error: gcc -Wall -o "test2" "test2.c" (in directory: /home/eddy/Code/euler) /tmp/ccVfxkNh.o:

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

梦想与她 提交于 2019-11-27 08:48:31
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 explain this? Just in case, my gcc -v output: Configured with: ../src/configure -v --with-pkgversion='Ubuntu

finding cube root in C++?

☆樱花仙子☆ 提交于 2019-11-27 03:22:09
问题 Strange things happen when i try to find the cube root of a number. The following code returns me undefined. In cmd : -1.#IND cout<<pow(( double )(20.0*(-3.2) + 30.0),( double )1/3) While this one works perfectly fine. In cmd : 4.93242414866094 cout<<pow(( double )(20.0*4.5 + 30.0),( double )1/3) From mathematical way it must work since we can have the cube root from a negative number. Pow is from Visual C++ 2010 math.h library. Any ideas? 回答1: pow(x, y) from <cmath> does NOT work if x is

SIMD math libraries for SSE and AVX

邮差的信 提交于 2019-11-27 02:13:07
问题 I am looking for SIMD math libraries (preferably open source) for SSE and AVX. I mean for example if I have a AVX register v with 8 float values I want sin(v) to return the sin of all eight values at once. AMD has a propreitery library, LibM http://developer.amd.com/tools/cpu-development/libm/ which has some SIMD math functions but LibM only uses AVX if it detects FMA4 which Intel CPUs don't have. Also I'm not sure it fully uses AVX as all the function names end in s4 (d2) and not s8 (d4). It

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

余生颓废 提交于 2019-11-26 23:02:53
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? ephemient 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 instructed to avoid this automatic link with the -nostdlib or -nodefaultlibs options. The math functions in

Using pow() function throws undefined reference error in C

99封情书 提交于 2019-11-26 22:38:39
Why does the following bit of code work in C: int res = pow(2, 3); printf("%d\n", res); while this other doesn't? int a = 2; int b = 3; int res = pow(a, b); printf("%d\n", res); Even if I try double a = 2; double b = 3; double res = pow(a, b); printf("%f\n", res); I get an undefined reference to `pow' What am I doing wrong? When it works, it's because the calculation was done by the compiler itself (and included in the binary as if you wrote it out) printf("8\n"); When it doesn't work, is because the pow function is included in the math library and the math library isn't linked with your

When do I use fabs and when is it sufficient to use std::abs?

我是研究僧i 提交于 2019-11-26 15:50:02
问题 I assume that abs and fabs are behaving different when using math.h . But when I use just cmath and std::abs , do I have to use std::fabs or fabs ? Or isn't this defined? 回答1: In C++, it's always sufficient to use std::abs ; it's overloaded for all the numerical types. In C, abs only works on integers, and you need fabs for floating point values. These are available in C++ (along with all of the C library), but there's no need to use them. 回答2: It's still okay to use fabs for double and float

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

给你一囗甜甜゛ 提交于 2019-11-26 15:46:34
What is the difference between atan and atan2 in C++? Chris Jester-Young std::atan2 allows calculating the arctangent of all four quadrants. std::atan only allows calculating from quadrants 1 and 4. Mehrwolf 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 ): Quadrant Angle sin cos tan ------------------------------------------------- I 0 < α < π