All Projects → kiprobinson → BigFraction

kiprobinson / BigFraction

Licence: other
Java class that represents a fraction as a ratio of two BigIntegers, reduced to lowest terms.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to BigFraction

speedy-math
An application which allows user (small kids) to practice basic Mathematics operations
Stars: ✭ 28 (-20%)
Mutual labels:  math
alokmenghrajani.github.com
Alok Menghrajani's Blog
Stars: ✭ 64 (+82.86%)
Mutual labels:  math
bewl
A DSL for the internal language of a topos
Stars: ✭ 41 (+17.14%)
Mutual labels:  math
topologic
Visualiser for basic geometric primitives and fractals in arbitrary-dimensional spaces
Stars: ✭ 39 (+11.43%)
Mutual labels:  math
SCNMathExtensions
Math extensions for SCNVector3, SCNQuaternion, SCNMatrix4
Stars: ✭ 32 (-8.57%)
Mutual labels:  math
VsTeXCommentsExtension
TeX comments rendering inside Visual Studio.
Stars: ✭ 48 (+37.14%)
Mutual labels:  math
Math
考研数学,数学一,包括高等数学、线性代数、概率统计
Stars: ✭ 300 (+757.14%)
Mutual labels:  math
Fourier-and-Images
Fourier and Images
Stars: ✭ 81 (+131.43%)
Mutual labels:  math
langtons-ant
Langton’s Ant macOS screen saver written in Swift
Stars: ✭ 12 (-65.71%)
Mutual labels:  math
VSCode-LaTeX-Inkscape
✍️ A way to integrate LaTeX, VS Code, and Inkscape in macOS
Stars: ✭ 62 (+77.14%)
Mutual labels:  math
noteworthy
Markdown editor with bidirectional links and excellent math support, powered by ProseMirror. (In Development!)
Stars: ✭ 178 (+408.57%)
Mutual labels:  math
commons-statistics
Statistics
Stars: ✭ 35 (+0%)
Mutual labels:  math
fraction
provides approximate fractions in ruby
Stars: ✭ 20 (-42.86%)
Mutual labels:  fraction
d rive
c++17 compile time math(derivation/integration)
Stars: ✭ 16 (-54.29%)
Mutual labels:  math
abacus
📐 C# cross precision 3D maths library.
Stars: ✭ 35 (+0%)
Mutual labels:  math
mml-book-chinese
mml-book-chinese《Mathematics For Machine Learning》机器学习中的数学 中文版
Stars: ✭ 113 (+222.86%)
Mutual labels:  math
Upsurge
Multi-dimensional Swift math
Stars: ✭ 180 (+414.29%)
Mutual labels:  math
react-katex
Display math in TeX with KaTeX and ReactJS
Stars: ✭ 135 (+285.71%)
Mutual labels:  math
class
Rust library for building IQC: cryptography based on class groups of imaginary quadratic orders
Stars: ✭ 54 (+54.29%)
Mutual labels:  math
perl-scripts
A nice collection of day-to-day Perl scripts.
Stars: ✭ 92 (+162.86%)
Mutual labels:  math

BigFraction

Java library that represents a fraction as a ratio of two BigIntegers, reduced to lowest terms. A rich set of mathematical functions are supported.

Creating BigFractions

Constructors are protected. Create new BigFractions using valueOf(Number numerator), valueOf(Number numerator, Number denominator), or valueOf(String):

BigFraction.valueOf(11);      // 11/1
BigFraction.valueOf(11, 17);  // 11/17
BigFraction.valueOf("19/81"); // 19/81

Fractions are always reduced to lowest terms:

BigFraction.valueOf(17, 34);  // 1/2
BigFraction.valueOf(0, 999);  // 0/1

The sign is always carried by the numerator:

BigFraction.valueOf(-9, 4);   // -9/4
BigFraction.valueOf(9, -4);   // -9/4
BigFraction.valueOf(-9, -4);  // 9/4  (negatives cancel out)

You can use floating point numbers to create the fraction:

BigFraction.valueOf(0.625);  // 5/8
BigFraction.valueOf(-8.5, 6.25); //-34/25

