Example usage for java.math BigInteger divide

List of usage examples for java.math BigInteger divide

Introduction

In this page you can find the example usage for java.math BigInteger divide.

Prototype

public BigInteger divide(BigInteger val) 

Source Link

Document

Returns a BigInteger whose value is (this / val) .

Usage

From source file:com.example.util.FileUtils.java

/**
 * Returns a human-readable version of the file size, where the input represents a specific number of bytes.
 * <p>/*  w ww  . java2 s.c o  m*/
 * If the size is over 1GB, the size is returned as the number of whole GB, i.e. the size is rounded down to the
 * nearest GB boundary.
 * </p>
 * <p>
 * Similarly for the 1MB and 1KB boundaries.
 * </p>
 * 
 * @param size
 *            the number of bytes
 * @return a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes)
 * @see <a href="https://issues.apache.org/jira/browse/IO-226">IO-226 - should the rounding be changed?</a>
 * @since 2.4
 */
// See https://issues.apache.org/jira/browse/IO-226 - should the rounding be changed?
public static String byteCountToDisplaySize(BigInteger size) {
    String displaySize;

    if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
        displaySize = String.valueOf(size.divide(ONE_EB_BI)) + " EB";
    } else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
        displaySize = String.valueOf(size.divide(ONE_PB_BI)) + " PB";
    } else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) {
        displaySize = String.valueOf(size.divide(ONE_TB_BI)) + " TB";
    } else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
        displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB";
    } else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
        displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB";
    } else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
        displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB";
    } else {
        displaySize = String.valueOf(size) + " bytes";
    }
    return displaySize;
}

From source file:net.pms.util.Rational.java

/**
 * Returns an instance with the given {@code numerator} and
 * {@code denominator}./*from w w  w.j a v a  2s.c  om*/
 *
 * @param numerator the numerator.
 * @param denominator the denominator.
 * @return An instance that represents the value of {@code numerator}/
 *         {@code denominator}.
 */
@Nonnull
public static Rational valueOf(long numerator, long denominator) {
    if (numerator == 0 && denominator == 0) {
        return NaN;
    }
    if (denominator == 0) {
        return numerator > 0 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
    }
    if (numerator == 0) {
        return ZERO;
    }
    if (numerator == denominator) {
        return ONE;
    }
    BigInteger biNumerator;
    BigInteger biDenominator;
    if (denominator < 0) {
        biNumerator = BigInteger.valueOf(-numerator);
        biDenominator = BigInteger.valueOf(-denominator);
    } else {
        biNumerator = BigInteger.valueOf(numerator);
        biDenominator = BigInteger.valueOf(denominator);
    }
    long gcd = calculateGreatestCommonDivisor(numerator, denominator);
    BigInteger greatestCommonDivisor = BigInteger.valueOf(gcd);
    BigInteger reducedNumerator;
    BigInteger reducedDenominator;
    if (gcd == 1) {
        reducedNumerator = biNumerator;
        reducedDenominator = biDenominator;
    } else {
        reducedNumerator = biNumerator.divide(greatestCommonDivisor);
        reducedDenominator = biDenominator.divide(greatestCommonDivisor);
    }
    return new Rational(biNumerator, biDenominator, greatestCommonDivisor, reducedNumerator,
            reducedDenominator);
}

From source file:net.pms.util.Rational.java

/**
 * Returns an instance with the given {@code numerator} and
 * {@code denominator}.//from   ww  w .  jav  a  2 s . c  om
 *
 * @param numerator the numerator.
 * @param denominator the denominator.
 * @return An instance that represents the value of {@code numerator}/
 *         {@code denominator}.
 */
@Nullable
public static Rational valueOf(@Nullable BigInteger numerator, @Nullable BigInteger denominator) {
    if (numerator == null || denominator == null) {
        return null;
    }
    if (numerator.signum() == 0 && denominator.signum() == 0) {
        return NaN;
    }
    if (denominator.signum() == 0) {
        return numerator.signum() > 0 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
    }
    if (numerator.signum() == 0) {
        return ZERO;
    }
    if (numerator.equals(denominator)) {
        return ONE;
    }
    if (denominator.signum() < 0) {
        numerator = numerator.negate();
        denominator = denominator.negate();
    }

    BigInteger reducedNumerator;
    BigInteger reducedDenominator;
    BigInteger greatestCommonDivisor = calculateGreatestCommonDivisor(numerator, denominator);
    if (BigInteger.ONE.equals(greatestCommonDivisor)) {
        reducedNumerator = numerator;
        reducedDenominator = denominator;
    } else {
        reducedNumerator = numerator.divide(greatestCommonDivisor);
        reducedDenominator = denominator.divide(greatestCommonDivisor);
    }
    return new Rational(numerator, denominator, greatestCommonDivisor, reducedNumerator, reducedDenominator);
}

