Java Double Number Divide divide(double first, double second)

Here you can find the source of divide(double first, double second)

Description

Divide the first parameter by the second parameter

License

LGPL

Parameter

Parameter Description

Return

double, the result from dividing the first number by the second number.

Declaration

public static double divide(double first, double second) 

Method Source Code

//package com.java2s;
/********************************************************************************
 *                                                                              *
 *  (c) Copyright 2010 Verizon Communications USA and The Open University UK    *
 *                                                                              *
 *  This software is freely distributed in accordance with                      *
 *  the GNU Lesser General Public (LGPL) license, version 3 or later            *
 *  as published by the Free Software Foundation.                               *
 *  For details see LGPL: http://www.fsf.org/licensing/licenses/lgpl.html       *
 *               and GPL: http://www.fsf.org/licensing/licenses/gpl-3.0.html    *
 *                                                                              *
 *  This software is provided by the copyright holders and contributors "as is" *
 *  and any express or implied warranties, including, but not limited to, the   *
 *  implied warranties of merchantability and fitness for a particular purpose  *
 *  are disclaimed. In no event shall the copyright owner or contributors be    *
 *  liable for any direct, indirect, incidental, special, exemplary, or         *
 *  consequential damages (including, but not limited to, procurement of        *
 *  substitute goods or services; loss of use, data, or profits; or business    *
 *  interruption) however caused and on any theory of liability, whether in     *
 *  contract, strict liability, or tort (including negligence or otherwise)     *
 *  arising in any way out of the use of this software, even if advised of the  *
 *  possibility of such damage.                                                 *
 *                                                                              *
 ********************************************************************************/

public class Main {
    private static double minDoubleValue = 100.0 * Float.MIN_VALUE;
    private static double epsilon = 10.0 * 1.192092896e-07f;

    /**//from   w  ww.jav a 2  s.  c o m
     * Divide the first parameter by the second parameter
     *
     * @param double first, the number to divide into.
     * @param double second, the number to divide by
     * @return double, the result from dividing the first number by the second number.
     */
    public static double divide(double first, double second) {

        int limit = 1;
        double sign = 1.0;
        if ((first > 0.0 && second < 0.0) || (first < 0.0 && second > 0.0)) {
            sign = -1.0;
        }

        first = Math.abs(first);
        second = Math.abs(second);

        if (second >= 1.0 || first < second * 0.1 * Float.MAX_VALUE) {
            return sign * first / second;
        } else if (compare(first, second) == 0) {
            if (first < 0.)
                first = -first;
            if (first < minDoubleValue) {
                return limit;
            } else {
                return sign;
            }
        } else {
            return sign * 0.1 * 0.1 * Float.MAX_VALUE;
        }
    }

    private static int compare(double r1, double r2) {
        double ar1 = r1;
        double ar2 = r2;
        if (ar1 < 0.)
            ar1 = -ar1;
        if (ar2 < 0.)
            ar2 = -ar2;
        if (ar1 < minDoubleValue && ar2 < minDoubleValue)
            return 0;

        double er1 = epsilon * ar1;
        double er2 = epsilon * ar2;
        if (r1 - er1 > r2 + er2)
            return 1;
        else if (r1 + er1 < r2 - er2)
            return -1;
        else
            return 0;
    }
}

Related

  1. divergenceKL(double... a)
  2. divHex(double num)
  3. divide(Double a, Double b)
  4. divide(double a, double b)
  5. divide(Double a, Double b)
  6. divide(double left, double right)
  7. divide(Double numerator, Double denominator)
  8. divide(double v1, double v2)
  9. divide(double x, double y, int scale, RoundingMode roundingMode)