But be careful, what you get is exactly equal to the value you provide:

BigFraction.valueOf(1.1);  // 2476979795053773/2251799813685248
BigFraction.valueOf(1.1f); // 9227469/8388608

The version that takes a String may be more like what you expect:

BigFraction.valueOf("1.1");                // 11/10
BigFraction.valueOf(Double.toString(1.1)); // 11/10
BigFraction.valueOf(Float.toString(1.1f)); // 11/10

You can also use BigInteger and BigDecimal:

BigFraction.valueOf(new BigInteger("9999999999999999999"), BigInteger.valueOf(1));
// ->  9999999999999999999/1   (note that this is larger than Long.MAX_VALUE)

BigFraction.valueOf(new BigDecimal("1.23456789012345678901E-50"));
// ->  123456789012345678901/10000000000000000000000000000000000000000000000000000000000000000000000

You can even mix different Number types for numerator and denominator:

BigFraction.valueOf(1.5, BigInteger.valueOf(17)); // 3/34

A few exceptions:

BigFraction.valueOf(1,0); // ArithmeticException - divide by zero
BigFraction.valueOf(0,0); // ArithmeticException - divide by zero
BigFraction.valueOf(Double.POSITIVE_INFINITY); //IllegalArgumentException
BigFraction.valueOf(Double.NaN); //IllegalArgumentException

Mathematical Operations

BigFraction a = BigFraction.valueOf(1,2);
BigFraction b = BigFraction.valueOf(3,4);
BigFraction z = BigFraction.ZERO;

a.add(a); // 1/2 + 1/2 = 1/1
a.add(b); // 1/2 + 3/4 = 5/4
b.add(z); // 3/4 + 0/1 = 3/4
z.add(a); // 0/1 + 1/2 = 1/2

a.subtract(a); // 1/2 - 1/2 = 0/1
a.subtract(b); // 1/2 - 3/4 = -1/4
b.subtract(z); // 3/4 - 0/1 = 3/4
z.subtract(a); // 0/1 - 1/2 = -1/2

a.multiply(a); // (1/2) * (1/2) = 1/4
a.multiply(b); // (1/2) * (3/4) = 3/8
b.multiply(z); // (3/4) * (0/1) = 0/1
z.multiply(a); // (0/1) * (1/2) = 0/1

a.divide(a); // (1/2) / (1/2) = 1/1
a.divide(b); // (1/2) / (3/4) = 2/3
b.divide(z); // => ArithmeticException - divide by zero
z.divide(a); // (0/1) / (1/2) = 0/1

a.pow(3);  // (1/2)^3 = 1/8
b.pow(4);  // (3/4)^4 = 81/256
a.pow(0);  // (1/2)^0 = 1/1
z.pow(0);  // (0/1)^0 = 1/1  => Mathematicians may not like it, but this is consistent with Math.pow()

a.pow(-3); // (1/2)^(-3) = 8/1
b.pow(-4); // (3/4)^(-4) = 256/81
z.pow(-1); // => ArithmeticException (divide by zero)

a.reciprocal(); // (1/2)^(-1) = 2/1
b.reciprocal(); // (3/4)^(-1) = 4/3
z.reciprocal(); // => ArithmeticException (divide by zero)

Complement is 1 - n. Useful in statistics a lot:

a.complement(); // 1 - 1/2 = 1/2
b.complement(); // 1 - 3/4 = 1/4
z.complement(); // 1 - 0/1 = 1/1

LongFraction

There is also a LongFraction that represents a fraction as a ratio of two longs, with the same methods as BigFraction. The mathematics are much faster, but you run the risk of overflows.

How to Get BigFraction

The library is available via Maven Central and can be used in any project built using Maven or a compatible build tool (Ivy, Gradle, etc.).

You can also download the jar file from the repository and manually add it to your project, if you are not utilizing a supported build tool.

Support for Older Java Versions

I am currently building my jar files using Java 8, with source compatibility only for Java 8+. These jar files will not run in older versions of Java. If you need support for Java 6 or Java 7, notice that there are -java6 or -java7 versions available from Maven. These are compiled with compatibility to that Java version.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].