Debian Codegraph @

src/dr/math/BFloat.java

raw symbols SCIP
1 /*
2 * BFloat.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math;
27
28 /**
29 *
30 * BFloat -- floats with added buoyancy
31 *
32 * Implements numbers with precision equal to
33 * floats, and range roughly
34 *
35 * 1.0e-K ... 1.0e+K with K = log(2^(104*10^9))
36 *
37 * BFloat is a signed version
38 * UBFloat is an unsigned version (slightly faster)
39 * BDouble is a signed double version
40 * UBDouble is an unsigned double version
41 *
42 * Todo:
43 * - Implement everything except BFloat
44 * - Implement comparisons
45 * - Implement division, subtraction
46 *
47 *
48 * @author Gerton Lunter
49 *
50 * 14/4/2003
51 *
52 */
53
54 public class BFloat implements Cloneable {
55 /** Mantissa and exponent: */
56 float iF;
57 int iE;
58
59 /** Static stuff: */
60 /** Mantissa iF is guaranteed to satisfy iRangeInv <= |iF| <= iRange.
61 2^104 = 2.03e+31. Nice clean float, to minimize computational noise. */
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 /** Value between square root of iRange, and iRange. Square of this
67 value should still be representable with full mantissa. */
68 static private final float iHalfRange = 1.0e+18F;
69 static private final float iHalfRangeInv = 1.0e-18F;
70
71 /** Tiniest number representable is iRangeInv ^ iInfExp */
72 static private final int iInfExp = 1000000000;
73
74 /** true if checking for exponent overflow */
75 static private final boolean iCheckOF = false;
76
77 /** Static array for adding numbers */
78 static private final int iConvTableSize = 100;
79 static private float[] iConvTable = new float[iConvTableSize];
80
81 /** Static initialization block: */
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 /** Clone this object */
90 public Object clone() {
91 try {
92 // This magically creates an object of the right type
93 BFloat iObj = (BFloat) super.clone();
94 // No need to call clone(), as these are not Objects
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 /** Overriding built-in toString */
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 // The double->float cast may result in iFM == 10.0, and we can't have that.
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 /** First and intermediate normalization */
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 /* throw exception */
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 /* throw exception */
186 }
187 }
188 }
189
190
191 /** Various constructors */
192
193 public BFloat(float iX) {
194 iF = iX;
195 iE = 0;
196 normalise();
197 }
198
199 public BFloat(double iX) {
200 // This is a bit of code-duplication, but
201 // I didn't see a clean way of doing it.
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 /** Static members that return a BFloat */
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 /** Members that return a non-BFloat. Nicer to overload Math.log etc, but hey. */
241
242 public double log() {
243 return iE*(double)iLogRange + Math.log(iF);
244 }
245
246 /** Calculations */
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 // No normalization, for speed. Although adding numbers
263 // may break the invariant, this is would require a bit of
264 // devious programming, since iHalfRange is not precisely
265 // the square root of iRange.
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 /** Todo: less, lessequal, */
291
292 }
293