javascript - javascript 是使用自己的算法实现数学函数还是将它们外包给 cpu/处理器?

标签 javascript actionscript-3

ActionScript3 声明如下:

Note: The Math functions acos, asin, atan, atan2, cos, exp, log, pow, sin, and sqrt may result in slightly different values depending on the algorithms used by the CPU or operating system. Flash runtimes call on the CPU (or operating system if the CPU doesn't support floating point calculations) when performing the calculations for the listed functions, and results have shown slight variations depending upon the CPU or operating system in use.

我的 Web 应用程序处理非常大和非常小的数字,我怀疑它们可能会根据运行它的机器而有所不同(尤其是对于处理器不如台式机强大的移动设备)。我的问题是,这种行为是语言的功能吗?

也就是说,我假设某些语言为上述函数实现了它们自己的算法,因此可能更加一致(除了处理器/CPU 的选择引入的舍入误差差异外)将存在于所有语言)。

有人知道 Javascript 是否有自己的算法,或者它是否像 ActionScript 一样将它们外包给 CPU/处理器?

最佳答案

摘自 ECMAScript 5 standard , 第 15.8.2 节

NOTE The behaviour of the functions acos, asin, atan, atan2, cos, exp, log, pow, sin, sqrt, and tan is not precisely specified here except to require specific results for certain argument values that represent boundary cases of interest. For other argument values, these functions are intended to compute approximations to the results of familiar mathematical functions, but some latitude is allowed in the choice of approximation algorithms. The general intent is that an implementer should be able to use the same mathematical library for ECMAScript on a given hardware platform that is available to C programmers on that platform.

Although the choice of algorithms is left to the implementation, it is recommended (but not specified by this standard) that implementations use the approximation algorithms for IEEE 754 arithmetic contained in fdlibm, the freely distributable mathematical library from Sun Microsystems (http://www.netlib.org/fdlibm)

从本质上讲,这意味着每个实现(包括每个浏览器)都可以决定他们希望如何处理这些功能。根据建议,我假设绝大多数人使用 fdlibm,尽管检查将作为练习留给读者。

关于javascript - javascript 是使用自己的算法实现数学函数还是将它们外包给 cpu/处理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203007/

相关文章:

javascript - 使用 d3.js 堆栈布局的堆积条形图;条形部分并不总是在正确的位置

javascript - 使用 pdfmake 将文本修复到 pdf 中最后一页的底部

actionscript-3 - AS3 : Check if FLV File Exists

actionscript-3 - ActionScript 3/Flash : Basic Game Loop Stuttering Problems

c - Adobe alchemy 编译器问题 - 它生成 .l.bc 文件,如何将它们转换为 swc 文件?

javascript - 在 ramda.js 中使用嵌套字段进行排序

java - 在 Java 中解析 JavaScript 日期字符串

javascript - 许多使用 javascript 对 API 的查询,其中一些得到 403

actionscript-3 - 修复 ASDoc 中的损坏路径?

flash - AS3 : streamlining a 'universal loader'