| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.math; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public class BFloat implements Cloneable { |
| 55 |
|
| 56 |
float iF; |
| 57 |
int iE; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
static private final float iRange = 20282409603651670423947251286016.0F; |
| 63 |
static private final float iRangeInv = 1.0F/iRange; |
| 64 |
static private final float iLogRange = (float)Math.log(iRange); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static private final float iHalfRange = 1.0e+18F; |
| 69 |
static private final float iHalfRangeInv = 1.0e-18F; |
| 70 |
|
| 71 |
|
| 72 |
static private final int iInfExp = 1000000000; |
| 73 |
|
| 74 |
|
| 75 |
static private final boolean iCheckOF = false; |
| 76 |
|
| 77 |
|
| 78 |
static private final int iConvTableSize = 100; |
| 79 |
static private float[] iConvTable = new float[iConvTableSize]; |
| 80 |
|
| 81 |
|
| 82 |
static { |
| 83 |
iConvTable[0]= 1.0F; |
| 84 |
for (int i=1; i<iConvTableSize; i++) |
| 85 |
iConvTable[i] = iConvTable[i-1] * iRangeInv; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public Object clone() { |
| 91 |
try { |
| 92 |
|
| 93 |
BFloat iObj = (BFloat) super.clone(); |
| 94 |
|
| 95 |
iObj.iE = iE; |
| 96 |
iObj.iF = iF; |
| 97 |
return iObj; |
| 98 |
} catch (CloneNotSupportedException e) { |
| 99 |
System.out.println("BFloat.clone: Something happened that cannot happen -- ?"); |
| 100 |
return null; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
public String toString() { |
| 106 |
String iResult; |
| 107 |
if (iE == iInfExp) { |
| 108 |
iResult = "1.0e+Inf"; |
| 109 |
} else if (iE == -iInfExp) { |
| 110 |
iResult = "1.0e-Inf"; |
| 111 |
} else { |
| 112 |
double iM = (Math.log(iF) + Math.log(iRange)*(double)iE)/Math.log(10.0); |
| 113 |
long iExp = (long)Math.floor(iM); |
| 114 |
iM -= Math.floor(iM); |
| 115 |
float iFM = (float)Math.exp(iM*Math.log(10.0)); |
| 116 |
|
| 117 |
if (iFM >= 10.0F) { |
| 118 |
iFM = 1.0F; |
| 119 |
iExp++; |
| 120 |
} |
| 121 |
iResult = String.valueOf(iFM) + "e"; |
| 122 |
if (iExp<0) |
| 123 |
iResult += String.valueOf(iExp); |
| 124 |
else |
| 125 |
iResult += "+" + String.valueOf(iExp); |
| 126 |
} |
| 127 |
return iResult; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private void normalise() { |
| 133 |
if (iF > 0.0) { |
| 134 |
while (iF > iHalfRange) { |
| 135 |
iF *= iRangeInv; |
| 136 |
iE++; |
| 137 |
} |
| 138 |
while (iF < iHalfRangeInv) { |
| 139 |
iF *= iRange; |
| 140 |
iE--; |
| 141 |
} |
| 142 |
} else if (iF < 0.0) { |
| 143 |
while (iF < -iHalfRange) { |
| 144 |
iF *= iRangeInv; |
| 145 |
iE++; |
| 146 |
} |
| 147 |
while (iF > -iHalfRangeInv) { |
| 148 |
iF *= iRange; |
| 149 |
iE--; |
| 150 |
} |
| 151 |
} else { |
| 152 |
iE = -iInfExp; |
| 153 |
} |
| 154 |
if (iCheckOF) { |
| 155 |
if (Math.abs(iE)>iInfExp) { |
| 156 |
System.out.println("BFloat: exponent overflow"); |
| 157 |
|
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private void normaliseOnce() { |
| 163 |
if (iF > 0.0) { |
| 164 |
if (iF > iHalfRange) { |
| 165 |
iF *= iRangeInv; |
| 166 |
iE++; |
| 167 |
} else if (iF < iHalfRangeInv) { |
| 168 |
iF *= iRange; |
| 169 |
iE--; |
| 170 |
} |
| 171 |
} else if (iF < 0.0) { |
| 172 |
if (iF < -iHalfRange) { |
| 173 |
iF *= iRangeInv; |
| 174 |
iE++; |
| 175 |
} else if (iF > -iHalfRangeInv) { |
| 176 |
iF *= iRange; |
| 177 |
iE--; |
| 178 |
} |
| 179 |
} else { |
| 180 |
iE = -iInfExp; |
| 181 |
} |
| 182 |
if (iCheckOF) { |
| 183 |
if (Math.abs(iE)>iInfExp) { |
| 184 |
System.out.println("BFloat: exponent overflow"); |
| 185 |
|
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public BFloat(float iX) { |
| 194 |
iF = iX; |
| 195 |
iE = 0; |
| 196 |
normalise(); |
| 197 |
} |
| 198 |
|
| 199 |
public BFloat(double iX) { |
| 200 |
|
| 201 |
|
| 202 |
int iExp = 0; |
| 203 |
if (iX > 0.0) { |
| 204 |
while (iX > iHalfRange) { |
| 205 |
iX *= iRangeInv; |
| 206 |
iExp++; |
| 207 |
} |
| 208 |
while (iX < iHalfRangeInv) { |
| 209 |
iX *= iRange; |
| 210 |
iExp--; |
| 211 |
} |
| 212 |
} else if (iX < 0.0) { |
| 213 |
while (iX < -iHalfRange) { |
| 214 |
iX *= iRangeInv; |
| 215 |
iExp++; |
| 216 |
} |
| 217 |
while (iX > -iHalfRangeInv) { |
| 218 |
iX *= iRange; |
| 219 |
iExp--; |
| 220 |
} |
| 221 |
} else { |
| 222 |
iExp = -iInfExp; |
| 223 |
} |
| 224 |
iF = (float)iX; |
| 225 |
iE = iExp; |
| 226 |
} |
| 227 |
|
| 228 |
public BFloat(float iF, int iE) { |
| 229 |
this.iF = iF; |
| 230 |
this.iE = iE; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public static BFloat exp(float iX) { |
| 236 |
int iN = (int)Math.round(iX / iLogRange); |
| 237 |
return new BFloat( (float)Math.exp(iX - iN*iLogRange), iN ); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public double log() { |
| 243 |
return iE*(double)iLogRange + Math.log(iF); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public void add(BFloat iBF) { |
| 249 |
if (iE >= iBF.iE) { |
| 250 |
if (iE < iBF.iE + iConvTableSize) { |
| 251 |
iF += iBF.iF * iConvTable[ iE - iBF.iE ]; |
| 252 |
} |
| 253 |
} else { |
| 254 |
if (iE > iBF.iE - iConvTableSize) { |
| 255 |
iF = iBF.iF + iF * iConvTable[ iBF.iE - iE ]; |
| 256 |
iE = iBF.iE; |
| 257 |
} else { |
| 258 |
iF = iBF.iF; |
| 259 |
iE = iBF.iE; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
public void add(float iX) { |
| 269 |
this.add( new BFloat( iX ) ); |
| 270 |
} |
| 271 |
|
| 272 |
public void add(double iX) { |
| 273 |
this.add( new BFloat( iX ) ); |
| 274 |
} |
| 275 |
|
| 276 |
public void multiply(BFloat iBF) { |
| 277 |
iF *= iBF.iF; |
| 278 |
iE += iBF.iE; |
| 279 |
normaliseOnce(); |
| 280 |
} |
| 281 |
|
| 282 |
public void multiply(float iX) { |
| 283 |
this.multiply( new BFloat( iX ) ); |
| 284 |
} |
| 285 |
|
| 286 |
public void multiply(double iX) { |
| 287 |
this.multiply( new BFloat( iX ) ); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
} |
| 293 |
|