Example usage for org.apache.commons.math3.fraction Fraction divide

List of usage examples for org.apache.commons.math3.fraction Fraction divide

Introduction

In this page you can find the example usage for org.apache.commons.math3.fraction Fraction divide.

Prototype

public Fraction divide(final int i) 

Source Link

Document

Divide the fraction by an integer.

Usage

From source file:unikl.disco.misc.NumberObj.java

public static NumberObj div(NumberObj num1, NumberObj num2) {
    if (performTypeChecks) {
        TypeCheck(num1, num2);/*from   w  w  w  .j  av a 2s. c  om*/
    }

    if (num1.equals(NaN) || num2.equals(NaN)) {
        return NaN;
    }
    // Integer based number representations use Integer.MAX_VALUE to signal infinity so special treatment is necessary when dividing
    if (num1.equals(POSITIVE_INFINITY)) {
        return POSITIVE_INFINITY;
    }
    if (num2.equals(POSITIVE_INFINITY)) {
        return new NumberObj(0.0);
    }
    if (num1.equals(NEGATIVE_INFINITY)) {
        return NEGATIVE_INFINITY;
    }
    if (num2.equals(NEGATIVE_INFINITY)) {
        return new NumberObj(0.0);
    }

    switch (num1.getType()) {
    case DOUBLE:
        Double double1 = (Double) num1.getValue();
        Double double2 = (Double) num2.getValue();
        return new NumberObj(double1.doubleValue() / double2.doubleValue());
    case RATIONAL:
        Fraction frac2 = (Fraction) num2.getValue();
        if (frac2.getNumerator() == 0) {
            return getPosInfinity();
            //              return getNaN();
        } else {
            Fraction frac1 = (Fraction) num1.getValue();
            return new NumberObj(frac1.divide(frac2));
        }
    default:
        return null;
    }
}