Import GNU Classpath (libgcj-import-20070727).
[official-gcc.git] / libjava / classpath / gnu / java / math / Fixed.java
blobed4150b108c83c0d191476c54cf03745db073426
1 /* Fixed.java -- Utility methods for fixed point arithmetics
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.math;
41 /**
42 * Utility methods for fixed point arithmetics.
44 public final class Fixed
47 /**
48 * Private constructor to avoid instantiation.
50 private Fixed()
52 // Forbidden constructor.
55 /**
56 * Divides two fixed point values with <code>n</code> digits.
58 * @param n the number of digits
59 * @param a the first operand as fixed point value
60 * @param b the second operand as fixed point value
62 * @return <code>a / b</code> as fixed point value
64 public static int div(int n, int a, int b)
66 return (int) ((((long) a) << n) / b);
69 /**
70 * Multiplies two fixed point values with <code>n</code> digits.
72 * @param n the number of digits
73 * @param a the first operand as fixed point value
74 * @param b the second operand as fixed point value
76 * @return <code>a * b</code> as fixed point value
78 public static int mul(int n, int a, int b)
80 return (int) ((((long) a) * b) >> n);
83 /**
84 * Returns the ceiling value of a fixed point value <code>a</code> with
85 * the <code>n</code> digits.
87 * @param n the number of digits
88 * @param a the fixed point value
90 * @return <code>ceil(a)</code> as fixed point value
92 public static int ceil(int n, int a)
94 return (a + (1 << n - 1)) & -(1 << n);
97 /**
98 * Returns the floor value of a fixed point value <code>a</code> with
99 * <code>n</code> digits.
101 * @param n the number of digits
102 * @param a the fixed point value
104 * @return <code>floor(a)</code> as fixed point value
106 public static int floor(int n, int a)
108 return a & -(1 << n);
112 * Truncates the number so that only the digits after the point are left.
114 * @param n the number of digits
115 * @param a the fixed point value
117 * @return the truncated value
119 public static int trunc(int n, int a)
121 return a & (0xFFFFFFFF >>> 32 - n);
125 * Returns the round value of a fixed point value <code>a</code> with
126 * the <code>n</code> digits.
128 * @param n the number of digits
129 * @param a the fixed point value
131 * @return <code>round(a)</code> as fixed point value
133 public static int round(int n, int a)
135 return (a + (1 << (n - 1))) & -(1 << n);
139 * Returns the fixed point value <code>a</code> with <code>n</code> digits
140 * as float.
142 * @param n the number of digits
143 * @param a the fixed point value
145 * @return the float value of <code>a</code>
147 public static float floatValue(int n, int a)
149 return ((float) a) / (1 << n);
153 * Returns the fixed point value <code>a</code> with <code>n</code> digits
154 * as double.
156 * @param n the number of digits
157 * @param a the fixed point value
159 * @return the double value of <code>a</code>
161 public static double doubleValue(int n, int a)
163 return ((double) a) / (1 << n);
167 * Returns the fixed point value that corresponds to the specified float
168 * value <code>a</code> with <code>n</code> digits.
170 * @param n the number of digits
171 * @param a the float value
173 * @return the fixed point value
175 public static int fixedValue(int n, float a)
177 return (int) (a * (1 << n));
181 * Returns the fixed point value that corresponds to the specified double
182 * value <code>a</code> with <code>n</code> digits.
184 * @param n the number of digits
185 * @param a the double value
187 * @return the fixed point value
189 public static int fixedValue(int n, double a)
191 return (int) (a * (1 << n));
195 * Returns the integer value of the specified fixed point value
196 * <code>a</code>. This simply cuts of the digits (== floor(a)).
198 * @param n the number of digits
199 * @param a the fixed point value
201 * @return the integer value
203 public static int intValue(int n, int a)
205 return a >> n;
209 * Returns a fixed point decimal as rounded integer value.
211 * @param n the number of digits
212 * @param a the fixed point number
214 * @return the fixed point decimal as rounded integer value
216 public static int roundIntValue(int n, int a)
218 return (a + (1 << (n - 1))) >> n;