Questions tagged [math.h]

For questions relating to functions declared by the math.h header file of C programming language.

Most of the mathematical functions are placed in math.h header (cmath header in C++).
math.h header file has following functions:

int abs (int x);                    
long labs (long x);

double fabs (double x);
double sin (double x);
double cos (double x);
double tan (double x);
double asin (double x);
double acos (double x);
double atan (double x);
double sinh (double x);
double cosh (double x);
double tanh (double x);

double exp (double x);              ex
double log (double x);              ln x
double log10 (double x);            log x

double pow (double x,double y);     xy
double sqrt(double x);              square root of x
double fmod(double x, double y);    x mod y

double ceil (double x);
double floor(double x);
310 questions
329
votes
14 answers

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

If I include or in a C program, I don't have to link these when compiling, but I do have to link to , 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…
Nope
  • 34,682
  • 42
  • 94
  • 119
217
votes
11 answers

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

What is the difference between atan and atan2 in C++?
yesraaj
  • 46,370
  • 69
  • 194
  • 251
104
votes
4 answers

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

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?
math
  • 8,514
  • 10
  • 53
  • 61
45
votes
2 answers

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

For the following C source code: #include 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…
xuhdev
  • 8,018
  • 2
  • 41
  • 69
44
votes
4 answers

Does calculating Sqrt(x) as x * InvSqrt(x) make any sense in the Doom 3 BFG code?

I browsed through the recently released Doom 3 BFG source code, when I came upon something that does not appear to make any sense. Doom 3 wraps mathematical functions in the idMath class. Some of the functions just foward to the corresponding…
Robert Rüger
  • 851
  • 9
  • 21
43
votes
4 answers

Using pow() function throws undefined reference error in C

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,…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
36
votes
4 answers

expected unqualified-id before string constant

I'm currently writing a C++ application which implements an Oscillator in conjuction with math.h. The code I have should work fine for the application (trying to compile an object file), bu I'm getting a compiler error most likely having to do with…
DanMoore
  • 621
  • 1
  • 8
  • 14
31
votes
4 answers

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

#include #include #include 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…
exsnake
  • 1,767
  • 2
  • 23
  • 44
28
votes
2 answers

Why does math.h define pi, pi/2, 2/pi but not 2*pi?

This is a really simple question: Why are there predefined constants for pi, pi/2, pi/4, 1/pi and 2/pi but not for 2*pi? Is there a deeper reason behind it? This question is not about the whole pi vs tau debate. I am wondering if there is a…
hanno
  • 6,401
  • 8
  • 48
  • 80
22
votes
3 answers

Linking with GCC and -lm doesn't define ceil() on Ubuntu

I am currently using GCC to compile and I need to use . The problem is that it won't recognize the library. I have also tried -lm and nothing. The function I tried to use was ceil() and I get the following error: : undefined reference to…
drum
  • 5,416
  • 7
  • 57
  • 91
22
votes
5 answers

C/C++ fastest cmath log operation

I'm trying to calculate logab (and get a floating point back, not an integer). I was planning to do this as log(b)/log(a). Mathematically speaking, I can use any of the cmath log functions (base 2, e, or 10) to do this calculation; however, I will…
Nick
  • 2,821
  • 5
  • 30
  • 35
21
votes
5 answers

c math linker problems on Ubuntu 11.10

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…
Hachi
  • 3,237
  • 1
  • 21
  • 29
19
votes
1 answer

Implementation of sinpi() and cospi() using standard C math library

The function sinpi(x) computes sin(πx), and the function cospi(x) computes cos(πx), where the multiplication with π is implicit inside the functions. These functions were initially introduced into the C standard math library as an extension by Sun…
njuffa
  • 23,970
  • 4
  • 78
  • 130
18
votes
2 answers

abs 'implicit declaration...' error after including math.h

I used the abs() function and I added #include at the top of code. But I keep getting this error: hello.c:20:11: warning: implicit declaration of function 'abs' is invalid in C99 [-Wimplicit-function-declaration] int a =…
wakwakwak99
  • 321
  • 1
  • 2
  • 9
16
votes
1 answer

Does the C++ standard require `#include ` to define the `abs` overloads found in ``?

The C++ standard defines some overloaded functions in the header that are not part of the header in C (because C doesn't have overloading). Among these are float abs(float), double abs(double), long double abs(long double), and…
Nick Matteo
  • 4,453
  • 1
  • 24
  • 35
1
2 3
20 21