如何Ç计算罪()等数学函数? [英] How does C compute sin() and other math functions?

查看:112
本文介绍了如何Ç计算罪()等数学函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在通过.NET拆卸服务与海合会源$ C ​​$ C研读,但似乎无法找到任何地方的实际执行的罪()和其他的数学函数......他们似乎总是引用别的东西。

I've been poring through .NET disassemblies and the GCC source code, but can't seem to find anywhere the actual implementation of sin() and other math functions... they always seem to be referencing something else.

谁能帮我找到他们呢?我觉得这是不可能的,所有的硬件​​将c将在硬件上支持三角函数运行,所以必须有一个软件算法的地方的,对不对?

Can anyone help me find them? I feel like it's unlikely that ALL hardware that C will run on supports trig functions in hardware, so there must be a software algorithm somewhere, right?

编辑:我所知道的几种方法,功能的可以的计算,并写了我自己的例程来计算使用泰勒级数为乐趣的功能。我很好奇多么真实,生产语言做到这一点,因为我所有的实现都是幅度总是几个数量级慢,即使我想我的算法是pretty聪明(显然他们不是)。

I'm aware of several ways that functions can be calculated, and have written my own routines to compute functions using taylor series for fun. I'm curious about how real, production languages do it, since all of my implementations are always several orders of magnitude slower, even though I think my algorithms are pretty clever (obviously they're not).

推荐答案

在GNU的libm,的实施取决于系统。因此,你可以找到的执行情况,对每个平台,的 sysdeps

In GNU libm, the implementation of sin is system-dependent. Therefore you can find the implementation, for each platform, somewhere in the appropriate subdirectory of sysdeps.

一个目录包括C中的实现,贡献的IBM。自2011年10月,这是当你调用罪()一个典型的x86-64 Linux系统上实际运行code。这显然​​是比 FSIN 汇编指令更快。来源$ C ​​$ C:<一href=\"https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/ieee754/dbl-64/s_sin.c;hb=HEAD#l281\">sysdeps/ieee754/dbl-64/s_sin.c,寻找 __罪(双X)

One directory includes an implementation in C, contributed by IBM. Since October 2011, this is the code that actually runs when you call sin() on a typical x86-64 Linux system. It is apparently faster than the fsin assembly instruction. Source code: sysdeps/ieee754/dbl-64/s_sin.c, look for __sin (double x).

这code是非常复杂的。没有一个软件算法是尽可能快的,也是准确的整个范围内的 X 的值,所以库实现了许多不同的算法和它的第一份工作是看的 X 的并决定使用哪种算法。在一些地区,它使用什么样貌似熟悉的泰勒级数。几个算法首先计算一个快速的结果,那么如果这不够准确,丢弃它并依傍较慢的算法。

This code is very complex. No one software algorithm is as fast as possible and also accurate over the whole range of x values, so the library implements many different algorithms and its first job is to look at x and decide which algorithm to use. In some regions it uses what looks like the familiar Taylor series. Several of the algorithms first compute a quick result, then if that's not accurate enough, discard it and fall back on a slower algorithm.

旧的32位版本/ glibc的使用了 FSIN 指令,这对于某些输入出奇不准确的。有一个<一个href=\"http://randomascii.word$p$pss.com/2014/10/09/intel-underestimates-error-bounds-by-1-3-quintillion/\">fascinating只有2 code 行的博客文章说明这一点。

Older 32-bit versions of GCC/glibc used the fsin instruction, which is surprisingly inaccurate for some inputs. There's a fascinating blog post illustrating this with just 2 lines of code.

fdlibm的实施在纯C比的glibc的更简单,是很好的注释。来源$ C ​​$ C: fdlibm / s_sin.c 并的 fdlibm / k_sin.c

fdlibm's implementation of sin in pure C is much simpler than glibc's and is nicely commented. Source code: fdlibm/s_sin.c and fdlibm/k_sin.c

这篇关于如何Ç计算罪()等数学函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