<legend id='l15KC'><style id='l15KC'><dir id='l15KC'><q id='l15KC'></q></dir></style></legend>
    <i id='l15KC'><tr id='l15KC'><dt id='l15KC'><q id='l15KC'><span id='l15KC'><b id='l15KC'><form id='l15KC'><ins id='l15KC'></ins><ul id='l15KC'></ul><sub id='l15KC'></sub></form><legend id='l15KC'></legend><bdo id='l15KC'><pre id='l15KC'><center id='l15KC'></center></pre></bdo></b><th id='l15KC'></th></span></q></dt></tr></i><div id='l15KC'><tfoot id='l15KC'></tfoot><dl id='l15KC'><fieldset id='l15KC'></fieldset></dl></div>
      <tfoot id='l15KC'></tfoot>

      <small id='l15KC'></small><noframes id='l15KC'>

        <bdo id='l15KC'></bdo><ul id='l15KC'></ul>

        如何将浮点数保存为 2 个字节?

        时间:2023-06-04

              <tbody id='w9m7L'></tbody>
            <i id='w9m7L'><tr id='w9m7L'><dt id='w9m7L'><q id='w9m7L'><span id='w9m7L'><b id='w9m7L'><form id='w9m7L'><ins id='w9m7L'></ins><ul id='w9m7L'></ul><sub id='w9m7L'></sub></form><legend id='w9m7L'></legend><bdo id='w9m7L'><pre id='w9m7L'><center id='w9m7L'></center></pre></bdo></b><th id='w9m7L'></th></span></q></dt></tr></i><div id='w9m7L'><tfoot id='w9m7L'></tfoot><dl id='w9m7L'><fieldset id='w9m7L'></fieldset></dl></div>
            <tfoot id='w9m7L'></tfoot>

            <small id='w9m7L'></small><noframes id='w9m7L'>

              <bdo id='w9m7L'></bdo><ul id='w9m7L'></ul>
                <legend id='w9m7L'><style id='w9m7L'><dir id='w9m7L'><q id='w9m7L'></q></dir></style></legend>

                1. 本文介绍了如何将浮点数保存为 2 个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  是的,我知道 IEEE-754 半精度标准,是的,我知道在该领域所做的工作.简而言之,我试图将一个简单的浮点数(如 52.11.25)保存在 2 个字节中.

                  Yes I'm aware of the IEEE-754 half-precision standard, and yes I'm aware of the work done in the field. Put very simply, I'm trying to save a simple floating point number (like 52.1, or 1.25) in just 2 bytes.

                  我在 Java 和 C# 但它们通过解码不同的数字来破坏输入值.您输入 32.1 并在编码解码后得到 32.0985.

                  I've tried some implementations in Java and in C# but they ruin the input value by decoding a different number. You feed in 32.1 and after encode-decode you get 32.0985.

                  有什么方法可以在不破坏输入值的情况下以 16 位存储浮点数?

                  Is there ANY way I can store floating point numbers in just 16-bits without ruining the input value?

                  非常感谢.

                  推荐答案

                  您可以将三位数字存储为 BCD 并使用剩余的四位作为小数点位置:

                  You could store three digits in BCD and use the remaining four bits for the decimal point position:

                  52.1 = 521 * 10 ^ -1 => 0x1521
                  1.25 = 125 * 10 ^ -2 => 0x2125
                  

                  这将为您提供从 0.0000000000000001 到 999 的范围.当然,您可以为小数点添加偏移量,例如范围为 0.0000000001 到 999000000.

                  This would give you a range from 0.0000000000000001 to 999. You can of course add an offset for the decimal point to get for example the range 0.0000000001 to 999000000.

                  四位用于小数点放置的简单实现,其余用于值.没有任何错误检查,也没有彻底检查.(使用 != 比较双精度时,某些值可能存在精度问题.)

                  Simple implementation of four bit used for decimal point placement, and the rest for the value. Without any error check, and not thoroughly checked. (May have precision issues with some values when using != to compare doubles.)

                  public static short Encode(double value) {
                    int cnt = 0;
                    while (value != Math.Floor(value)) {
                      value *= 10.0;
                      cnt++;
                    }
                    return (short)((cnt << 12) + (int)value);
                  }
                  
                  public static double Decode(short value) {
                    int cnt = value >> 12;
                    double result = value & 0xfff;
                    while (cnt > 0) {
                      result /= 10.0;
                      cnt--;
                    }
                    return result;
                  }
                  

                  例子:

                  Console.WriteLine(Encode(52.1));
                  Console.WriteLine(Decode(4617));
                  

                  输出:

                  4617
                  52.1
                  

                  这篇关于如何将浮点数保存为 2 个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:以 C# BinaryReader.ReadString 的 7 位格式编码整数 下一篇:如何直接在 C# 中执行批处理命令?

                  相关文章

                  最新文章

                  <legend id='74DyU'><style id='74DyU'><dir id='74DyU'><q id='74DyU'></q></dir></style></legend>

                    <i id='74DyU'><tr id='74DyU'><dt id='74DyU'><q id='74DyU'><span id='74DyU'><b id='74DyU'><form id='74DyU'><ins id='74DyU'></ins><ul id='74DyU'></ul><sub id='74DyU'></sub></form><legend id='74DyU'></legend><bdo id='74DyU'><pre id='74DyU'><center id='74DyU'></center></pre></bdo></b><th id='74DyU'></th></span></q></dt></tr></i><div id='74DyU'><tfoot id='74DyU'></tfoot><dl id='74DyU'><fieldset id='74DyU'></fieldset></dl></div>

                    1. <small id='74DyU'></small><noframes id='74DyU'>

                        <bdo id='74DyU'></bdo><ul id='74DyU'></ul>
                    2. <tfoot id='74DyU'></tfoot>