C:数学函数?

4

我需要在这段C代码中访问数学函数,应该使用哪个include语句?

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));
}

我试过使用 #include <math.h>,但好像没有成功。
我使用的是 Visual Studio 2010 (Windows 7)。这是错误信息:
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

2
sqrt函数肯定在math.h库中。你尝试过输入“man xxxx”查找需要的函数吗?你使用的是什么系统/编译器?问题是链接还是编译? - bmargulies
请通过显示编译器错误来解释“似乎没做到”的实际含义。并告诉我们你所在的平台是什么。 - Stefan Arentz
6
看起来像是微软恶作剧。 - bmargulies
1
这里的真正问题是你没有使用C编译器。你正在使用一个为微软定义的语言编写的编译器,它看起来很像C,但实际上并不是。=) - Stephen Canon
C语言就是C99。自标准发布以来,微软已经有10年时间了,甚至没有支持C99中最基本的更改。他们在多个场合公开表示,他们基本上不关心C语言的支持。如果您想进行C开发,则MSVC是错误的工具。 - Stephen Canon
显示剩余3条评论
2个回答

15

Round()在Windows库的math.h中不存在。请定义:

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

更新:针对您的评论,sqrt()math.h 中被定义。


3

在C99标准中添加了Round函数,但您的编译器不支持。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接