您的位置:首页 > 其它

Float类

2015-10-02 18:28 323 查看
这一节,一起研究下Float类,该类同样是继承Numeber类,实现Comparable(Float)接口。

重点内容

属性:

static final int EXPONENT_BIAS = 127;

static final int EXPONENT_BITS = 9;

static final int MANTISSA_BITS = 23;

static final int NON_MANTISSA_BITS = 9;

static final int SIGN_MASK = 0x80000000;

static final int EXPONENT_MASK = 0x7f800000;

static final int MANTISSA_MASK = 0x007fffff;

public static final float MAX_VALUE = 3.40282346638528860e+38f;

public static final float MIN_VALUE = 1.40129846432481707e-45f;

public static final float NaN = 0.0f / 0.0f;

public static final float POSITIVE_INFINITY = 1.0f / 0.0f;

public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;

public static final float MIN_NORMAL = 1.1754943508222875E-38f;

public static final int MAX_EXPONENT = 127;

public static final int MIN_EXPONENT = -126;

private final float value;


构造方法:

public Float(float value) {
this.value = value;
}

public Float(double value) {
this.value = (float) value;
}

public Float(String string) throws NumberFormatException {
this(parseFloat(string));
}


对象方法:

public int compareTo(Float object) {
return compare(value, object.value);
}

@Override
public byte byteValue() {
return (byte) value;
}

@Override
public double doubleValue() {
return value;
}

@Override
public boolean equals(Object object) {
return (object instanceof Float) &&
(floatToIntBits(this.value) == floatToIntBits(((Float) object).value));
}

@Override
public float floatValue() {
return value;
}

@Override
public int hashCode() {
return floatToIntBits(value);
}

@Override
public int intValue() {
return (int) value;
}

public boolean isInfinite() {
return isInfinite(value);
}

public boolean isNaN() {
return isNaN(value);
}

@Override
public long longValue() {
return (long) value;
}

@Override
public short shortValue() {
return (short) value;
}

@Override
public String toString() {
return Float.toString(value);
}


类方法:

public static String toHexString(float f) {
/*
* Reference: http://en.wikipedia.org/wiki/IEEE_754-1985 */
if (f != f) {
return "NaN";
}
if (f == POSITIVE_INFINITY) {
return "Infinity";
}
if (f == NEGATIVE_INFINITY) {
return "-Infinity";
}

int bitValue = floatToIntBits(f);

boolean negative = (bitValue & 0x80000000) != 0;
// mask exponent bits and shift down
int exponent = (bitValue & 0x7f800000) >>> 23;
// mask significand bits and shift up
// significand is 23-bits, so we shift to treat it like 24-bits
int significand = (bitValue & 0x007FFFFF) << 1;

if (exponent == 0 && significand == 0) {
return (negative ? "-0x0.0p0" : "0x0.0p0");
}

StringBuilder hexString = new StringBuilder(10);
if (negative) {
hexString.append("-0x");
} else {
hexString.append("0x");
}

if (exponent == 0) { // denormal (subnormal) value
hexString.append("0.");
// significand is 23-bits, so there can be 6 hex digits
int fractionDigits = 6;
// remove trailing hex zeros, so Integer.toHexString() won't print
// them
while ((significand != 0) && ((significand & 0xF) == 0)) {
significand >>>= 4;
fractionDigits--;
}
// this assumes Integer.toHexString() returns lowercase characters
String hexSignificand = Integer.toHexString(significand);

// if there are digits left, then insert some '0' chars first
if (significand != 0 && fractionDigits > hexSignificand.length()) {
int digitDiff = fractionDigits - hexSignificand.length();
while (digitDiff-- != 0) {
hexString.append('0');
}
}
hexString.append(hexSignificand);
hexString.append("p-126");
} else { // normal value
hexString.append("1.");
// significand is 23-bits, so there can be 6 hex digits
int fractionDigits = 6;
// remove trailing hex zeros, so Integer.toHexString() won't print
// them
while ((significand != 0) && ((significand & 0xF) == 0)) {
significand >>>= 4;
fractionDigits--;
}
// this assumes Integer.toHexString() returns lowercase characters
String hexSignificand = Integer.toHexString(significand);

// if there are digits left, then insert some '0' chars first
if (significand != 0 && fractionDigits > hexSignificand.length()) {
int digitDiff = fractionDigits - hexSignificand.length();
while (digitDiff-- != 0) {
hexString.append('0');
}
}
hexString.append(hexSignificand);
hexString.append('p');
// remove exponent's 'bias' and convert to a string
hexString.append(exponent - 127);
}
return hexString.toString();
}

public static Float valueOf(float f) {
return new Float(f);
}

public static int compare(float float1, float float2) {
// Non-zero, non-NaN checking.
if (float1 > float2) {
return 1;
}
if (float2 > float1) {
return -1;
}
if (float1 == float2 && 0.0f != float1) {
return 0;
}

// NaNs are equal to other NaNs and larger than any other float
if (isNaN(float1)) {
if (isNaN(float2)) {
return 0;
}
return 1;
} else if (isNaN(float2)) {
return -1;
}

// Deal with +0.0 and -0.0
int f1 = floatToRawIntBits(float1);
int f2 = floatToRawIntBits(float2);
// The below expression is equivalent to:
// (f1 == f2) ? 0 : (f1 < f2) ? -1 : 1
// because f1 and f2 are either 0 or Integer.MIN_VALUE
return (f1 >> 31) - (f2 >> 31);
}

public static Float valueOf(String string) throws NumberFormatException {
return parseFloat(string);
}

public static String toString(float f) {
return RealToString.getInstance().floatToString(f);
}

public static float parseFloat(String string) throws NumberFormatException {
return StringToReal.parseFloat(string);
}

public static boolean isNaN(float f) {
return f != f;
}

public static boolean isInfinite(float f) {
return (f == POSITIVE_INFINITY) || (f == NEGATIVE_INFINITY);
}

public static int floatToIntBits(float value) {
if (value != value) {
return 0x7fc00000;  // NaN.
} else {
return floatToRawIntBits(value);
}
}

@SuppressWarnings("unchecked")
public static final Class<Float> TYPE
= (Class<Float>) float[].class.getComponentType();


Native方法:

public static native int floatToRawIntBits(float value);

public static native float intBitsToFloat(int bits);


Float类与之前几个类的区别,没有静态代码块,并且增加了Native方法,多了个构造方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: