⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mathx.java

📁 java 读写word excel ppt
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements.  See the NOTICE file distributed with* this work for additional information regarding copyright ownership.* The ASF licenses this file to You 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.*//* * Created on May 19, 2005 * */package org.apache.poi.hssf.record.formula.functions;/** * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt; * This class is an extension to the standard math library * provided by java.lang.Math class. It follows the Math class * in that it has a private constructor and all static methods. */public final class MathX {        private MathX() {}            /**     * Returns a value rounded to p digits after decimal.     * If p is negative, then the number is rounded to     * places to the left of the decimal point. eg.      * 10.23 rounded to -1 will give: 10. If p is zero,     * the returned value is rounded to the nearest integral     * value.     * <p>If n is negative, the resulting value is obtained     * as the round value of absolute value of n multiplied     * by the sign value of n (@see MathX.sign(double d)).      * Thus, -0.6666666 rounded to p=0 will give -1 not 0.     * <p>If n is NaN, returned value is NaN.     * @param n     * @param p     * @return     */    public static double round(double n, int p) {        double retval;                if (Double.isNaN(n) || Double.isInfinite(n)) {            retval = Double.NaN;        }        else {            if (p != 0) {                double temp = Math.pow(10, p);                retval = Math.round(n*temp)/temp;            }            else {                retval = Math.round(n);            }        }                return retval;    }    /**     * Returns a value rounded-up to p digits after decimal.     * If p is negative, then the number is rounded to     * places to the left of the decimal point. eg.      * 10.23 rounded to -1 will give: 20. If p is zero,     * the returned value is rounded to the nearest integral     * value.     * <p>If n is negative, the resulting value is obtained     * as the round-up value of absolute value of n multiplied     * by the sign value of n (@see MathX.sign(double d)).      * Thus, -0.2 rounded-up to p=0 will give -1 not 0.     * <p>If n is NaN, returned value is NaN.     * @param n     * @param p     * @return     */    public static double roundUp(double n, int p) {        double retval;                if (Double.isNaN(n) || Double.isInfinite(n)) {            retval = Double.NaN;        }        else {            if (p != 0) {                double temp = Math.pow(10, p);                double nat = Math.abs(n*temp);                                retval = sign(n) *                     ((nat == (long) nat)                            ? nat / temp                            : Math.round(nat + 0.5) / temp);            }            else {                double na = Math.abs(n);                retval = sign(n) *                     ((na == (long) na)                        ? na                        : (long) na + 1);            }        }                return retval;    }    /**     * Returns a value rounded to p digits after decimal.     * If p is negative, then the number is rounded to     * places to the left of the decimal point. eg.      * 10.23 rounded to -1 will give: 10. If p is zero,     * the returned value is rounded to the nearest integral     * value.     * <p>If n is negative, the resulting value is obtained     * as the round-up value of absolute value of n multiplied     * by the sign value of n (@see MathX.sign(double d)).      * Thus, -0.8 rounded-down to p=0 will give 0 not -1.     * <p>If n is NaN, returned value is NaN.     * @param n     * @param p     * @return     */    public static double roundDown(double n, int p) {        double retval;                if (Double.isNaN(n) || Double.isInfinite(n)) {            retval = Double.NaN;        }        else {            if (p != 0) {                double temp = Math.pow(10, p);                retval = sign(n) * Math.round((Math.abs(n)*temp) - 0.5)/temp;            }            else {                retval = (long) n;            }        }                return retval;    }            /**     * If d < 0, returns short -1     * <br/>     * If d > 0, returns short 1     * <br/>     * If d == 0, returns short 0      * <p> If d is NaN, then 1 will be returned. It is the responsibility     * of caller to check for d isNaN if some other value is desired.     * @param d     * @return     */    public static short sign(double d) {        return (short) ((d == 0)                ? 0                : (d < 0)                        ? -1                        : 1);    }       /**     * average of all values     * @param values     * @return     */    public static double average(double[] values) {        double ave = 0;        double sum = 0;        for (int i=0, iSize=values.length; i<iSize; i++) {            sum += values[i];        }        ave = sum / values.length;        return ave;    }            /**     * sum of all values     * @param values     * @return     */    public static double sum(double[] values) {        double sum = 0;        for (int i=0, iSize=values.length; i<iSize; i++) {            sum += values[i];        }        return sum;    }        /**     * sum of squares of all values     * @param values     * @return     */    public static double sumsq(double[] values) {        double sumsq = 0;        for (int i=0, iSize=values.length; i<iSize; i++) {            sumsq += values[i]*values[i];        }        return sumsq;    }            /**     * product of all values     * @param values     * @return     */    public static double product(double[] values) {        double product = 0;        if (values!=null && values.length > 0) {            product = 1;            for (int i=0, iSize=values.length; i<iSize; i++) {                product *= values[i];            }        }        return product;    }        /**     * min of all values. If supplied array is zero length,     * Double.POSITIVE_INFINITY is returned.     * @param values     * @return     */    public static double min(double[] values) {        double min = Double.POSITIVE_INFINITY;        for (int i=0, iSize=values.length; i<iSize; i++) {            min = Math.min(min, values[i]);        }        return min;    }    /**     * min of all values. If supplied array is zero length,     * Double.NEGATIVE_INFINITY is returned.     * @param values     * @return     */    public static double max(double[] values) {        double max = Double.NEGATIVE_INFINITY;        for (int i=0, iSize=values.length; i<iSize; i++) {            max = Math.max(max, values[i]);        }        return max;    }    /**     * Note: this function is different from java.lang.Math.floor(..).     * <p>     * When n and s are "valid" arguments, the returned value is: Math.floor(n/s) * s;     * <br/>     * n and s are invalid if any of following conditions are true:     * <ul>     * <li>s is zero</li>     * <li>n is negative and s is positive</li>     * <li>n is positive and s is negative</li>     * </ul>     * In all such cases, Double.NaN is returned.     * @param n     * @param s     * @return     */    public static double floor(double n, double s) {        double f;                if ((n<0 && s>0) || (n>0 && s<0) || (s==0 && n!=0)) {            f = Double.NaN;        }        else {            f = (n==0 || s==0) ? 0 : Math.floor(n/s) * s;        }                return f;    }        /**     * Note: this function is different from java.lang.Math.ceil(..).     * <p>     * When n and s are "valid" arguments, the returned value is: Math.ceiling(n/s) * s;     * <br/>     * n and s are invalid if any of following conditions are true:     * <ul>     * <li>s is zero</li>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -