开发者

Convert pre-IEEE-754 C++ floating-point numbers to/from C#

开发者 https://www.devze.com 2022-12-27 13:06 出处:网络
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.

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 thi开发者_如何学Pythons 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.


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.


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|                             |
  -------------------------------------------------
0

精彩评论

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

关注公众号