[英]Printing bits as IEEE-754 float
Is there some clever and reliable way to print series of bits as an IEEE-754 without actually using a float type?是否有一些巧妙而可靠的方法可以在不实际使用浮点类型的情况下将一系列位打印为 IEEE-754?
I have found a way to print fractions, which allows me to represent the float as aa fraction.我找到了一种打印分数的方法,它允许我将浮点数表示为一个分数。 However, I then came to realize that the exponent may range from -127 to 128 (after adjusting with bias), which may result in the multiplication mantissa * 2^128
.然而,我后来意识到指数可能在 -127 到 128 之间(在调整偏差后),这可能导致乘法mantissa * 2^128
。 The fraction method relies on representing the numerator as an integer, and I would require a really large integer to do this multiplication.分数法依赖于将分子表示为 integer,我需要一个非常大的 integer 来执行此乘法。 I mean, I could use "custom" type to represent this large value (ie https://gmplib.org/ ), but I would prefer if to avoid this.我的意思是,我可以使用“自定义”类型来表示这个大值(即https://gmplib.org/ ),但我更愿意避免这种情况。 If we multiplied by 10^x, I could simply adjust the decimal point and add some zeros, but sadly that's not the case either.如果我们乘以 10^x,我可以简单地调整小数点并添加一些零,但遗憾的是,情况也不是这样。
I have not been able to find anything that mentions any solution for this.我一直找不到任何提到任何解决方案的内容。 Probably due to the fact that googling stuff like "print from可能是因为谷歌搜索诸如“打印自
Why am I actually trying to do this?为什么我实际上要这样做?
I'm only doing this to get a better understanding of how floats (IEEE-754 in particular) work, and I find that it always help to do some practical example.我这样做只是为了更好地理解浮点数(尤其是 IEEE-754)是如何工作的,我发现做一些实际的例子总是有帮助的。 So I thought "Hey, why not try to code it?".所以我想“嘿,为什么不尝试编码呢?”。 This has no practical application (that I know of)!这没有实际应用(据我所知)!
So, almost immediately after posting this, I finally succeded in finding the resources I've been looking for.所以,几乎是在发布这篇文章之后,我终于成功地找到了我一直在寻找的资源。
https://www.ryanjuckett.com/printing-floating-point-numbers/ talks about it, and references other relevant sources.https://www.ryanjuckett.com/printing-floating-point-numbers/ 对此进行了讨论,并参考了其他相关资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.