001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019
020 import java.text.NumberFormat;
021
022 import java.util.HashMap;
023 import java.util.Map;
024
025
028 public class MathUtil {
029
030 public static int base2Log(long x) {
031 return _base2LogValues.get(x);
032 }
033
034 public static long base2Pow(int x) {
035 if (x == 0) {
036 return 1;
037 }
038 else {
039 return 2L << (x - 1);
040 }
041 }
042
043 public static int difference(Integer value1, Integer value2) {
044 return value1 - value2;
045 }
046
047 public static int factorial(int x) {
048 if (x < 0) {
049 return 0;
050 }
051
052 int factorial = 1;
053
054 while (x > 1) {
055 factorial = factorial * x;
056 x = x - 1;
057 }
058
059 return factorial;
060 }
061
062 public static double format(double x, int max, int min) {
063 NumberFormat nf = NumberFormat.getInstance();
064
065 nf.setMaximumFractionDigits(max);
066 nf.setMinimumFractionDigits(min);
067
068 try {
069 Number number = nf.parse(nf.format(x));
070
071 x = number.doubleValue();
072 }
073 catch (Exception e) {
074 _log.error(e.getMessage());
075 }
076
077 return x;
078 }
079
080 public static int[] generatePrimes(int max) {
081 if (max < 2) {
082 return new int[0];
083 }
084
085 boolean[] crossedOut = new boolean[max + 1];
086
087 for (int i = 2; i < crossedOut.length; i++) {
088 crossedOut[i] = false;
089 }
090
091 int limit = (int)Math.sqrt(crossedOut.length);
092
093 for (int i = 2; i <= limit; i++) {
094 if (!crossedOut[i]) {
095 for (int multiple = 2 * i; multiple < crossedOut.length;
096 multiple += i) {
097
098 crossedOut[multiple] = true;
099 }
100 }
101 }
102
103 int uncrossedCount = 0;
104
105 for (int i = 2; i < crossedOut.length; i++) {
106 if (!crossedOut[i]) {
107 uncrossedCount++;
108 }
109 }
110
111 int[] result = new int[uncrossedCount];
112
113 for (int i = 2, j = 0; i < crossedOut.length; i++) {
114 if (!crossedOut[i]) {
115 result[j++] = i;
116 }
117 }
118
119 return result;
120 }
121
122 public static boolean isEven(int x) {
123 if ((x % 2) == 0) {
124 return true;
125 }
126
127 return false;
128 }
129
130 public static boolean isOdd(int x) {
131 return !isEven(x);
132 }
133
134 public static double product(Double... values) {
135 double product = 1.0;
136
137 for (double value : values) {
138 product *= value;
139 }
140
141 return product;
142 }
143
144 public static int product(Integer... values) {
145 int product = 1;
146
147 for (int value : values) {
148 product *= value;
149 }
150
151 return product;
152 }
153
154 public static long product(Long... values) {
155 long product = 1;
156
157 for (long value : values) {
158 product *= value;
159 }
160
161 return product;
162 }
163
164 public static int quotient(Integer value1, Integer value2) {
165 return value1 / value2;
166 }
167
168 public static double sum(Double... values) {
169 double sum = 0.0;
170
171 for (double value : values) {
172 sum += value;
173 }
174
175 return sum;
176 }
177
178 public static int sum(Integer... values) {
179 int sum = 0;
180
181 for (int value : values) {
182 sum += value;
183 }
184
185 return sum;
186 }
187
188 public static long sum(Long... values) {
189 long sum = 0;
190
191 for (long value : values) {
192 sum += value;
193 }
194
195 return sum;
196 }
197
198 private static final Log _log = LogFactoryUtil.getLog(MathUtil.class);
199
200 private static final Map<Long, Integer> _base2LogValues = new HashMap<>();
201
202 static {
203 _base2LogValues.put(0L, Integer.MIN_VALUE);
204
205 for (int i = 0; i < 63; i++) {
206 _base2LogValues.put(base2Pow(i), i);
207 }
208 }
209
210 }