近似值从双精度到单精度的转换

Transfer of approximation from double precision to single precision

提问人:Dexter S 提问时间:9/18/2020 最后编辑:Dexter S 更新时间:9/19/2020 访问量:235

问:

我想将给定函数的双精度近似转换为单精度 C 实现(目标设备仅提供单精度 ALU)。

使用双精度生成高精度(例如.max误差0.1e-12)近似并不太复杂。我使用过 maple minimax 函数,但我也发现了一些使用双精度示例的实现。

但是,一旦将这种近似值转换为单一精度方法,当我简单地将系数转换为浮点数时,我就面临着精度损失。我的目标是一个近似值(单精度),它精确到大约 +/-5 ulp。简单地将系数转换为浮点数似乎并不能完成这项工作。我已经学会了将 pi/2 等常数拆分为四舍五入部分和误差部分,我认为有某种技巧可以转移系数(近似的核心计算通常是多项式,我想在这个问题中重点介绍它们),我还不知道。

我感谢每一个提示,关于旨在实施的转移的文件。我已经研究了一些关于浮子精度的论文,但在过去的两周里没有取得太大的进展。

提前致谢!

c 浮点 精度 floating-accuracy

评论

1赞 John Bollinger 9/18/2020
我不知道准确性的损失,但如果您更改为较低精度的表示,您肯定会面临(相对)精度的损失。如果你想要 的精度,那么你为什么会考虑去任何地方?除了存储大小之外,几乎没有什么可以推荐后者而不是前者,并且存储大小的减少必然涉及范围和/或精度的降低。doublefloat
1赞 Ian Abbott 9/18/2020
有几个链接:错误传播(来自浮点指南)和每个计算机科学家都应该知道的浮点算术(非常技术性)。
1赞 Jonathan Leffler 9/18/2020
你能量化你的“巨大的准确性损失”吗?是 1E-6,还是 1E-3,还是什么?由于 a 仅支持大约 7 位数字的精度,因此期望比 1E-6 好得多可能是不明智的(但 1E-13 和 1E-6 之间的区别是“巨大的”)。双精度计算可能会执行一些操作,以确保在单精度中适得其反。这是一个复杂的话题。float
0赞 Dexter S 9/18/2020
@IanAbbott感谢您的链接,我实际上已经研究了其中两个。在 JohnBollinger,JonathanLeffler,我将编辑我的帖子以更准确地解释我的目标
1赞 Eric Postpischil 9/19/2020
显示您的代码,或者至少是伪代码?你是如何计算多项式的?你得到的准确性/误差是多少?

答:

2赞 njuffa 9/19/2020 #1

生成多项式极小值近似的常用方法是使用俄罗斯数学家叶夫根尼·雷梅兹 (Evgeny Remez) 于 1934 年发表的 Remez 交换算法。这是一个数值过程,通常涉及条件不佳的方程组。因此,它通常是在任意精度库的帮助下实现的。例如,在我使用的 Remez 算法的实现中,我将库配置为 1024 位精度。

对于表现相当良好的函数,Remez 算法的各种变体可以找到非常接近数学最小值多项式的近似值。正如问题中所指出的,问题在于,当将生成的多项式系数移动到有限精度浮点计算时会发生什么。人们经常发现近似值的最小值特性受损,有时甚至严重受损。有两个错误来源在起作用。首先,生成的系数不能用有限精度浮点格式准确表示。其次,多项式的计算使用有限精度运算,而不是具有无限精度的数学运算。

The first problem is the easier one to address. As one can see from some quick experiments, simply rounding the coefficients to the finite-precision format doesn't accomplish the desired near minimax result. By using a finite-precision format, we basically transform from an N-dimensional continuous space to an N-dimensional discrete lattice, and to do this properly, we need to find the closest lattice points. This is a solvable but hard problem, which is usually made easier through the use of heuristics. Relevant literature:

N. Brisebarre, J.-M. Muller, and A. Tisserand, "Computing machine-efficient polynomial approximations". ACM Transactions on Mathematical Software, Vol. 32. No. 2, June 2006, pp. 236-256. (online)

Nicolas Brisebarre and Sylvain Chevillard, "Efficient polynomial L-approximations", In 18th IEEE Symposium on Computer Arithmetic, June 2007, pp. 169-176 (online)

Florent de Dinechin and Christoph Lauter, "Optimizing polynomials for floating-point implementation", ArXiv preprint 2008 (online)

The Sollya tool uses these techniques from the literature for its command. Worth checking out in addition to Maple's and Mathematica's facilities for generating minimax polynomial approximations, as it often results in superior approximations in my experience.fpminimax

The second problem, how to account for evaluation with finite-precision floating-point computation and how to adjust the coefficients of a polynomial approximation accordingly, are still subject to research. Some initial results:

Tor Myklebust, "Computing accurate Horner form approximations to special functions in finite precision arithmetic", ArXiv manuscript 2015 (online)

Denis Arzelier, Florent Bréhard, Mioara Joldes, "Exchange algorithm for evaluation and approximation error-optimized polynomials", In 26th IEEE Symposium on Computer Arithmetic, June 2019, pp. 30-37 (online)

Note that the first publication came about due to a question I asked on Stackoverflow.

For my own use I am using a heuristic search for finding approximations optimized to account for both representational error in the coefficients and evaluation error in the polynomial evaluation. it can be loosely described as a form of simulated annealing. I have also checked into the use of genetic programming, but the preliminary results did not look promising, so I stopped pursuing this approach.

评论

0赞 Dexter S 9/19/2020
Thanks for your answer and your detailed description. I will definetely check these papers within the next days. I assume that “your own“ tools arent published or free available.... I will test sollya fpminimax to generate an approximation and evaluate the results.
0赞 njuffa 9/19/2020
@DexterS My own closed-source proprietary code for generating polynomial and rational approximations has been evolving in many small steps since the mid 1980s. The software quality is horrible from a modern software engineering perspective and so is the "user interface". The results are often quite competitive, though :-)
0赞 Dexter S 9/20/2020
so if you were in my situation, and did not have your own tool: What would you recommend to get fairly accurate approximations of several functions? Would you use sollya?
0赞 njuffa 9/21/2020
@DexterS I don't know your situation. In my experience, Sollya provides an excellent starting point if one needs to create polynomial approximations. If you anticipate working on this subject matter for years to come, you may ultimately want to create your own software.
0赞 Dexter S 9/23/2020
I actually didn't manage to install and use sollya. I am not experienced with compilers and installing packages, and there is fairly no detailed manual for the installation. I might ask a new question aiming to approximate the sine function... Thank you for your help!!