From source file:net.pms.util.Rational.java

/**
 * Used internally to find by which factor to multiply the reduced
 * numerators when comparing two {@link Rational}s.
 *
 * @param other the {@link Rational} to which this {@link Rational}'s value
 *            is to be compared.//from w  w  w  .j av  a 2 s.co  m
 * @return An array of {@link BigInteger} multipliers.
 * @throws ArithmeticException if either part is {@code NaN} or infinite.
 */
@Nonnull
protected BigInteger[] getMultipliers(@Nonnull Rational other) {
    if (isNaN() || isInfinite() || other.isNaN() || other.isInfinite()) {
        throw new ArithmeticException("Can't calculate multipliers for NaN or infinity");
    }
    BigInteger[] result = new BigInteger[2];
    BigInteger lcm = calculateLeastCommonMultiple(reducedDenominator, other.reducedDenominator);
    result[0] = lcm.divide(reducedDenominator);
    result[1] = lcm.divide(other.reducedDenominator);
    return result;
}

From source file:net.pms.util.Rational.java

/**
 * Returns a {@link Rational} whose value is {@code (this + value)}.
 *
 * @param value the value to be added to this {@link Rational}.
 * @return The addition result./*w w  w.j a v a  2 s  .com*/
 */
@Nullable
public Rational add(@Nullable Rational value) {
    if (value == null) {
        return null;
    }

    if (isNaN() || value.isNaN()) {
        return NaN;
    }

    if (value.numerator.signum() == 0) {
        return this;
    }

    if (this.numerator.signum() == 0) {
        return value;
    }

    if (isInfinite()) {
        if (value.isInfinite() && signum() != value.signum()) {
            return NaN; // Infinity minus infinity
        }
        return this;
    }

    BigInteger lcm = calculateLeastCommonMultiple(denominator, value.denominator);
    return valueOf(numerator.multiply(lcm.divide(denominator))
            .add(value.numerator.multiply(lcm.divide(value.denominator))), lcm);
}

From source file:net.pms.util.Rational.java

/**
 * Returns a {@link Rational} whose value is {@code (this * value)}.
 *
 * @param value the value to be multiplied by this {@link Rational}.
 * @return The multiplication result.//from   w  ww  . j  a  va2s.  c  o m
 */
@Nullable
public Rational multiply(@Nullable Rational value) {
    if (value == null) {
        return null;
    }
    if (isNaN() || value.isNaN()) {
        return NaN;
    }
    if (isInfinite() || value.isInfinite()) {
        if (signum() == 0 || value.signum() == 0) {
            return NaN; // Infinity by zero
        }
        return numerator.signum() == value.signum() ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
    }
    if (value.signum() == 0) {
        return ZERO;
    }

    if (value.numerator.abs().equals(value.denominator)) {
        return value.signum() < 0 ? this.negate() : this;
    }

    BigInteger newNumerator = reducedNumerator.multiply(value.reducedNumerator);
    BigInteger newDenominator = reducedDenominator.multiply(value.reducedDenominator);
    BigInteger gcd = newNumerator.gcd(newDenominator);
    return valueOf(newNumerator.divide(gcd), newDenominator.divide(gcd));
}

From source file:org.vafer.jdeb.Processor.java

/**
 * Build control archive of the deb//  w  ww .jav a2  s  .c o  m
 * @param pControlFiles
 * @param pDataSize
 * @param pChecksums
 * @param pOutput
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 * @throws ParseException
 */
private PackageDescriptor buildControl(final File[] pControlFiles, final BigInteger pDataSize,
        final StringBuffer pChecksums, final File pOutput) throws IOException, ParseException {

    PackageDescriptor packageDescriptor = null;

    final TarOutputStream outputStream = new TarOutputStream(
            new GZIPOutputStream(new FileOutputStream(pOutput)));
    outputStream.setLongFileMode(TarOutputStream.LONGFILE_GNU);

    for (int i = 0; i < pControlFiles.length; i++) {
        final File file = pControlFiles[i];

        if (file.isDirectory()) {
            continue;
        }

        final TarEntry entry = new TarEntry(file);

        final String name = file.getName();

        entry.setName("./" + name);
        entry.setNames("root", "root");
        entry.setMode(PermMapper.toMode("755"));

        if ("control".equals(name)) {
            packageDescriptor = new PackageDescriptor(new FileInputStream(file), resolver);

            if (packageDescriptor.get("Date") == null) {
                SimpleDateFormat fmt = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH); // Mon, 26 Mar 2007 11:44:04 +0200 (RFC 2822)
                // FIXME Is this field allowed in package descriptors ?
                packageDescriptor.set("Date", fmt.format(new Date()));
            }

            if (packageDescriptor.get("Distribution") == null) {
                packageDescriptor.set("Distribution", "unknown");
            }

            if (packageDescriptor.get("Urgency") == null) {
                packageDescriptor.set("Urgency", "low");
            }

            final String debFullName = System.getenv("DEBFULLNAME");
            final String debEmail = System.getenv("DEBEMAIL");

            if (debFullName != null && debEmail != null) {
                packageDescriptor.set("Maintainer", debFullName + " <" + debEmail + ">");
                console.println("Using maintainer from the environment variables.");
            }

            continue;
        }

        final InputStream inputStream = new FileInputStream(file);

        outputStream.putNextEntry(entry);

        Utils.copy(inputStream, outputStream);

        outputStream.closeEntry();

        inputStream.close();

    }

    if (packageDescriptor == null) {
        throw new FileNotFoundException("No control file in " + Arrays.toString(pControlFiles));
    }

    packageDescriptor.set("Installed-Size", pDataSize.divide(BigInteger.valueOf(1024)).toString());

    addEntry("control", packageDescriptor.toString(), outputStream);

    addEntry("md5sums", pChecksums.toString(), outputStream);

    outputStream.close();

    return packageDescriptor;
}

