20 namespace Lucene.Net.Util
47 public static sbyte FloatToByte(
float f,
int numMantissaBits,
int zeroExp)
51 int fzero = (63 - zeroExp) << numMantissaBits;
52 int bits = System.BitConverter.ToInt32(System.BitConverter.GetBytes(f), 0);
53 int smallfloat = bits >> (24 - numMantissaBits);
54 if (smallfloat < fzero)
56 return (bits <= 0)?(sbyte) 0:(sbyte) 1;
58 else if (smallfloat >= fzero + 0x100)
64 return (sbyte) (smallfloat - fzero);
69 public static float ByteToFloat(byte b,
int numMantissaBits,
int zeroExp)
75 int bits = (b & 0xff) << (24 - numMantissaBits);
76 bits += ((63 - zeroExp) << 24);
77 return BitConverter.ToSingle(BitConverter.GetBytes(bits), 0);
92 public static sbyte FloatToByte315(
float f)
94 int bits = System.BitConverter.ToInt32(System.BitConverter.GetBytes(f), 0);
95 int smallfloat = bits >> (24 - 3);
96 if (smallfloat < (63 - 15) << 3)
98 return (bits <= 0)?(sbyte) 0:(sbyte) 1;
100 if (smallfloat >= ((63 - 15) << 3) + 0x100)
104 return (sbyte) (smallfloat - ((63 - 15) << 3));
108 public static float Byte315ToFloat(byte b)
114 int bits = (b & 0xff) << (24 - 3);
115 bits += ((63 - 15) << 24);
116 return BitConverter.ToSingle(BitConverter.GetBytes(bits), 0);
125 public static sbyte FloatToByte52(
float f)
127 int bits = System.BitConverter.ToInt32(System.BitConverter.GetBytes(f), 0);
128 int smallfloat = bits >> (24 - 5);
129 if (smallfloat < (63 - 2) << 5)
131 return (bits <= 0)?(sbyte) 0:(sbyte) 1;
133 if (smallfloat >= ((63 - 2) << 5) + 0x100)
137 return (sbyte) (smallfloat - ((63 - 2) << 5));
141 public static float Byte52ToFloat(byte b)
147 int bits = (b & 0xff) << (24 - 5);
148 bits += ((63 - 2) << 24);
149 return BitConverter.ToSingle(BitConverter.GetBytes(bits), 0);