org.spaceroots.mantissa.algebra
Class RationalNumber

java.lang.Object
  extended byorg.spaceroots.mantissa.algebra.RationalNumber
All Implemented Interfaces:
Serializable

public class RationalNumber
extends Object
implements Serializable

This class implements reduced rational numbers.

Instances of this class are immutable.

Version:
$Id: RationalNumber.java 1711 2006-12-13 21:27:51Z luc $
Author:
L. Maisonobe
See Also:
Serialized Form

Field Summary
static RationalNumber ONE
          One as a rational numer.
static RationalNumber ZERO
          Zero as a rational numer.
 
Constructor Summary
RationalNumber()
          Simple constructor.
RationalNumber(BigInteger i)
          Simple constructor.
RationalNumber(BigInteger numerator, BigInteger denominator)
          Simple constructor.
RationalNumber(long l)
          Simple constructor.
RationalNumber(long numerator, long denominator)
          Simple constructor.
 
Method Summary
static RationalNumber abs(RationalNumber r)
          Get the absolute value of a rational number.
 RationalNumber add(BigInteger l)
          Add an integer to the instance.
 RationalNumber add(long l)
          Add an integer to the instance.
 RationalNumber add(RationalNumber r)
          Add a rational number to the instance.
 RationalNumber divide(BigInteger l)
          Divide the instance by an integer.
 RationalNumber divide(long l)
          Divide the instance by an integer.
 RationalNumber divide(RationalNumber r)
          Divide the instance by a rational number.
 double doubleValue()
          Return the double value of the instance.
 boolean equals(Object o)
          Check if the instance is equal to another rational number.
 BigInteger getDenominator()
          Get the denominator.
 BigInteger getNumerator()
          Get the numerator.
 int hashCode()
          Returns a hash code value for the object.
 RationalNumber invert()
          Invert the instance.
 boolean isInteger()
          Check if the number is integer.
 boolean isNegative()
          Check if the number is negative.
 boolean isOne()
          Check if the number is one.
 boolean isZero()
          Check if the number is zero.
 RationalNumber multiply(BigInteger l)
          Multiply the instance by an integer.
 RationalNumber multiply(long l)
          Multiply the instance by an integer.
 RationalNumber multiply(RationalNumber r)
          Multiply the instance by a rational number.
 RationalNumber negate()
          Negate the instance.
 RationalNumber subtract(BigInteger l)
          Subtract an integer from the instance.
 RationalNumber subtract(long l)
          Subtract an integer from the instance.
 RationalNumber subtract(RationalNumber r)
          Subtract a rational number from the instance.
 String toString()
          Returns a string representation of the rational number.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final RationalNumber ZERO
Zero as a rational numer.


ONE

public static final RationalNumber ONE
One as a rational numer.

Constructor Detail

RationalNumber

public RationalNumber()
Simple constructor. Build a null rational number


RationalNumber

public RationalNumber(long numerator,
                      long denominator)
Simple constructor. Build a rational number from a numerator and a denominator.

Parameters:
numerator - numerator of the rational number
denominator - denominator of the rational number
Throws:
ArithmeticException - if the denominator is zero

RationalNumber

public RationalNumber(BigInteger numerator,
                      BigInteger denominator)
Simple constructor. Build a rational number from a numerator and a denominator.

Parameters:
numerator - numerator of the rational number
denominator - denominator of the rational number
Throws:
ArithmeticException - if the denominator is zero

RationalNumber

public RationalNumber(long l)
Simple constructor. Build a rational number from a single integer

Parameters:
l - value of the rational number

RationalNumber

public RationalNumber(BigInteger i)
Simple constructor. Build a rational number from a single integer

Parameters:
i - value of the rational number
Method Detail

negate

public RationalNumber negate()
Negate the instance.

Returns:
a new rational number, opposite to the isntance

add

public RationalNumber add(long l)
Add an integer to the instance.

