如何将定点数转换为浮点数?

问题描述 投票:0回答:2

使用统一。 我需要使用定点确定性地联网一些数据。如何将固定点转换回浮点数,以便将其应用于对象转换?

public class Fixed
{
    private int front;
    private int back;

    private const uint MAX_32 = 4294967295;
}

这是我上的定点数类

c# unity3d floating-point fixed-point
2个回答
0
投票

不要想太多。只需将您的实数缩放一个常数(最好是 2 或 10 的幂,具体取决于您的需要)并存储在一个整数中。要转换回来,除以相同的金额。在您的情况下,考虑将浮点值乘以 4294967296.0 并分配给

long
.


0
投票

正如@NineBerry 所说,您可以使用 Decimal 类型来表示定点数:

    decimal fixedPointNum = 123.456m; // a fixed point number
    float floatNum = Convert.ToSingle(fixedPointNum); 
    Console.WriteLine(floatNum);
© www.soinside.com 2019 - 2024. All rights reserved.