开发者

Text to floating point: how does it work

Sa开发者_C百科y, I write down in a programming language the characters 5.4 and store it into a double variable. How exactly does a computer decide what floating point representation (mantissa and exponent) is the closest to that number?


I once described this, in a simple, easily understood way, for the value 5.2, see this SO article.


Well, the compiler or interpreter of the programming language uses an algorithm to convert from a decimal string representation to a (binary) floating point representation.

One implementation is David Gay's strtod() at http://www.netlib.org/fp/dtoa.c .

Note that while Gay's code is the basis for many implementations of strtod() and snprintf() in widely used C libraries, the code as such suffers from a number of issues and should not be used as such. But, it's instructive as a self-contained example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