如何在 PHP 中获取浮点数的二进制表示?
有没有办法在 PHP 中获取浮点数的二进制表示形式?类似于 Java 的 Double.doubleToRawLongBits() 。
给定一个正浮点数,我想获得小于该数字的最大可表示浮点数。在 Java 中,我可以这样做:
double x = Double.longBitsToDouble(Double.doubleToRawLongBits(d) - 1);
但我在 PHP 中没有看到任何类似的东西。
Is there any way to get the binary representation of a floating point number in PHP? Something like Java's Double.doubleToRawLongBits().
Given a positive floating point number, I'd like to get the largest representable floating-point number which is less than that number. In Java, I can do it like this:
double x = Double.longBitsToDouble(Double.doubleToRawLongBits(d) - 1);
But I'm not seeing anything similar in PHP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我使用 Peter Bailey 的建议提出的解决方案。它需要 64 位版本的 PHP。我不以任何方式声称这是生产质量的,但我分享以防有人想要在它的基础上进行构建。 (事实上,在发布问题后,我最终做了一些完全不同的事情,但我把它留在这里作为智力练习。)
Here is a solution I came up with using Peter Bailey's suggestion. It requires 64-bit version of PHP. I don't claim this to be production-quality in any way, but I'm sharing in case anyone wants to build upon it. (In fact I ended up doing something different entirely after I posted the question, but I leave it here as an intellectual exercise.)
作为不是整个问题而是标题的附加答案:
如果您想查看浮点数作为二进制文件的外观:
输出:
As an additional answer not to the whole question but to the title:
If you want to see how your floats looks as a binary:
Output:
这不是完整的答案,但我知道将浮点数放入二进制文件的唯一方法是使用 pack()
This isn't a full answer, but the only way I know of to put a float into binary is with pack()
java:
php:
java:
php: