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
3
votes
2 answers

undefined reference to `log2f@GLIBC_2.27' undefined reference to `logf@GLIBC_2.27'

I'am trying to a run a sample code from pardiso website but end up with this error. I installed the lapack package from http://www.netlib.org/lapack/ gcc pardiso_sym.c -L /home/sree/ -lpardiso600-GNU800-X86-64 -llapack -lgfortran -fopenmp -lm…
SHD
  • 31
  • 1
  • 3
3
votes
1 answer

Disable exception with Infinity and NaN

I have a huge project (not built by me) that is allowed to have Infinity and NaN values. Although it's allowed, it is not desirable. I read that these values are generated by these kind of operations: 1/0 = ∞ log (0) = -∞ sqrt (-1) =…
João Paulo
  • 6,300
  • 4
  • 51
  • 80
3
votes
2 answers

sqrt() returning INF

Hi I'm trying to do some calculations with long doubles and I am getting INF from sqrt() function. Code: #include #include int main() { long double bigNUMBER; long double p1,p2,p3; long double p4; p1 =…
Murilo Vasconcelos
  • 4,677
  • 24
  • 27
3
votes
1 answer

Cannot use C libraries that include math.h with g++ 7 (Raspberry PI)

I've built GCC 7.2.0 for Raspberry PI, and installed it with prefix /usr/local/gcc-7.2.0 (using this tutorial). Whenever I try to compile a source that includes a C library that in turn includes math.h I get strange errors. Below is a minimal…
Azad Salahli
  • 876
  • 3
  • 14
  • 30
3
votes
2 answers

-lm doesnt work unless it's at the end of the command

Im currently writing a program for a uni asssessment and they have a set line to compile it, so if it doesn't work with that it won't be accepted. They command they use is gcc -Wall -ansi -lm program.c -o program.out My program will not compile…
Paloking
  • 41
  • 4
3
votes
0 answers

replace sqrt in libm.a with my own implementation

I write a new "sqrt(double)" in libtest.cpp, and compile it to a dynamic (or static) library libtest.so. The function interface is same as in math.h. Then I want to link main.o with libtest.so instead of libm.a. I am wondering how to do it with…
Feng
  • 31
  • 2
3
votes
1 answer

Pow: ambiguous call to overloaded function

I have problem , I can not find any solution. It gives the same error: Pow: ambiguous call to overloaded function #include #include int main() { int a, i, n, product, result=1; printf("enter a number\n"); …
user5085395
3
votes
1 answer

undefined reference to `roundf' - C language

I'm new to C and I keep running into the same error. I am typing the code in nano text editor and compiling it in the linux terminal. My operating system is Ubuntu 14.04. Here's an example of code that won't compile, #include
B. Standage
  • 45
  • 1
  • 6
3
votes
4 answers

pow function not working properly in Code::Blocks IDE

I have got a question from my junior and I can't fix it. Following is the code he is using in the Code::Blocks IDE just downloaded from official site of Code::Blocks. It's a hello world console project, which he just modified a little, using the…
mdsingh
  • 973
  • 2
  • 13
  • 20
3
votes
1 answer

How to use M_LN2 from Math.h

I am trying to use the Constant M_LN2 from the math.h library but always seem to get a compiler error. The code is: #include #include int main(){ double x = M_LN2; printf("%e",x); return 0; } compiling with gcc on ubuntu gcc…
CSnewb
  • 147
  • 4
  • 13
3
votes
1 answer

how does isNan( ) work?

isNan is defined like this in iOS SDK, math.h as below, #define isnan(x) \ ( sizeof(x) == sizeof(float) ? __inline_isnanf((float)(x)) \ : sizeof(x) == sizeof(double) ?…
Selvin
  • 12,333
  • 17
  • 59
  • 80
3
votes
0 answers

Math library included but error undefined reference to `log'. Can it be the casting?

I have a function with the following code: static void calculate_lifetime(uint16_t p_weight){ double param, result; param = (double) p_weight; result = log (param); } I include the math library, but when I compile I get the error: undefined…
CodePorter
  • 230
  • 1
  • 3
  • 13
3
votes
3 answers

acos() bad results with two close points in C

I am making a function that calculates the distance of two points using their latitude/longitude (in degrees not radians) and the spherical law of cosines. The problem I have is that due to rounding errors in the function acos() the results I am…
Jesús Ros
  • 480
  • 2
  • 6
  • 19
3
votes
1 answer

Failure to get next 32 bit float value using the C/C++ nextafter/nexttoward function in

Using C or C++, I want to increment over the range of all representable 32 bit floating point numbers in a loop, similarly to the way you might increment over all distinct values represented by a 32 bit integer. Something like: for(float f =…
user2287171
3
votes
2 answers

error in pow from math.h

I am stuck with this error in C and just can't figure out what's going wrong. The following query forms an integral part of my code to increment the values exponentially in successive iterations. Here is the formula that I programmed: I have…
akash_c
  • 173
  • 1
  • 2
  • 9