001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.math;
020
021import java.math.BigDecimal;
022import java.math.MathContext;
023
024
025/**
026 * Math Util
027 */
028public final class MathUtil {
029
030    /**
031     * abs
032     * @param number
033     * @return abs value
034     */
035    public static double abs(double number) {
036        return (number <= 0.0D) ? 0.0D - number : number;
037    }
038
039    public static double sgn(double number) {
040        return number != 0.0d ? number >= 0.0d ? 1 : -1 : 0;
041    }
042
043        public static int nextPowerOf2(int value) {
044
045                int result = 1;
046                while (result < value)
047                        result = result << 1;
048
049                return result;
050        }
051
052        public static BigDecimal divide(BigDecimal left, BigDecimal right) {
053                try {
054                        return left.divide(right,BigDecimal.ROUND_UNNECESSARY);
055                }
056                catch (ArithmeticException ex) {
057                        return left.divide(right,MathContext.DECIMAL128);
058                }
059        }
060
061        public static BigDecimal add(BigDecimal left, BigDecimal right) {
062                try {
063                        return left.add(right,MathContext.UNLIMITED);
064                }
065                catch (ArithmeticException ex) {
066                        return left.add(right,MathContext.DECIMAL128);
067                }
068        }
069
070        public static BigDecimal subtract(BigDecimal left, BigDecimal right) {
071                try {
072                        return left.subtract(right,MathContext.UNLIMITED);
073                }
074                catch (ArithmeticException ex) {
075                        return left.subtract(right,MathContext.DECIMAL128);
076                }
077        }
078
079        public static BigDecimal multiply(BigDecimal left, BigDecimal right) {
080                try {
081                        return left.multiply(right,MathContext.UNLIMITED);
082                }
083                catch (ArithmeticException ex) {
084                        return left.multiply(right,MathContext.DECIMAL128);
085                }
086        }
087}