Simple Java Rational is a lightweight, arbitrary precision, rational arithmetic library, with a MIT license. I was inspired to write it after observing that Apache’s and JScience’s implementations are packaged in jars that include ~700kb to ~2mb of classes (as of December 2015), which unnecessarily added bulk to the program I was writing.

This version compiles to a 8kb class file or a 5kb jar file.

Links

  1. License
  2. Download
  3. Example
  4. JavaDocs
  5. Development Branch on Gitlab

License

The MIT License (MIT)

Copyright (c) 2015 Elliott Karpilovsky

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Download

The simple-java-rational.zip file contains the JAR file for easy use. The simple-java-rational-src.zip file contains the source code for generating the JAR file, or the class file by itself. It requires ant to build and junit4 for tests.

The MD5 sums are provided as a quick check that the file downloaded properly. However, it does not validate the download. Verify by using the signatures.

Example

import com.ekarp.math.Rational;
import java.math.BigDecimal;
import java.math.BigInteger;

public class Example {

    public static void main(String[] args) {
        // Constants 0 and 1 are pre-defined.
        System.out.println(Rational.ZERO.toString());  // Prints 0.
        System.out.println(Rational.ONE.toString());  // Prints 1.

        // Define rationals using big integers.
        Rational threeQuarters = new Rational(new BigInteger("3"),
                                              new BigInteger("4"));
        System.out.println(threeQuarters.toString()); // Prints 3/4.

        // Has constructors for longs, strings, and BigDecimals.
        threeQuarters = new Rational(3, 4);
        System.out.println(threeQuarters.toString()); // Prints 3/4.

        threeQuarters =  new Rational("3/4");
        System.out.println(threeQuarters.toString()); // Prints 3/4.

        threeQuarters = new Rational(new BigDecimal("0.75"));
        System.out.println(threeQuarters.toString()); // Prints 3/4.

        // Rationals with a divisor of 1 are printed without the denominator.
        Rational reduced = new Rational(1000, 1);
        System.out.println(reduced.toString()); // Prints 1000.

        // Common factors are removed.
        Rational factored = new Rational(120, 240);
        System.out.println(factored.toString()); // Prints 1/2.

        // Standard arithmetic is available.
        Rational r = threeQuarters.subtract(reduced).multiply(factored).add(
            threeQuarters); // ((3/4 - 1000) * 1/2) + 3/4 = -3991/8.
        // Prints -3991, the numerator always carries the sign.
        System.out.printf("Numerator: %s\n", r.numerator());
        // Prints 8.
        System.out.printf("Denominator: %s\n", r.denominator());
    }
}