001: /*
002: * The JTS Topology Suite is a collection of Java classes that
003: * implement the fundamental operations required to validate a given
004: * geo-spatial data set to a known topological specification.
005: *
006: * Copyright (C) 2001 Vivid Solutions
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: *
022: * For more information, contact:
023: *
024: * Vivid Solutions
025: * Suite #1A
026: * 2328 Government Street
027: * Victoria BC V8T 5G5
028: * Canada
029: *
030: * (250)385-6040
031: * www.vividsolutions.com
032: */
033: package com.vividsolutions.jts.index.quadtree;
034:
035: /**
036: * DoubleBits manipulates Double numbers
037: * by using bit manipulation and bit-field extraction.
038: * For some operations (such as determining the exponent)
039: * this is more accurate than using mathematical operations
040: * (which suffer from round-off error).
041: * <p>
042: * The algorithms and constants in this class
043: * apply only to IEEE-754 double-precision floating point format.
044: *
045: * @version 1.7
046: */
047: public class DoubleBits {
048:
049: public static final int EXPONENT_BIAS = 1023;
050:
051: public static double powerOf2(int exp) {
052: if (exp > 1023 || exp < -1022)
053: throw new IllegalArgumentException("Exponent out of bounds");
054: long expBias = exp + EXPONENT_BIAS;
055: long bits = (long) expBias << 52;
056: return Double.longBitsToDouble(bits);
057: }
058:
059: public static int exponent(double d) {
060: DoubleBits db = new DoubleBits(d);
061: return db.getExponent();
062: }
063:
064: public static double truncateToPowerOfTwo(double d) {
065: DoubleBits db = new DoubleBits(d);
066: db.zeroLowerBits(52);
067: return db.getDouble();
068: }
069:
070: public static String toBinaryString(double d) {
071: DoubleBits db = new DoubleBits(d);
072: return db.toString();
073: }
074:
075: public static double maximumCommonMantissa(double d1, double d2) {
076: if (d1 == 0.0 || d2 == 0.0)
077: return 0.0;
078:
079: DoubleBits db1 = new DoubleBits(d1);
080: DoubleBits db2 = new DoubleBits(d2);
081:
082: if (db1.getExponent() != db2.getExponent())
083: return 0.0;
084:
085: int maxCommon = db1.numCommonMantissaBits(db2);
086: db1.zeroLowerBits(64 - (12 + maxCommon));
087: return db1.getDouble();
088: }
089:
090: private double x;
091: private long xBits;
092:
093: public DoubleBits(double x) {
094: this .x = x;
095: xBits = Double.doubleToLongBits(x);
096: }
097:
098: public double getDouble() {
099: return Double.longBitsToDouble(xBits);
100: }
101:
102: /**
103: * Determines the exponent for the number
104: */
105: public int biasedExponent() {
106: int signExp = (int) (xBits >> 52);
107: int exp = signExp & 0x07ff;
108: return exp;
109: }
110:
111: /**
112: * Determines the exponent for the number
113: */
114: public int getExponent() {
115: return biasedExponent() - EXPONENT_BIAS;
116: }
117:
118: public void zeroLowerBits(int nBits) {
119: long invMask = (1L << nBits) - 1L;
120: long mask = ~invMask;
121: xBits &= mask;
122: }
123:
124: public int getBit(int i) {
125: long mask = (1L << i);
126: return (xBits & mask) != 0 ? 1 : 0;
127: }
128:
129: /**
130: * This computes the number of common most-significant bits in the mantissa.
131: * It does not count the hidden bit, which is always 1.
132: * It does not determine whether the numbers have the same exponent - if they do
133: * not, the value computed by this function is meaningless.
134: * @param db
135: * @return the number of common most-significant mantissa bits
136: */
137: public int numCommonMantissaBits(DoubleBits db) {
138: for (int i = 0; i < 52; i++) {
139: int bitIndex = i + 12;
140: if (getBit(i) != db.getBit(i))
141: return i;
142: }
143: return 52;
144: }
145:
146: /**
147: * A representation of the Double bits formatted for easy readability
148: */
149: public String toString() {
150: String numStr = Long.toBinaryString(xBits);
151: // 64 zeroes!
152: String zero64 = "0000000000000000000000000000000000000000000000000000000000000000";
153: String padStr = zero64 + numStr;
154: String bitStr = padStr.substring(padStr.length() - 64);
155: String str = bitStr.substring(0, 1) + " "
156: + bitStr.substring(1, 12) + "(" + getExponent() + ") "
157: + bitStr.substring(12) + " [ " + x + " ]";
158: return str;
159: }
160: }
|