From source file:de.tudarmstadt.ukp.dkpro.lexsemresource.graph.EntityGraphJGraphT.java

/**
 * Computes and sets the diameter, the average degree and the average shortest path length of
 * the graph. Do not call this in the constructor. May run a while. It is called in the getters,
 * if parameters are not yet initialized when retrieved.
 *//*from   www  .  j av  a2s . com*/
private void setGraphParameters() {

    logger.info("Setting graph parameters.");
    // The directed graph is treated as an undirected graph to compute these parameters.
    // UndirectedGraph<String, DefaultEdge> undirectedGraph = new AsUndirectedGraph<String,
    // DefaultEdge>(directedGraph);
    logger.info("Treating the graph as undirected.");

    // Diameter is the maximum of all shortest path lengths
    // Average shortest path length is (as the name says) the average of the shortest path
    // length between all node pairs

    BigInteger bigMaxPathLength = BigInteger.valueOf(0);
    BigInteger bigShortestPathLengthSum = BigInteger.valueOf(0);
    double degreeSum = 0.0;
    double clusterCoefficientSum = 0.0;

    // iterate over all node pairs
    Set<Entity> nodes = undirectedGraph.vertexSet();

    // a hashset of the nodes which have been the start node of the computation process
    // for such nodes all path lengths have been already computed
    Set<Entity> wasSource = new HashSet<Entity>();

    int progress = 0;
    double percent = 0.0;
    for (Entity node : nodes) {

        progress++;
        percent = (double) progress / nodes.size() * 100;

        if (percent % 10 == 0) {
            logger.info("Progress: " + percent);
        }
        LoggingUtils.printProgressInfo(progress, nodes.size(), 100, LoggingUtils.ProgressInfoMode.TEXT,
                "Getting graph parameters");

        int nodeDegree = undirectedGraph.degreeOf(node);
        degreeSum += nodeDegree;

        // logger.info("Updating degree distribution.");
        updateDegreeDistribution(nodeDegree);

        // cluster coefficient C_v of a node v is the fraction of the connections that exist
        // between the
        // neighbor nodes (k_v) of this node and all allowable connections between the neighbors
        // (k_v(k_v -1)/2)
        // for degrees 0 or 1 there is no cluster coefficient, as there can be no connections
        // between neighbors
        if (nodeDegree > 1) {
            double numberOfNeighborConnections = getNumberOfNeighborConnections(node);
            clusterCoefficientSum += (numberOfNeighborConnections / (nodeDegree * (nodeDegree - 1)));
        }

        // Returns the new shortestPathLengthSum and the new maxPathLength.
        // They are returned as an double array for performance reasons.
        // I do not want to create an object, as this function is called *very* often
        // logger.info("Computing shortest path lengths.");
        BigInteger[] returnValues = computeShortestPathLengths(node, bigShortestPathLengthSum, bigMaxPathLength,
                wasSource);
        bigShortestPathLengthSum = returnValues[0];
        bigMaxPathLength = returnValues[1];

        // save the info that the node was already used as the source of path computation
        wasSource.add(node);
    }

    if (nodes.size() > 1) {
        long denominator = nodes.size() * (nodes.size() - 1) / 2;
        this.averageShortestPathLength = bigShortestPathLengthSum.divide(BigInteger.valueOf(denominator))
                .doubleValue();
        // sum of path lengths / (number of node pairs)
    } else {
        this.averageShortestPathLength = 0; // there is only one node
    }
    this.diameter = bigMaxPathLength.doubleValue();
    this.clusterCoefficient = clusterCoefficientSum / nodes.size();
}