你如何打印(无的printf)的IEEE754多少? [英] How do you print out an IEEE754 number (without printf)?

查看:138
本文介绍了你如何打印(无的printf)的IEEE754多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关这个问题的目的,我做的的必须使用的printf 设施(我不能告诉你为什么,可惜的能力,但我们只是现在假设我知道我在做什么)。

For the purposes of this question, I do not have the ability to use printf facilities (I can't tell you why, unfortunately, but let's just assume for now that I know what I'm doing).

有关的IEEE754单precision号,您有以下位:

For an IEEE754 single precision number, you have the following bits:

SEEE EEEE EFFF FFFF FFFF FFFF FFFF FFFF

其中,取值为符号,电子是指数和˚F是小部分。

where S is the sign, E is the exponent and F is the fraction.

打印标志是相对容易的所有情况下,为的是抓住所有的特殊情况下,如 NaN的电子== 0xFF的,女!= 0 ),天道酬勤电子== 0xFF的,女== 0 )和 0 电子== 0,女== 0 ,视为特殊只是因为指数的偏见是不是在使用情况)。

Printing the sign is relatively easy for all cases, as is catching all the special cases like NaN (E == 0xff, F != 0), Inf (E == 0xff, F == 0) and 0 (E == 0, F == 0, considered special just because the exponent bias isn't used in that case).

我有两个问题。

首先是如何最好地把denormalised号码(如电子== 0,女!= 0 )转换成标准化的号码(如 1&LT = E< = 0xFE的)?我怀疑这将是必要的简化答案的一个问题(但我可能是错的可以随意来教育我)。

The first is how best to turn denormalised numbers (where E == 0, F != 0) into normalised numbers (where 1 <= E <= 0xfe)? I suspect this will be necessary to simplify the answer to the next question (but I could be wrong so feel free to educate me).

第二个问题是如何打印出的归一化数字。我希望能够打印出来有两种方式,像指数 -3.74195E3 和非指数如 3741.95 。虽然,只是看着那两个并排的一面,它应该是相当容易,只需移动小数点扭转前者为后者。因此,让我们把心思的指数形式。

The second question is how to print out the normalised numbers. I want to be able to print them out in two ways, exponential like -3.74195E3 and non-exponential like 3741.95. Although, just looking at those two side-by-side, it should be fairly easy to turn the former into the latter by just moving the decimal point around. So let's just concentrate on the exponential form.

我有我以前早就打印出来PI,你使用的日益减少公式之一,并保持对可能性的上限和下限,输出数字时,这两个限制答应了,移算法的模糊的记忆计算由10(所以当上限和下限分别为 3.2364 3.1234 ,您可以输出的一个因素 3 ,并在计算调整的)。

I have a vague recollection of an algorithm I used long ago for printing out PI where you used one of the ever-reducing formulae and kept an upper and lower limit on the possibilities, outputting a digit when both limits agreed, and shifting the calculation by a factor of 10 (so when the upper and lower limits were 3.2364 and 3.1234, you could output the 3 and adjust for that in the calculation).

但它是一个的的,因为我这样做,所以我甚至不知道这是在这里采取合适的进场时间。如此看来,因为每个位的值的一半,通过小数部分移动时(在previous位的 1/2 1 / 4 1/8 等)。

But it's been a long time since I did that so I don't even know if that's a suitable approach to take here. It seems so since the value of each bit is half that of the previous bit when moving through the fractional part (1/2, 1/4, 1/8 and so on).

我真的preFER的的有去通过的printf 源$ C ​​$ C跋涉,除非绝对必要的,因此,如果任何人可以帮助出这一点,我会感激不尽。

I would really prefer not to have to go trudging through printf source code unless absolutely necessary so, if anyone can help out with this, I'll be eternally grateful.

推荐答案

如果你想获得对每次转换确切的结果,你将不得不使用arbitrary- precision算法,在做的printf()实现。如果你想要得到的结果是接近,也许只有在它们的最小显著位(S)不同,那么一个很简单的双precision基于算法就足够了:对于整数部分,反复除以十追加余数形成十进制字符串(反向);为小数部分,反复十繁殖并减去关闭的整数部分以形成十进制字符串

If you want to get exact results for every conversion, you'll have to use arbitrary-precision arithmetic, as done in printf() implementations. If you want to get results that are "close," perhaps differing only in their least significant digit(s), then a very simple double-precision based algorithm will suffice: for the integer part, repeatedly divide by ten and append the remainders to form the decimal string (in reverse); for the fractional part, repeatedly multiply by ten and subtract off the integer parts to form the decimal string.

我最近写了一篇关于这种方法的文章:的http://www.exploringbinary.com/quick-and-dirty-floating-point-to-decimal-conversion/ 。它不打印科学记数法,但是这应该是微不足道的补充。该算法打印正规数(我印的那些准确的走了出来,但你必须做更全面的测试)。

I recently wrote an article about this method: http://www.exploringbinary.com/quick-and-dirty-floating-point-to-decimal-conversion/ . It does not print scientific notation, but that should be trivial to add. The algorithm prints subnormal numbers (the ones I printed came out accurately, but you'd have to do more thorough testing).

这篇关于你如何打印(无的printf)的IEEE754多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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