Here you can find the source of roundDivide(double[] dividend, double[] divisor, int scale)
Parameter | Description |
---|---|
dividend | adjustment |
divisor | adjustment |
Parameter | Description |
---|---|
IllegalArgumentException | if the two argument arrays are not the same length |
public static double[] roundDivide(double[] dividend, double[] divisor, int scale)
//package com.java2s; /* --------------------------------------------------------------------- * Numenta Platform for Intelligent Computing (NuPIC) * Copyright (C) 2014, Numenta, Inc. Unless you have an agreement * with Numenta, Inc., for a separate license for this software code, the * following terms and conditions apply: * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Affero Public License for more details. * * You should have received a copy of the GNU Affero Public License * along with this program. If not, see http://www.gnu.org/licenses. * * http://numenta.org/licenses///from w w w. j a v a 2 s. com * --------------------------------------------------------------------- */ import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; public class Main { /** * Returns an array whose members are the quotient of the dividend array * values and the divisor array values. * * @param dividend * @param divisor * @param dividend adjustment * @param divisor adjustment * @return * @throws IllegalArgumentException if the two argument arrays are not the same length */ public static double[] roundDivide(double[] dividend, double[] divisor, int scale) { if (dividend.length != divisor.length) { throw new IllegalArgumentException("The dividend array and the divisor array must be the same length"); } double[] quotient = new double[dividend.length]; for (int i = 0; i < dividend.length; i++) { quotient[i] = (dividend[i]) / (divisor[i] == 0 ? 1 : divisor[i]); //Protect against division by 0 quotient[i] = new BigDecimal(quotient[i]).round(new MathContext(scale, RoundingMode.HALF_UP)) .doubleValue(); } return quotient; } }