📄 double.java
字号:
/* libaegisvm - The Aegis Virtual Machine for executing Java bytecode Copyright (C) 2001-2002 Philip W. L. Fong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/// java.lang.Double// (c) Peter Nagy// API version: 1.0.2// History:// 1997-01-18 Initial version Peter Nagy// 1997-02-25 modified Glynn Clements <glynn@sensei.co.uk>// 1997-12-02 modified Glynn Clements <glynn@sensei.co.uk>// valueOf() written to use DoubleParserpackage java.lang;import aegis.*;public final class Double extends Number implements Comparable { public static final double POSITIVE_INFINITY; public static final double NEGATIVE_INFINITY; public static final double NaN; public static final double MIN_VALUE; public static final double MAX_VALUE; public static final Class TYPE; static { POSITIVE_INFINITY = rawLongBitsToDouble(0x7ff0000000000000L); NEGATIVE_INFINITY = rawLongBitsToDouble(0xfff0000000000000L); NaN = rawLongBitsToDouble(0x7ff8000000000000L); MAX_VALUE = rawLongBitsToDouble(0x7fefffffffffffffL); MIN_VALUE = rawLongBitsToDouble(0x0000000000000001L); TYPE = Class.getPrimitiveType(Class.T_DOUBLE); } private double value; public Double(double value) { this.value = value; } public Double(String s) throws NumberFormatException { this.value = parseDouble(s); } /** * \todo Some of the test cases in test-JavaLangNumber don't work. * Need to implement conversion algorithms along the line * of Burger and Dybvig 1996. See * http://citeseer.nj.nec.com/28233.html */ public static String toString(double v) { if (isNaN(v)) return "NaN"; if (v == POSITIVE_INFINITY) return "Infinity"; if (v == NEGATIVE_INFINITY) return "-Infinity"; if (v == 0.0d) return (1.0d / v == POSITIVE_INFINITY) ? "0.0" : "-0.0"; boolean negative = (doubleToLongBits(v) & (1L << 63)) != 0; double m = negative ? -v : v; String result; if (m >= 1e-3 && m < 1e7) { double original = m; double approx; int multiplier = 0; result = "" + (int) m + "."; approx = (int) m; m -= approx; if (m == 0.0) { result += '0'; } else { while (approx < original) { m *= 10.0; int digit = (int) m; m -= digit; result += digit; multiplier++; approx = parseDouble(result); } } } else { int exponent = 0; double mantissa = m; double original = m; double approx; double multiplier = 1.0; if (mantissa > 1.0) { while (mantissa > 10.0) { mantissa /= 10.0; multiplier *= 10.0; exponent++; } } else { while (mantissa <= 1.0) { mantissa *= 10.0; multiplier /= 10.0; exponent--; } } result = "" + (int) mantissa + "."; approx = multiplier * (int) mantissa; mantissa -= (int) mantissa; while (approx < original) { mantissa *= 10.0; int digit = (int) mantissa; mantissa -= digit; result += digit; multiplier /= 10.0; approx += multiplier * digit; } result += "E" + Integer.toString(exponent); // int exponent = (int) (Math.log(m) / Math.log(10.0d)); // double mantissa = m / Math.pow(10.0d, (double) exponent); // result = toString(mantissa) + "E" + Integer.toString(exponent); } return negative ? ("-" + result) : result; } public static Double valueOf(String s) throws NumberFormatException { return new Double(parseDouble(s)); } /** * \todo Should turn DoubleParser into an inner class of Double. */ public static double parseDouble(String s) throws NumberFormatException { return DoubleParser.parseDouble(s); } public static boolean isNaN(double v) { return v != v; } public static boolean isInfinite(double v) { return (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY); } public boolean isNaN() { return isNaN(value); } public boolean isInfinite() { return isInfinite(value); } public String toString() { return toString(value); } public byte byteValue() { return (byte) value; } public short shortValue() { return (short) value; } public int intValue() { return (int) value; } public long longValue() { return (long) value; } public float floatValue() { return (float) value; } public double doubleValue() { return value; } public int hashCode() { long v = doubleToLongBits(value); return (int) (v ^ (v >>> 32)); } public boolean equals(Object obj) { return (obj instanceof Double) && doubleToLongBits(value) == doubleToLongBits(((Double) obj).value); } public static long doubleToLongBits(double value) { if (isNaN(value)) return 0x7ff8000000000000L; return doubleToRawLongBits(value); } public static native long doubleToRawLongBits(double value); public static double longBitsToDouble(long bits) { if (bits >= 0x7ff0000000000001L && bits <= 0x7fffffffffffffffL) return NaN; if (bits >= 0xfff0000000000001L && bits <= 0xffffffffffffffffL) return NaN; return rawLongBitsToDouble(bits); } private static native double rawLongBitsToDouble(long bits); public int compareTo(Double anotherDouble) { double value2 = anotherDouble.value; if (isNaN(value)) { if (isNaN(value2)) return 0; else return 1; } else if (isNaN(value2)) { return -1; } else if (value == 0.0d && value2 == 0.0d) { if (1.0d / value == POSITIVE_INFINITY) { if (1.0d / value2 == POSITIVE_INFINITY) return 0; else return 1; } else { if (1.0d / value2 == POSITIVE_INFINITY) return -1; else return 0; } } else { if (value > value2) return 1; else if (value < value2) return -1; else return 0; } } public int compareTo(Object o) { return compareTo((Double) o); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -