如何声明 IEEE 数学函数,如“ilogbf”;在 MSVC++6 中?
有人可以帮忙告诉我如何在 MSVC++6 中包含 IEEE 数学函数吗?我尝试了 和 ,但仍然收到以下错误:
错误 C2065:“ilogbf”:未声明的标识符
错误 C2065:“scalbnf”:未声明的标识符
Could someone please help and tell me how to include IEEE mathematical functions in MSVC++6? I tried both and , but I still get these errors:
error C2065: 'ilogbf' : undeclared identifier
error C2065: 'scalbnf' : undeclared identifier
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑 3:希望这将是我的最终编辑。我逐渐意识到我根本没有正确地回答这个问题。我将保留我的答案作为一个警示故事,因为它可能具有一定的教育价值。但我明白为什么我的赞成票为零,事实上我会赞成安迪·罗斯的答案,因为我认为他的答案更相关(尽管至少在撰写本文时不完整)。在我看来,我的错误在于稍微肤浅地采用了我为 ilogbf() 找到的 Man 定义。这是一个取浮点数对数整数部分的函数,实现起来有多难?事实证明,该函数真正涉及的是 IEEE 浮点表示形式,特别是该表示形式的指数(而不是尾数)部分。在尝试回答这个问题之前我绝对应该意识到这一点!对我来说有趣的一点是函数如何找到浮点数的指数部分,因为我认为 C 的基本规则是浮点数作为函数调用的一部分被提升为双精度数。但这当然是一个完全单独的讨论。
--- 编辑 3 结束,警示故事开始 ---
一点谷歌搜索表明这些是在某些 Unix 风格中定义的,但可能不在任何 Posix 或 ANSI 标准中,因此不随 MSVC 库提供。如果函数不在库中,则不会在 math.h 中声明它们。显然,如果编译器看不到这些外部符号的声明,它不会高兴,并且您会得到像您列出的那样的错误。
显而易见的解决方法是使用提供的数学函数创建您自己的这些函数版本。例如
编辑:这不太正确。显然,这个函数应该使用以 2 为底的对数,而不是自然对数,这样返回的值实际上是一个二进制指数。它还应该采用其参数的绝对值,以便它也适用于负数。如果你在评论中问我,我将制定一个改进的版本,否则我很想把它作为读者的练习:-)
我的答案的本质,即 ANSI C 不需要这个函数并且MSVC 不包含它,显然是正确的。
编辑2:好的,我已经削弱并在没有被要求的情况下提供了改进的版本。这里是;
Edit 3: Hopefully this will be my final edit. I have come to realize that I haven't properly addressed this question at all. I am going to leave my answer in place as a cautionary tale, and because it may have some educational value. But I understand why I have zero upvotes, and in fact I am going to upvote Andy Ross' answer because I think his is much more relevant (although incomplete at least at the time of writing). It seems to me my mistake was to take the Man definitions I found for ilogbf() a little superficially. It's a function that takes the integer part of the log of a float, how hard can that be to implement ? It turns out what the function is really about is IEEE floating point representation, in particular the exponent (as opposed to the mantissa) part of that representation. I should definitely have realized that before attempting to answer the question! An interesting point to me is how a function can possibly find the exponent part of a float, as I thought a fundamental rule of C is that floats are promoted to doubles as part of a function call. But that's a whole separate discussion of course.
--- End of edit 3, start of cautionary tale ---
A little googling suggests these are defined in some flavors of Unix, but maybe are not in any Posix or ANSI standard and so not provided with the MSVC libraries. If the functions aren't in the library they won't be declared in math.h. Obviously if the compiler can't see declarations for these external symbols it won't be happy and you'll get errors like the ones you list.
The obvious work around is to create your own versions of these functions, using math functions that are provided. eg
Edit: This isn't quite right. Apparently, this function should use log to the base 2, rather than natural logs, so that the returned value is actually a binary exponent. It should also take the absolute value of its parameter, so that it will work for negative numbers as well. I will work up an improved version, if you ask me in a comment, otherwise I'm tempted to leave that as an exercise for the reader :-)
The essence of my answer, i.e. that ANSI C doesn't require this function and that MSVC doesn't include it, is apparently correct.
Edit 2: Okay I've weakened and provided an improved version without being asked. Here it is;
这些是 C99 函数,而不是 IEEE754-1985。微软似乎已经决定他们的市场并不关心C99支持,所以他们没有费心去提供它们。这是一种耻辱,但除非更多人(开发人员)抱怨,否则没有理由期望情况会改变。
全新的 754 标准 IEEE754-2008 需要这些函数(第 5.3.3 条,“logBFormat 操作”),但该版本的标准在几年内不会被广泛采用;即使它确实得到了广泛采用,微软在 C99 中已经有十年了,他们认为不适合提供这些功能,那么为什么他们会因为它们在 IEEE754 标准中而费心提供它们呢?
编辑:请注意,scalab 和 logb 在 IEEE754-1985 附录“推荐函数和谓词”中定义,但所述附录明确“不是”所述标准的一部分。
These are C99 functions, not IEEE754-1985. Microsoft seems to have decided that their market doesn't care about C99 support, so they haven't bothered to provide them. This is a shame, but unless more of you (developers) complain, there's no reason to expect that the situation will change.
The brand new 754 standard, IEEE754-2008, requires these functions (Clause 5.3.3, "logBFormat operations"), but that version of the standard won't be widely adopted for several more years; even if it does reach wide adoption, Microsoft hasn't seen fit to provide these functions for the ten years they've been in C99 so why would they bother to provide them just because they're in the IEEE754 standard?
edit: note that scalb and logb are defined in the IEEE754-1985 Appendix "Recommended Functions and Predicates", but said appendix is explicitly "not a part of" said standard.
如果您知道自己使用的是 IEEE 系统(现在确实如此),则不需要这些函数:只需通过将 double 与 uint64_t 联合来直接检查这些位即可。想必您使用这些函数首先是为了提高效率(否则您将使用更自然的操作,例如
log()
或exp()
),因此,花一点精力将代码与浮点表示相匹配可能是值得的。If you know you're on an IEEE system (and these days, you do), these functions aren't needed: just inspect the bits directly by unioning the double with a uint64_t. Presumably you're using these functions in the interest of efficiency in the first place (otherwise you'd be using more natural operations like
log()
orexp()
), so spending a little effort on matching your code to the floating point representation is probably worthwhile.