【发布时间】:2014-09-16 19:21:05
【问题描述】:
是否可以将24 bit integer 值转换为float,然后再转换回24 bit integer 而不会丢失数据?
例如,让我们考虑 8 位 int,一个字节,范围是 [-127..127](我们去掉 -128)。
public static float ToFloatSample (byte x) { return x / 127f; }
所以,如果x == -127,结果将为-1,如果x == 127,结果将为1。如果x == 64,结果将是~0.5
public static int ToIntSample (float x) { return (int) (x * 127f); }
那么现在:
int x = some_number;
float f = ToFloatSample (x);
int y = ToIntSample (f);
会一直x == y 吗?使用 8 位 int 是的,但如果我使用 24 位呢?
【问题讨论】:
-
源变量的类型是什么? “24 位值”没有任何意义。
-
表示该值有 2^24 个可能的值。例如 0..2^24 - 1.
-
为什么不在 Int32 中?看起来更合适。
标签: c# floating-point