4

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
bmargulies
  • 97,814
  • 39
  • 186
  • 310
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
  • 2
    sqrt is certainly in math.h. Have you tried typing 'man xxxx' for the functions you need? What system/compiler are you using? Is the problem linking or compiling. – bmargulies Jan 31 '10 at 02:54
  • Explain what "didn't seem to do it" actually means by showing a compiler error. And tell us what platform you are on. – Stefan Arentz Jan 31 '10 at 02:55
  • OK, you still haven't told us the exact error. – bmargulies Jan 31 '10 at 02:56
  • 6
    Looks like a Microsoft prank. – bmargulies Jan 31 '10 at 03:03
  • 1
    The real problem here is that you're not using a C compiler. You're using a compiler for a Microsoft-defined language that looks similar to C, but isn't. =) – Stephen Canon Jan 31 '10 at 03:31
  • @Stephen: MCVC just doesn't support C99; the rest is pretty much there. – Joey Jan 31 '10 at 11:54
  • C *is* C99. Microsoft has had 10 years since the standard was published, and haven't bothered to support even the most basic changes in C99. They have publicly stated at several times that they essentially don't care about C language support. MSVC is the wrong tool to use if you want to do C development. – Stephen Canon Jan 31 '10 at 19:02
  • @Stephen: what other tools would you recommend using? Initially I wanted to use Eclipse, but I don't want to manually compile, and I'd prefer to use a visual debugger instead of a text based one. – Nick Heiner Feb 01 '10 at 18:08

2 Answers2

15

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
  • 295,962
  • 43
  • 465
  • 541
3

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

Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75