/********************************************************************** * $Id: DoubleBits.cpp,v 1.17 2004/12/08 13:54:43 strk Exp $ * * GEOS - Geometry Engine Open Source * http://geos.refractions.net * * Copyright (C) 2001-2002 Vivid Solutions Inc. * * This is free software; you can redistribute and/or modify it under * the terms of the GNU Lesser General Public Licence as published * by the Free Software Foundation. * See the COPYING file for more information. * **********************************************************************/ #include #include namespace geos { double DoubleBits::powerOf2(int exp) { if (exp>1023 || exp<-1022) throw new IllegalArgumentException("Exponent out of bounds"); #if ASSUME_IEEE_DOUBLE int64 expBias=exp+EXPONENT_BIAS; int64 bits=expBias << 52; double ret; memcpy(&ret, &bits, sizeof(int64)); return ret; #else return pow(2.0, exp); #endif } int DoubleBits::exponent(double d) { DoubleBits db(d); return db.getExponent(); } double DoubleBits::truncateToPowerOfTwo(double d) { DoubleBits db(d); db.zeroLowerBits(52); return db.getDouble(); } string DoubleBits::toBinaryString(double d) { DoubleBits db(d); return db.toString(); } double DoubleBits::maximumCommonMantissa(double d1, double d2) { if (d1==0.0 || d2==0.0) return 0.0; DoubleBits db1(d1); DoubleBits db2(d2); if (db1.getExponent() != db2.getExponent()) return 0.0; int maxCommon=db1.numCommonMantissaBits(&db2); db1.zeroLowerBits(64-(12+maxCommon)); return db1.getDouble(); } DoubleBits::DoubleBits(double nx) { #if ASSUME_IEEE_DOUBLE memcpy(&xBits,&nx,sizeof(double)); #endif x = nx; } double DoubleBits::getDouble() { return x; } /** * Determines the exponent for the number * * @return */ int64 DoubleBits::biasedExponent() { int64 signExp=xBits>>52; int64 exp=signExp&0x07ff; //cerr<<"xBits:"<