lucene3.5之SmallFloat

阅读更多
package org.apache.lucene.util;
/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/** Floating point numbers smaller than 32 bits.
 *
 * @lucene.internal
 */
public class SmallFloat {

  /** Converts a 32 bit float to an 8 bit float.
   * 
Values less than zero are all mapped to zero. *
Values are truncated (rounded down) to the nearest 8 bit value. *
Values between zero and the smallest representable value * are rounded up. * * @param f the 32 bit float to be converted to an 8 bit float (byte) * @param numMantissaBits the number of mantissa bits to use in the byte, with the remainder to be used in the exponent * @param zeroExp the zero-point in the range of exponent values * @return the 8 bit float representation */ public static byte floatToByte(float f, int numMantissaBits, int zeroExp) { // Adjustment from a float zero exponent to our zero exponent, // shifted over to our exponent position. int fzero = (63-zeroExp)<> (24-numMantissaBits); if (smallfloat <= fzero) { return (bits<=0) ? (byte)0 // negative numbers and zero both map to 0 byte :(byte)1; // underflow is mapped to smallest non-zero number. } else if (smallfloat >= fzero + 0x100) { return -1; // overflow maps to largest number } else { return (byte)(smallfloat - fzero); } } /** Converts an 8 bit float to a 32 bit float. */ public static float byteToFloat(byte b, int numMantissaBits, int zeroExp) { // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup // is only a little bit faster (anywhere from 0% to 7%) if (b == 0) return 0.0f; int bits = (b&0xff) << (24-numMantissaBits); bits += (63-zeroExp) << 24; return Float.intBitsToFloat(bits); } // // Some specializations of the generic functions follow. // The generic functions are just as fast with current (1.5) // -server JVMs, but still slower with client JVMs. // /** floatToByte(b, mantissaBits=3, zeroExponent=15) *
smallest non-zero value = 5.820766E-10 *
largest value = 7.5161928E9 *
epsilon = 0.125 */ public static byte floatToByte315(float f) { int bits = Float.floatToRawIntBits(f); int smallfloat = bits >> (24-3); if (smallfloat <= ((63-15)<<3)) { return (bits<=0) ? (byte)0 : (byte)1; } if (smallfloat >= ((63-15)<<3) + 0x100) { return -1; } return (byte)(smallfloat - ((63-15)<<3)); } /** byteToFloat(b, mantissaBits=3, zeroExponent=15) */ public static float byte315ToFloat(byte b) { // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup // is only a little bit faster (anywhere from 0% to 7%) if (b == 0) return 0.0f; int bits = (b&0xff) << (24-3); bits += (63-15) << 24; return Float.intBitsToFloat(bits); } /** floatToByte(b, mantissaBits=5, zeroExponent=2) *
smallest nonzero value = 0.033203125 *
largest value = 1984.0 *
epsilon = 0.03125 */ public static byte floatToByte52(float f) { int bits = Float.floatToRawIntBits(f); int smallfloat = bits >> (24-5); if (smallfloat <= (63-2)<<5) { return (bits<=0) ? (byte)0 : (byte)1; } if (smallfloat >= ((63-2)<<5) + 0x100) { return -1; } return (byte)(smallfloat - ((63-2)<<5)); } /** byteToFloat(b, mantissaBits=5, zeroExponent=2) */ public static float byte52ToFloat(byte b) { // on Java1.5 & 1.6 JVMs, prebuilding a decoding array and doing a lookup // is only a little bit faster (anywhere from 0% to 7%) if (b == 0) return 0.0f; int bits = (b&0xff) << (24-5); bits += (63-2) << 24; return Float.intBitsToFloat(bits); } }

这个类的功能是:

1、对于小于0的置为0

2、对于0与小的可表示值之间的值向上舍入

3、其余的向下舍入到8位值

首先,我们看看什么是符点数

浮点数是属于有理数中某特定子集的数的数字表示,在计算机中用以近似表示任意某个实数。具体的说,这个实数由一个整数或定点数(即尾数)乘以某个基数(计算机中通常是2)的整数次幂得到,这种表示方法类似于基数为10的科学记数法。

然后,我们来看一下IEEE754标准是如何描述符点数

IEEE754标准

  IEEE754代码
  标准表示法
  为便于 软件的移植,浮点数的表示格式应该有统一标准(定义)。1985年IEEE(Institute of Electrical and Electronics Engineers)提出了IEEE754标准。该标准规定基数为2,阶码E用移码表示,尾数M用原码表示,根据二进制的规格化方法,最高数字位总是1, 该标准将这个1缺省存储,使得尾数表示范围比实际存储的多一位。实数 的IEEE754标准的浮点数格式为:
  具体有三种形式:
  IEEE754三种浮点数的格式参数
  
类型 存储位数


偏移值

数符(s) 阶码(E) 尾数(M) 总位数 十六进制 十进制
短实数(Single,Float) 1位 8位 23位 32位 0x7FH +127
长实数(Double) 1位 11 位 52位 64位 0x3FFH +1023
临时实数(延伸双精确度,不常用) 1位 15位 64位 80位 0x3FFFH +16383
  对于阶码为0或为255(2047)的情况,IEEE有特殊的规定:
  如果 E 是0 并且 M 是0,这个数±0(和符号位相关) 如果 E = 2 − 1 并且 M 是0,这个数是 ±无穷大(同样和符号位相关) 如果 E = 2 − 1 并且 M 非0,这个数表示为不是一个数(NaN)。
  标准浮点数的存储在尾数中隐含存储着一个1,因此在计算尾数的真值时比一般形式要多一个整数1。对于阶码E的存储形式因为是127的偏移,所以在计算其移码时与人们熟悉的128偏移不一样,正数的值比用128偏移求得的少1,负数的值多1,为避免计算错误,方便理解,常将E当成二进制真值进行存储。例如:将数值-0.5按IEEE754单精度格式存储,先将-0.5换成二进制并写成标准形式:-0.5(10进制)=-0.1(2进制)=-1.0×2-1(2进制,-1是指数),这里s=1,M为全0,E-127=-1,E=126(10进制)=01111110(2进制),则存储形式为:
  1 01111110 000000000000000000000000=BF000000(16进制) 
 
  public static byte floatToByte(float f, int numMantissaBits, int zeroExp)完成将float转化为byte的任务,可以按指定尾数位数和指数值范围的零点位置
其中调用了Float.floatToRawIntBits(float value)
其功能是:根据 IEEE 754 的浮点“单一形式”中的位布局,返回指定浮点值的表示形式,并保留非数字 (NaN) 值。
numMantissaBits:在byte中使用的尾数位数

zeroExp:在指数值范围内的零点
f:需要转化的32位float
返回8位float,对精度有影响

  

 

public static float byteToFloat(byte b, int numMantissaBits, int zeroExp)

8位到32位flloat

 

public static byte floatToByte315(float f)

相当于:floatToByte(b, mantissaBits=3, zeroExponent=15)

 

  public static float byte315ToFloat(byte b)

相当于: byteToFloat(b, mantissaBits=3, zeroExponent=15) 

 

 public static byte floatToByte52(float f)

相当于 :floatToByte(b, mantissaBits=5, zeroExponent=2)

public static float byte52ToFloat(byte b)

相当于:byteToFloat(b, mantissaBits=5, zeroExponent=2)

你可能感兴趣的:(lucene,java)