nl.knowledgeplaza.math
Class Fraction

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

public class Fraction
extends java.lang.Number
implements java.lang.Comparable, java.lang.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(java.math.BigDecimal value)
           
Fraction(java.math.BigInteger value)
           
Fraction(java.math.BigInteger bi1, java.math.BigInteger bi2)
           
Fraction(double value)
           
Fraction(float value)
           
Fraction(Fraction f)
           
Fraction(int value)
           
Fraction(int value, int divider)
           
Fraction(long value)
           
Fraction(java.lang.String value)
           
 
Method Summary
 Fraction abs()
          positive value
 Fraction add(Fraction other)
           
 java.lang.String asString()
          Simplify and return as fraction string notation, e.g.
 java.lang.String asString2()
          Simplify and return as integer+(fraction) string notation, e.g 1+(2/3)
 java.lang.String asStringRaw()
          Return as fraction string notation, e.g.
 java.math.BigDecimal bigDecimalValue()
          Return a BigDecimal with scale 10 and rounding half up
 java.math.BigDecimal bigDecimalValue(int scale, int rounding)
          Return a big decimal with the specified scale (if the conversion requires a divide)
 java.lang.Object clone()
           
 int compareTo(java.lang.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(Fraction an)
           
 Fraction divide(Fraction other)
           
 double doubleValue()
           
 boolean equals(java.lang.Object an)
           
 float floatValue()
           
 Fraction fraction()
          Get only the fraction bit
 void fractionalize(int precision)
          TODO: Find the nearest fraction e.g.
 java.math.BigInteger getDenominator()
           
 java.math.BigInteger getDivider()
           
 java.math.BigInteger getNumerator()
           
 java.math.BigInteger getValue()
           
 Fraction integer()
          Lose the fraction bit
 int intValue()
           
 Fraction inverse()
          Swap the values: 1/2 => 2/1
 long longValue()
           
 Fraction mul(Fraction an)
           
 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(Fraction an)
           
 Fraction substract(Fraction other)
           
 java.lang.String toString()
          Return this value as a decimal string notation (e.g.
static Fraction valueOf(java.math.BigDecimal value)
           
static Fraction valueOf(java.math.BigInteger value)
           
static Fraction valueOf(java.lang.Double value)
           
static Fraction valueOf(java.lang.Float value)
           
static Fraction valueOf(java.lang.Integer value)
           
static Fraction valueOf(java.lang.Long value)
           
static Fraction valueOf(java.lang.Short value)
           
static Fraction valueOf(java.lang.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(java.math.BigDecimal value)

Fraction

public Fraction(java.math.BigInteger value)

Fraction

public Fraction(double value)

Fraction

public Fraction(float value)

Fraction

public Fraction(java.lang.String value)

Fraction

public Fraction(int value,
                int divider)

Fraction

public Fraction(java.math.BigInteger bi1,
                java.math.BigInteger bi2)

Fraction

public Fraction(Fraction f)
Method Detail

getValue

public java.math.BigInteger getValue()

getNumerator

public java.math.BigInteger getNumerator()

getDivider

public java.math.BigInteger getDivider()

getDenominator

public java.math.BigInteger getDenominator()

add

public Fraction add(Fraction other)

substract

public Fraction substract(Fraction other)

sub

public Fraction sub(Fraction an)

multiply

public Fraction multiply(Fraction other)

mul

public Fraction mul(Fraction an)

divide

public Fraction divide(Fraction other)

div

public Fraction div(Fraction an)

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 java.lang.Object clone()
Overrides:
clone in class java.lang.Object

shallowClone

public Fraction shallowClone()

equals

public boolean equals(java.lang.Object an)
Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(java.lang.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 java.lang.Comparable

toString

public java.lang.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 java.lang.Object

asStringRaw

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


asString

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


asString2

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


bigDecimalValue

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


bigDecimalValue

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


doubleValue

public double doubleValue()
Specified by:
doubleValue in class java.lang.Number

floatValue

public float floatValue()
Specified by:
floatValue in class java.lang.Number

intValue

public int intValue()
Specified by:
intValue in class java.lang.Number

longValue

public long longValue()
Specified by:
longValue in class java.lang.Number

valueOf

public static Fraction valueOf(java.lang.String value)

valueOf

public static Fraction valueOf(java.math.BigDecimal value)

valueOf

public static Fraction valueOf(java.math.BigInteger value)

valueOf

public static Fraction valueOf(java.lang.Short value)

valueOf

public static Fraction valueOf(java.lang.Integer value)

valueOf

public static Fraction valueOf(java.lang.Long value)

valueOf

public static Fraction valueOf(java.lang.Double value)

valueOf

public static Fraction valueOf(java.lang.Float value)


Copyright © 2010 KnowledgePlaza. All Rights Reserved.