cmath 中 sqrt、sin、cos、pow 等的定义
是否有诸如 sqrt()
、sin()
、cos()
、tan()
等函数的定义、log()
、exp()
(这些来自 math.h/cmath)可用吗?
我只是想知道它们是如何工作的。
Are there any definitions of functions like sqrt()
, sin()
, cos()
, tan()
, log()
, exp()
(these from math.h/cmath) available ?
I just wanted to know how do they work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个有趣的问题,但是除非您碰巧知道所使用的方法,否则阅读高效库的源代码不会让您走得太远。
这里有一些指导可以帮助您理解经典方法。我的信息绝对不准确。下面的方法只是经典的方法,具体实现可以使用其他方法。
sincos
函数。atan2
是通过调用sincos
和一些逻辑来计算的。这些函数是复杂算术的构建块。This is an interesting question, but reading sources of efficient libraries won't get you very far unless you happen to know the method used.
Here are some pointers to help you understand the classical methods. My information is by no means accurate. The following methods are only the classical ones, particular implementations can use other methods.
sincos
function.atan2
is computed with a call tosincos
and a little logic. These functions are the building blocks for complex arithmetic.每个实现可能有所不同,但您可以从 glibc(GNU C 库)源代码中查看一种实现。
编辑:Google 代码搜索已离线,因此我的旧链接无处可去。
glibc 数学库的源代码位于此处:
http://sourceware.org/git/?p=glibc.git;a=tree;f=math;h=3d5233a292f12cd9e9b9c67c3a114c64564d72ab;hb=HEAD
Every implementation may be different, but you can check out one implementation from glibc's (the GNU C library) source code.
edit: Google Code Search has been taken offline, so the old link I had goes nowhere.
The sources for glibc's math library are located here:
http://sourceware.org/git/?p=glibc.git;a=tree;f=math;h=3d5233a292f12cd9e9b9c67c3a114c64564d72ab;hb=HEAD
看看
glibc
< /a> 实现各种数学函数,充满魔力、近似和汇编。Have a look at how
glibc
implements various math functions, full of magic, approximation and assembly.一定要看看 fdlibm 源代码。它们很好,因为 fdlibm 库是独立的,每个函数都有详细的文档记录,并对所涉及的数学进行了详细的解释,并且代码非常清晰易读。
Definitely take a look at the fdlibm sources. They're nice because the fdlibm library is self-contained, each function is well-documented with detailed explanations of the mathematics involved, and the code is immensely clear to read.
看了很多数学代码后,我建议不要看 glibc - 这些代码通常很难理解,并且很大程度上依赖于 glibc 的魔力。如果不知何故,FreeBSD 中的数学库 更容易阅读有时会慢一些(但不会慢很多)。
对于复杂函数,主要困难是边界情况 - 正确的 nan/inf/0 处理对于实际函数来说已经很困难,但对于复杂函数来说则是一场噩梦。 C99标准定义了很多corner case,有些函数很容易就有10-20个corner case。您可以查看最新C99的附件G标准文档以获得一个想法。 long double 也有一个困难,因为它的格式不是标准化的 - 根据我的经验,long double 应该会出现很多错误。希望即将推出的具有扩展精度的 IEEE754 修订版能够改善这种情况。
Having looked a lot at math code, I would advise against looking at glibc - the code is often quite difficult to follow, and depends a lot on glibc magic. The math lib in FreeBSD is much easier to read, if somehow sometimes slower (but not by much).
For complex functions, the main difficulty is border cases - correct nan/inf/0 handling is already difficult for real functions, but it is a nightmare for complex functions. C99 standard defines many corner cases, some functions have easily 10-20 corner cases. You can look at the annex G of the up to date C99 standard document to get an idea. There is also a difficult with long double, because its format is not standardized - in my experience, you should expect quite a few bugs with long double. Hopefully, the upcoming revised version of IEEE754 with extended precision will improve the situation.
大多数现代硬件都包含浮点单元,可以非常有效地实现这些功能。
Most modern hardware include floating point units that implement these functions very efficiently.
用法: root(number,root,深度)
示例: root(16,2) == sqrt(16) == 4示例: root(16,2,2) == sqrt(sqrt(16)) == 2
示例:root(64,3) == 4
C#实现:
用法:Sqrt(Number,深度)
示例:Sqrt(16) == 4
示例:Sqrt(8,2) == sqrt(sqrt(8))
作者:Imk0tter
Usage: root(number,root,depth)
Example: root(16,2) == sqrt(16) == 4
Example: root(16,2,2) == sqrt(sqrt(16)) == 2
Example: root(64,3) == 4
Implementation in C#:
Usage: Sqrt(Number,depth)
Example: Sqrt(16) == 4
Example: Sqrt(8,2) == sqrt(sqrt(8))
By: Imk0tter