views:

316

answers:

4
+1  Q: 

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
A: 

that should be sufficient for compilation. on UNIX, you have to link with -lm. What is the exact error that you have?

aaa
Looks more like a comment.
bmargulies
+2  A: 

Also, anytime you're on a POSIX-y system, you can usually use the manpages system to figure out what headers are required to get function declarations in-scope. Use $ man pow, and in the synopsis should be a #include <math.h> to show you what to include.

Ben Collins
What part of "I'm using Visual Studio 2010 (Windows 7)" made you think he has manpages? :) They're great when available, but using another system's would've completely misled him here.
Roger Pate
My bad. I stopped reading after I saw "I tried to include math.h"...
Ben Collins
+5  A: 

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

Mitch Wheat
ok, that leaves `sqrt()`...
Rosarch
+3  A: 

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

Soufiane Hassou