转换 IEEE-754 之前的 C++与 C# 之间的浮点数

发布于 2024-08-30 06:31:17 字数 409 浏览 1 评论 0原文

在 .Net 之前、在数学协处理器之前、在 IEEE-574 之前,Microsoft 定义了浮点数的位模式。旧版本的 C++ 编译器很乐意使用该定义。

我正在编写一个 C# 应用程序,需要在文件中读取/写入此类浮点数。如何进行 2 位格式之间的转换?我需要两个方向的转换方法。

该应用程序将在 PocketPC/WinCE 环境中运行。

更改文件的结构超出了该项目的范围。

是否有 C++ 编译器选项指示它使用旧的 FP 格式?那将是理想的。然后,我可以使用以 null 结尾的文本字符串在 C# 代码和 C++ 代码之间交换数据,并且 C++ 方法将是 sprintf 和 atof 函数的简单包装器。

至少,我希望有人可以回复旧 FP 格式的位定义,这样我就可以在必要时组合一个低级位操作算法。

谢谢。

Before .Net, before math coprocessors, before IEEE-574, Microsoft defined a bit pattern for floating-point numbers. Old versions of the C++ compiler happily used that definition.

I am writing a C# app that needs to read/write such floating-point numbers in a file. How can I do the conversions between the 2 bit formats? I need conversion methods in both directions.

This app is going to run in a PocketPC/WinCE environment.

Changing the structure of the file is out-of-scope for this project.

Is there a C++ compiler option that instructs it to use the old FP format? That would be ideal. I could then exchange data between the C# code and C++ code by using a null-terminated text string, and the C++ methods would be simple wrappers around sprintf and atof functions.

At the very least, I'm hoping someone can reply with the bit definitions for the old FP format, so I can put together a low-level bit manipulation algorithm if necessary.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-09-06 06:31:17

我按照 Johannes Rössel 的 Wikipedia 链接 中的面包屑找到了一个不应该的 Python 实现太难翻译:http://groups.google .com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe

以下是该链接中 Bengt Richter 的格式文档:

根据旧的 MASM 5.0 程序员指南,有一种 Microsoft 二进制格式
用于编码实数,包括短实数(32 位)和长实数(64 位)。

共有 3 个部分:

  1. 最高字节中的有偏差 8 位指数(我们一直在使用的小端视图中的最后一个)
    它说短数字的偏差是 0x81,长数字的偏差是 0x401,但我不确定它在哪里。
    我只是通过实验到达那里。

  2. 第二高字节的高位中的符号位(0 表示+,1 表示-)。

  3. 第二高字节剩余7位中除尾数第一设定位外的所有,
    和其余字节。由于非零数的最高有效位是 1,因此
    没有代表。但如果是的话,它将共享符号所在的相同位位置
    (这就是为什么我在那里进行或运算以完成实际的尾数)。

MASM 还支持类似于 IEEE 的 10 字节格式。我在该部分没有看到任何内容
关于 NaN 和 INF。

I followed the bread crumbs from Johannes Rössel's Wikipedia link and found a Python implementation that shouldn't be too hard to translate: http://groups.google.com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe

Here's the documentation of the format from Bengt Richter in that link:

According to an old MASM 5.0 programmer's guide, there was a Microsoft Binary format
for encoding real numbers, both short (32 bits) and long (64 bits).

There were 3 parts:

  1. Biased 8-bit exponent in the highest byte (last in the little-endian view we've been using)
    It says the bias is 0x81 for short numbers and 0x401 for long, but I'm not sure where that lines up.
    I just got there by experimentation.

  2. Sign bit (0 for +, 1 for -) in upper bit of second highest byte.

  3. All except the first set bit of the mantissa in the remaining 7 bits of the second highest byte,
    and the rest of the bytes. And since the most signficant bit for non-zero numbers is 1, it
    is not represented. But if if were, it would share the same bit position where the sign is
    (that's why I or-ed it in there to complete the actual mantissa).

MASM also supported a 10-byte format similar to IEEE. I didn't see anything in that section
on NaNs and INFs.

命硬 2024-09-06 06:31:17

根据Johannes的回答,您可以访问http://support.microsoft.com/kb/140520 下载 .dll 从 mbf 到 IEEE 转换的源代码。

编辑:实际上,这对你没有帮助。但实际的 MBF 格式记录如下: http://support.microsoft.com/kb/35826:

  -------------------------------------------------
 |              |    |                             |
 |8 Bit Exponent|Sign|   55 Bit Mantissa           |
 |              | Bit|                             |
  -------------------------------------------------

Based on Johannes's answer, you can go to http://support.microsoft.com/kb/140520 to download the source code for a conversion .dll from mbf to IEEE.

EDIT: Actually, that isn't helpful for you. But the actual MBF format is documented here: http://support.microsoft.com/kb/35826:

  -------------------------------------------------
 |              |    |                             |
 |8 Bit Exponent|Sign|   55 Bit Mantissa           |
 |              | Bit|                             |
  -------------------------------------------------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文