org.databene.commons.MathUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of databene-commons Show documentation
Show all versions of databene-commons Show documentation
'databene commons' is an open source Java library by Volker Bergmann.
It provides extensions to the Java core library by utility classes, abstract concepts
and concrete implementations.
The newest version!
/*
* Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.databene.commons;
/**
* Provides mathematical utility methods.
* @since 0.4.0
* @author Volker Bergmann
*/
public class MathUtil {
/**
* Returns the number of digits needed for displaying the postfix values of a number, but at most 7.
* @param number the number to examine
* @return the number of digits needed for displaying the postfix values of a number, but at most 7
*/
public static int fractionDigits(double number) {
double x = fraction(number);
int n = 0;
while (x >= 0.0000001 && n < 7) {
n++;
x = fraction(x * 10);
}
return n;
}
public static boolean isIntegralValue(double number) {
return (Math.IEEEremainder(Math.abs(number), 1) == 0);
}
private static double fraction(double number) {
double value = Math.IEEEremainder(Math.abs(number), 1);
if (value < 0)
value += 1;
return value;
}
public static int prefixDigitCount(double number) {
return nonNegativeDigitCount((long) Math.abs(number));
}
public static int digitCount(long number) {
return nonNegativeDigitCount(Math.abs(number));
}
private static int nonNegativeDigitCount(long number) {
if (number <= 1)
return 1;
return 1 + (int) Math.log10(number);
}
public static int sumOfDigits(int i) {
int tmp = i;
int result = 0;
while (tmp > 0) {
result += tmp % 10;
tmp /= 10;
}
return result;
}
public static int weightedSumOfSumOfDigits(String number, int startIndex, int... weights) {
int sum = 0;
for (int i = 0; i < weights.length; i++)
sum += MathUtil.sumOfDigits(weights[i] * (number.charAt(startIndex + i) - '0'));
return sum;
}
public static int weightedSumOfDigits(CharSequence number, int startIndex, int... weights) {
int sum = 0;
for (int i = 0; i < weights.length; i++)
sum += weights[i] * (number.charAt(startIndex + i) - '0');
return sum;
}
public static boolean rangeIncludes(long x, long min, long max) {
return (min <= x && x <= max);
}
public static boolean rangeIncludes(double x, double min, double max) {
return (min <= x && x <= max);
}
public static boolean between(long x, long min, long max) {
return (min < x && x < max);
}
public static boolean between(double x, double min, double max) {
return (min < x && x < max);
}
public static Double sum(double[] addends) {
double result = 0;
for (double addend : addends)
result += addend;
return result;
}
public static int max(int... args) {
int result = args[0];
for (int i = 1; i < args.length; i++)
if (args[i] > result)
result = args[i];
return result;
}
public static double max(double... args) {
double result = args[0];
for (int i = 1; i < args.length; i++)
if (args[i] > result)
result = args[i];
return result;
}
public static double min(double... args) {
double result = args[0];
for (int i = 1; i < args.length; i++)
if (args[i] < result)
result = args[i];
return result;
}
public static Double nullableProduct(Double... factors) {
if (factors.length == 0 || factors[0] == null)
return null;
Double result = factors[0];
for (int i = 1; i < factors.length; i++) {
Double factor = factors[i];
if (factor == null)
return null;
result *= factor;
}
return result;
}
public static Double nullableDivision(Double dividend, Double... divisors) {
if (dividend == null || divisors.length == 0 || divisors[0] == null)
return null;
Double result = dividend;
for (int i = 0; i < divisors.length; i++) {
Double divisor = divisors[i];
if (divisor == null)
return null;
result /= divisor;
}
return result;
}
public static Double nullableSum(Double... summands) {
if (summands.length == 0 || summands[0] == null)
return null;
Double result = summands[0];
for (int i = 1; i < summands.length; i++) {
Double factor = summands[i];
if (factor == null)
return null;
result += factor;
}
return result;
}
public static Double nullableSubtraction(Double minuend, Double... subtrahends) {
if (minuend == null || subtrahends.length == 0 || subtrahends[0] == null)
return null;
Double result = minuend;
for (int i = 0; i < subtrahends.length; i++) {
Double divisor = subtrahends[i];
if (divisor == null)
return null;
result -= divisor;
}
return result;
}
}