Parameters:
l - integer to add
Returns:
a new rational number which is the sum of the instance and l

add

public RationalNumber add(BigInteger l)
Add an integer to the instance.

Parameters:
l - integer to add
Returns:
a new rational number which is the sum of the instance and l

add

public RationalNumber add(RationalNumber r)
Add a rational number to the instance.

Parameters:
r - rational number to add
Returns:
a new rational number which is the sum of the instance and r

subtract

public RationalNumber subtract(long l)
Subtract an integer from the instance.

Parameters:
l - integer to subtract
Returns:
a new rational number which is the difference the instance minus l

subtract

public RationalNumber subtract(BigInteger l)
Subtract an integer from the instance.

Parameters:
l - integer to subtract
Returns:
a new rational number which is the difference the instance minus l

subtract

public RationalNumber subtract(RationalNumber r)
Subtract a rational number from the instance.

Parameters:
r - rational number to subtract
Returns:
a new rational number which is the difference the instance minus r

multiply

public RationalNumber multiply(long l)
Multiply the instance by an integer.

Parameters:
l - integer to multiply by
Returns:
a new rational number which is the produc of the instance by l

multiply

public RationalNumber multiply(BigInteger l)
Multiply the instance by an integer.

Parameters:
l - integer to multiply by
Returns:
a new rational number which is the produc of the instance by l

multiply

public RationalNumber multiply(RationalNumber r)
Multiply the instance by a rational number.

Parameters:
r - rational number to multiply the instance with
Returns:
a new rational number which is the product of the instance and r

divide

public RationalNumber divide(long l)
Divide the instance by an integer.

Parameters:
l - integer to divide by
Returns:
a new rational number which is the quotient of the instance by l
Throws:
ArithmeticException - if l is zero

divide

public RationalNumber divide(BigInteger l)
Divide the instance by an integer.

Parameters:
l - integer to divide by
Returns:
a new rational number which is the quotient of the instance by l
Throws:
ArithmeticException - if l is zero

divide

public RationalNumber divide(RationalNumber r)
Divide the instance by a rational number.

Parameters:
r - rational number to divide by
Returns:
a new rational number which is the quotient of the instance by r
Throws:
ArithmeticException - if r is zero

invert

public RationalNumber invert()
Invert the instance.

Returns:
the inverse of the instance
Throws:
ArithmeticException - if the instance is zero

getNumerator

public BigInteger getNumerator()
Get the numerator.

Returns:
the signed numerator

getDenominator

public BigInteger getDenominator()
Get the denominator.

Returns:
the denominator (always positive)

isZero

public boolean isZero()
Check if the number is zero.

Returns:
true if the number is zero

isOne

public boolean isOne()
Check if the number is one.

Returns:
true if the number is one

isInteger

public boolean isInteger()
Check if the number is integer.

Returns:
true if the number is an integer

isNegative

public boolean isNegative()
Check if the number is negative.

Returns:
true if the number is negative

abs

public static RationalNumber abs(RationalNumber r)
Get the absolute value of a rational number.

Parameters:
r - rational number from which we want the absolute value
Returns:
a new rational number which is the absolute value of r

doubleValue

public double doubleValue()
Return the double value of the instance.

Returns:
the double value of the instance

equals

public boolean equals(Object o)
Check if the instance is equal to another rational number. Equality here is having the same value.

Returns:
true if the object is a rational number which has the same value as the instance

hashCode

public int hashCode()
Returns a hash code value for the object. The hash code value is computed from the reduced numerator and denominator, hence equal rational numbers have the same hash code, as required by the method specification.

Returns:
a hash code value for this object.

toString

public String toString()
Returns a string representation of the rational number. The representation is reduced: there is no common factor left between the numerator and the denominator. The '/' character and the denominator are displayed only if the denominator is not one. The sign is on the numerator.

Returns:
string representation of the rational number


Copyright © 2001-2007 Luc Maisonobe. All Rights Reserved.