Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Math functions?

What include statement do I need to access the math functions in this C code?

unsigned int fibonacci_closed(unsigned int n) {
 double term_number = (double) n;
 double golden_ratio = (1 + sqrt(5)) / 2;
 double numerator = pow(golden_ratio, term_number);
 return round(numerator/sqrt(5));
}

I tried #include <math.h> but that didn't seem to do it.

I'm using Visual Studio 2010 (Windows 7). This is the error:

1>ClCompile:
1>  fibonacci_closed.c
1>c:\users\odp\documents\visual studio 2010\projects\fibonacci\fibonacci\fibonacci_closed.c(7): warning C4013: 'round' undefined; assuming extern returning int
1>fibonacci_closed.obj : error LNK2019: unresolved external symbol _round referenced in function _fibonacci_closed
like image 243
Nick Heiner Avatar asked Dec 02 '22 06:12

Nick Heiner


2 Answers

Round() does not exist in math.h for the Windows libraries. Define:

static inline double round(double val)
{    
    return floor(val + 0.5);
}

UPDATE: in response to your comment, sqrt() is defined in math.h

like image 189
Mitch Wheat Avatar answered Dec 22 '22 09:12

Mitch Wheat


Round was added to C in C99 standards which is not supported by your compiler.

like image 32
Soufiane Hassou Avatar answered Dec 22 '22 08:12

Soufiane Hassou