Why does quot;dtoa.cquot; contain so much code?(为什么“dtoa.c包含这么多代码?)
问题描述
我将是第一个承认我对低级编程的整体知识有点稀疏的人.我了解许多核心概念,但我不经常使用它们.话虽如此,我对 dtoa.c 需要多少代码感到非常震惊.
I'll be the first to admit that my overall knowledge of low level programming is a bit sparse. I understand many of the core concepts but I do not use them on a regular basis. That being said I was absolutely astounded at how much code was needed for dtoa.c.
在过去的几个月里,我一直致力于 C# 中的 ECMAScript 实现,我一直在减慢填补引擎中的漏洞.昨晚我开始研究 15.7.4.2 部分中描述的 Number.prototype.toString#x15.7.4.2" rel="noreferrer">ECMAScript 规范 (pdf).在 9.8.1 部分,NOTE 3 提供了一个到 dtoa.c 的链接,但我正在寻找一个挑战,所以我等待查看它.以下是我想出的.
For the past couple months I have been working on an ECMAScript implementation in C# and I've been slowing filling in the holes in my engine. Last night I started working on Number.prototype.toString which is described in section 15.7.4.2 of the ECMAScript specification (pdf). In section 9.8.1, NOTE 3 offers a link to dtoa.c but I was looking for a challenge so I waited to view it. The following is what I came up with.
谁能解释一下为什么 dtoa.c 有大约 40 倍的代码?我简直无法想象 C# 会变得如此高效.
Can anyone with more experience in low level programming explain why dtoa.c has roughly 40 times as much code? I just cannot imagine C# being that much more productive.
推荐答案
dtoa.c 包含两个主要函数:dtoa() 将双精度数转换为字符串,strtod() 将字符串转换为双精度数.它还包含很多支持函数,其中大部分是针对它自己的任意精度算术实现的.dtoa.c 声名鹊起的是正确地进行这些转换,而这通常只能通过任意精度的算术来完成.它还具有在四种不同舍入模式下正确舍入转换的代码.
dtoa.c contains two main functions: dtoa(), which converts a double to string, and strtod(), which converts a string to a double. It also contains a lot of support functions, most of which are for its own implementation of arbitrary-precision arithmetic. dtoa.c's claim to fame is getting these conversions right, and that can only be done, in general, with arbitrary-precision arithmetic. It also has code to round conversions correctly in four different rounding modes.
您的代码仅尝试实现 dtoa() 的等价物,并且由于它使用浮点进行转换,因此并不总是正确的.(更新:见我的文章 http://www.explorebinary.com/quick-and-dirty-floating-point-to-decimal-conversion/ 了解详情.)
Your code only tries to implement the equivalent of dtoa(), and since it uses floating-point to do its conversions, will not always get them right. (Update: see my article http://www.exploringbinary.com/quick-and-dirty-floating-point-to-decimal-conversion/ for details.)
(我在我的博客上写了很多关于此的内容,http://www.exploringbinary.com/.我过去的七篇文章中有六篇都是关于 strtod() 转换的.通读它们以了解正确舍入转换是多么复杂.)
(I've written a lot about this on my blog, http://www.exploringbinary.com/ . Six of my last seven articles have been about strtod() conversions alone. Read through them to see how complicated it is to do correctly rounded conversions.)
这篇关于为什么“dtoa.c"包含这么多代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!