nl.knowledgeplaza.math
Class Fraction

java.lang.Object
  extended by java.lang.Number
      extended by nl.knowledgeplaza.math.Fraction
All Implemented Interfaces:
Serializable, Cloneable, Comparable

public class Fraction
extends Number
implements Comparable, Cloneable

This class tries to mimick the calculating as we to it in school and not on computers. So 0.5 is 1/2 and 0.333333... does not exist but is 1/3. In this way it tries to remove those strange behaviors like zero not being equal to zero (0.00 != 0). This is especially frustrating when using binding libraries. And it tries to remove as much rounding as possible, thus minimizing those calculation errors. By only rounding when presenting the final result, not during calculation.

Author:
tbee
See Also:
Serialized Form

Field Summary
static Fraction ONE
           
static Fraction ZERO
           
 
Constructor Summary
Fraction()
          Default is zero
Fraction(BigDecimal value)
           
Fraction(BigInteger value)
           
Fraction(BigInteger bi1, BigInteger bi2)
           
Fraction(double value)
           
Fraction(float value)
           
Fraction(Fraction f)
           
Fraction(int value)
           
Fraction(int value, int divider)
           
Fraction(long value)
           
Fraction(String value)
           
 
Method Summary
 Fraction abs()
          positive value
 Fraction add(BigDecimal other)
           
 Fraction add(BigInteger other)
           
 Fraction add(Fraction other)
           
 String asString()
          Simplify and return as fraction string notation, e.g.
 String asString2()
          Simplify and return as integer+(fraction) string notation, e.g 1+(2/3)
 String asStringRaw()
          Return as fraction string notation, e.g.
 BigDecimal bigDecimalValue()
          Return a BigDecimal with scale 10 and rounding half up
 BigDecimal bigDecimalValue(int scale, int rounding)
          Return a big decimal with the specified scale (if the conversion requires a divide)
 BigInteger bigIntegerValue()
          Return a BigInteger
 BigInteger bigIntegerValueExact()
          Return a BigInteger
 Object clone()
           
 int compareTo(Object o)
          Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object
 Fraction div(BigDecimal other)
           
 Fraction div(BigInteger other)
           
 Fraction div(Fraction an)
           
 Fraction divide(BigDecimal other)
           
 Fraction divide(BigInteger other)
           
 Fraction divide(Fraction other)
           
 double doubleValue()
           
 boolean equals(Object an)
           
 float floatValue()
           
 Fraction fraction()
          Get only the fraction bit
 void fractionalize(int precision)
          TODO: Find the nearest fraction e.g.
 BigInteger getDenominator()
           
 BigInteger getDivider()
           
 BigInteger getNumerator()
           
 BigInteger getValue()
           
 Fraction integer()
          Lose the fraction bit
 int intValue()
           
 Fraction inverse()
          Swap the values: 1/2 => 2/1
 boolean isZero()
          is zero
 long longValue()
           
 Fraction mul(BigDecimal other)
           
 Fraction mul(BigInteger other)
           
 Fraction mul(Fraction an)
           
 Fraction multiply(BigDecimal other)
           
 Fraction multiply(BigInteger other)
           
 Fraction multiply(Fraction other)
           
 Fraction neg()
           
 Fraction negate()
          current value * -1
 Fraction not()
          This method returns a negative value if and only if this number is non-negative.
 Fraction pow(int an)
           
 Fraction power(int exponent)
           
 int sgn()
          positive value
 Fraction shallowClone()
           
 Fraction sub(BigDecimal other)
           
 Fraction sub(BigInteger other)
           
 Fraction sub(Fraction an)
           
 Fraction substract(BigDecimal other)
           
 Fraction substract(BigInteger other)
           
 Fraction substract(Fraction other)
           
 String toString()
          Return this value as a decimal string notation (e.g.
static Fraction valueOf(BigDecimal value)
           
static Fraction valueOf(BigInteger value)
           
static Fraction valueOf(Double value)
           
static Fraction valueOf(Float value)
           
static Fraction valueOf(Integer value)
           
static Fraction valueOf(Long value)
           
static Fraction valueOf(Short value)
           
static Fraction valueOf(String value)
           
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Fraction ZERO

ONE

public static final Fraction ONE
Constructor Detail

Fraction

public Fraction()
Default is zero


Fraction

public Fraction(int value)

Fraction

public Fraction(long value)

Fraction

public Fraction(BigDecimal value)

Fraction

public Fraction(BigInteger value)

Fraction

public Fraction(double value)

Fraction

public Fraction(float value)

Fraction

public Fraction(String value)

Fraction

public Fraction(int value,
                int divider)

Fraction

public Fraction(BigInteger bi1,
                BigInteger bi2)

Fraction

public Fraction(Fraction f)
Method Detail

getValue

public BigInteger getValue()

getNumerator

public BigInteger getNumerator()

getDivider

public BigInteger getDivider()

getDenominator

public BigInteger getDenominator()

add

public Fraction add(Fraction other)

add

public Fraction add(BigInteger other)

add

public Fraction add(BigDecimal other)

substract

public Fraction substract(Fraction other)

substract

public Fraction substract(BigInteger other)

substract

public Fraction substract(BigDecimal other)

sub

public Fraction sub(Fraction an)

sub

public Fraction sub(BigInteger other)

sub

public Fraction sub(BigDecimal other)

multiply

public Fraction multiply(Fraction other)

multiply

public Fraction multiply(BigInteger other)

multiply

public Fraction multiply(BigDecimal other)

mul

public Fraction mul(Fraction an)

mul

public Fraction mul(BigInteger other)

mul

public Fraction mul(BigDecimal other)

divide

public Fraction divide(Fraction other)

divide

public Fraction divide(BigInteger other)

divide

public Fraction divide(BigDecimal other)

div

public Fraction div(Fraction an)

div

public Fraction div(BigInteger other)

div

public Fraction div(BigDecimal other)

power

public Fraction power(int exponent)

pow

public Fraction pow(int an)

abs

public Fraction abs()
positive value


sgn

public int sgn()
positive value


negate

public Fraction negate()
current value * -1


neg

public Fraction neg()

integer

public Fraction integer()
Lose the fraction bit


fraction

public Fraction fraction()
Get only the fraction bit


not

public Fraction not()
This method returns a negative value if and only if this number is non-negative.


inverse

public Fraction inverse()
Swap the values: 1/2 => 2/1


fractionalize

public void fractionalize(int precision)
TODO: Find the nearest fraction e.g. 0.333333333433443 = 1/3


clone

public Object clone()
Overrides:
clone in class Object

shallowClone

public Fraction shallowClone()

isZero

public boolean isZero()
is zero

Returns:

equals

public boolean equals(Object an)
Overrides:
equals in class Object

compareTo

public int compareTo(Object o)
Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object

Specified by:
compareTo in interface Comparable

toString

public String toString()
Return this value as a decimal string notation (e.g. 1.333333333333333333333333333333333333333333333333333333...) This method uses the bigDecimalValue() method to generate the decimal (and thus uses the default scale and rounding) and then strips any postfixing zero's so 1/2 becomes 0.5 instead of 0.50000000... toString returns decimal notation because all number classes do this. Use asString to get the correct (unrounded value).

Overrides:
toString in class Object

asStringRaw

public String asStringRaw()
Return as fraction string notation, e.g. 1/3 (no simplify)


asString

public String asString()
Simplify and return as fraction string notation, e.g. 1/3


asString2

public String asString2()
Simplify and return as integer+(fraction) string notation, e.g 1+(2/3)


bigDecimalValue

public BigDecimal bigDecimalValue(int scale,
                                  int rounding)
Return a big decimal with the specified scale (if the conversion requires a divide)


bigDecimalValue

public BigDecimal bigDecimalValue()
Return a BigDecimal with scale 10 and rounding half up


bigIntegerValue

public BigInteger bigIntegerValue()
Return a BigInteger


bigIntegerValueExact

public BigInteger bigIntegerValueExact()
Return a BigInteger


doubleValue

public double doubleValue()
Specified by:
doubleValue in class Number

floatValue

public float floatValue()
Specified by:
floatValue in class Number

intValue

public int intValue()
Specified by:
intValue in class Number

longValue

public long longValue()
Specified by:
longValue in class Number

valueOf

public static Fraction valueOf(String value)

valueOf

public static Fraction valueOf(BigDecimal value)

valueOf

public static Fraction valueOf(BigInteger value)

valueOf

public static Fraction valueOf(Short value)

valueOf

public static Fraction valueOf(Integer value)

valueOf

public static Fraction valueOf(Long value)

valueOf

public static Fraction valueOf(Double value)

valueOf

public static Fraction valueOf(Float value)


Copyright © 2011 KnowledgePlaza. All Rights Reserved.