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

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

Introduction

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

Prototype

public Fraction subtract(final int i) 

Source Link

Document

Subtract an integer from the fraction.

Usage

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

public static NumberObj sub(NumberObj num1, NumberObj num2) {
    if (performTypeChecks) {
        TypeCheck(num1, num2);/*from   w  w w.  j  a  va  2s . c  o m*/
    }

    if (num1.equals(NaN) || num2.equals(NaN)) {
        return NaN;
    }
    // prevent overflow exception when adding integer based number representations like Fraction
    if (num1.equals(POSITIVE_INFINITY) || num2.equals(POSITIVE_INFINITY)) {
        return POSITIVE_INFINITY;
    }
    if (num1.equals(NEGATIVE_INFINITY) || num2.equals(NEGATIVE_INFINITY)) {
        return NEGATIVE_INFINITY;
    }

    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 frac1 = (Fraction) num1.getValue();
        Fraction frac2 = (Fraction) num2.getValue();
        // may throw MathArithmeticException due to integer overflow
        return new NumberObj(frac1.subtract(frac2));
    default:
        return null;
    }
}