C#双倍于真实值

C#双倍于真实值,c#,asp.net,double,pascal,type-conversion,C#,Asp.net,Double,Pascal,Type Conversion,我试图将double转换为pascal real,但当我将0.23转换为real时,我得到了0.2399999 real。如何将所有9999截断为0000 public static byte[] Double2Real48(double d) { byte[] r48 = new byte[6]; byte[] da = BitConverter.GetBytes(d); for (int i = 0; i < r48.Length; i++)

我试图将double转换为pascal real,但当我将0.23转换为real时,我得到了0.2399999 real。如何将所有9999截断为0000

public static byte[] Double2Real48(double d)
{
    byte[] r48 = new byte[6];
    byte[] da = BitConverter.GetBytes(d);

    for (int i = 0; i < r48.Length; i++)
        r48[i] = 0;

    //Copy the negative flag
    r48[5] |= (byte)(da[7] & 0x80);

    //Get the expoent
    byte b1 = (byte)(da[7] & 0x7f);
    ushort n = (ushort)(b1 << 4);
    byte b2 = (byte)(da[6] & 0xf0);
    b2 >>= 4;
    n |= b2;

    if (n == 0)
        return r48;

    byte ex = (byte)(n - 1023);
    r48[0] = (byte)(ex + 129);

    //Copy the Mantissa
    r48[5] |= (byte)((da[6] & 0x0f) << 3);//Get the last four bits
    r48[5] |= (byte)((da[5] & 0xe0) >> 5);//Get the first three bits

    r48[4] = (byte)((da[5] & 0x1f) << 3);//Get the last 5 bits
    r48[4] |= (byte)((da[4] & 0xe0) >> 5);//Get the first three bits

    r48[3] = (byte)((da[4] & 0x1f) << 3);//Get the last 5 bits
    r48[3] |= (byte)((da[3] & 0xe0) >> 5);//Get the first three bits

    r48[2] = (byte)((da[3] & 0x1f) << 3);//Get the last 5 bits
    r48[2] |= (byte)((da[2] & 0xe0) >> 5);//Get the first three bits

    r48[1] = (byte)((da[2] & 0x1f) << 3);//Get the last 5 bits
    r48[1] |= (byte)((da[1] & 0xe0) >> 5);//Get the first three bits

    return r48;

}
公共静态字节[]Double2Real48(双d)
{
字节[]r48=新字节[6];
字节[]da=位转换器.GetBytes(d);
for(int i=0;i=4;
n |=b2;
如果(n==0)
返回r48;
字节ex=(字节)(n-1023);
r48[0]=(字节)(ex+129);
//复制尾数
r48[5]|=(字节)((da[6]&0x0f)>5);//获取前三位
r48[4]=(字节)((da[5]&0x1f)>5);//获取前三位
r48[3]=(字节)((da[4]&0x1f)>5);//获取前三位
r48[2]=(字节)((da[3]&0x1f)>5);//获取前三位
r48[1]=(字节)((da[2]&0x1f)>5);//获取前三位
返回r48;
}

以下是我的转换例程。请注意,我在这里没有特别提到处理IEEE无穷大或NaN值

static byte[] DoubleToReal48(double d)
{
    byte[] r = new byte[6];

    long bits = BitConverter.DoubleToInt64Bits(d);
    bool negative = ((bits >> 63) & 1) != 0;
    long exponent = ((bits >> 52) & 0x7FF) - 1023;
    long mantissa = bits & 0xFFFFFFFFFFFFFL;

    long raw = (negative ? 1 : 0);
    raw = (raw << 39) | (mantissa >> 13);
    raw = (raw << 8) | ((exponent + 129) & 0xFF);

    for (int k = 0; k < 6; k++)
    {
        r[k] = (byte)(raw & 0xFF);
        raw >>= 8;
    }
    return r;
}

static double Real48ToDouble(byte[] r)
{
    long raw = 0;
    for (int k = 5; k >= 0; k--)
    {
        raw = (raw << 8) | r[k];
    }

    long mantissa = (raw << 5) & 0xFFFFFFFFFD000L;
    long exponent = (((raw & 0xFF) - 129 + 1023) & 0x7FF) << 52;
    long sign = (((raw & ~0x7FFFFFFFFFFFL) != 0) ? 1 : 0) << 63;

    return BitConverter.Int64BitsToDouble(sign | exponent | mantissa);
}
static byte[]DoubleToReal48(双d)
{
字节[]r=新字节[6];
长位=位转换器。双字节64位(d);
布尔负=((位>>63)和1)!=0;
长指数=((位>>52)和0x7FF)-1023;
长尾数=位&0xFFFFFFFFFFFL;
长原始=(负?1:0);
原始=(原始>13);
原始=(原始>=8;
}
返回r;
}
静态双实48ToDouble(字节[]r)
{
长原始=0;
对于(int k=5;k>=0;k--)
{

原始=(raw这是正常的舍入错误,这是意料之中的。并非所有浮点数都可以精确地用二进制表示。如果您想要更高的精度,请使用
decimal
,但这个问题仍然会发生。实际上,在这种情况下,这可能不是问题所在-我刚刚重新阅读了您的问题,并注意到这些数字并不是我所认为的首先,听起来好像你在尝试一个数字的平方,然后你用一个不正确的十进制表示替换这个数字,然后你发布了一个似乎是手动将数字加倍的方法……你想做什么?@Chrisf:0.229999将是一个正常的四舍五入问题。0.23999要么是算法问题,要么是数据输入错误。@天哪,我问了我的问题,请帮帮我