org.apache.commons.lang.math
クラス Fraction

java.lang.Object
  拡張java.lang.Number
      拡張org.apache.commons.lang.math.Fraction
すべての実装インタフェース:
Comparable, Serializable

public final class Fraction
extends Number
implements Serializable, Comparable

Fraction is a Number implementation that stores fractions accurately.

This class is immutable, and interoperable with most methods that accept a Number.

導入されたバージョン:
2.0
バージョン:
$Id: Fraction.java,v 1.1.1.1 2004/02/13 10:02:05 hioki Exp $
作成者:
Travis Reeder, Stephen Colebourne, Tim O'Brien, Pete Gieser
関連項目:
直列化された形式
翻訳状況:
訳者募集中

フィールドの概要
static Fraction FOUR_FIFTHS
          Fraction representation of 4/5.
static Fraction ONE
          Fraction representation of 1.
static Fraction ONE_FIFTH
          Fraction representation of 1/5.
static Fraction ONE_HALF
          Fraction representation of 1/2.
static Fraction ONE_QUARTER
          Fraction representation of 1/4.
static Fraction ONE_THIRD
          Fraction representation of 1/3.
static Fraction THREE_FIFTHS
          Fraction representation of 3/5.
static Fraction THREE_QUARTERS
          Fraction representation of 3/4.
static Fraction TWO_FIFTHS
          Fraction representation of 2/5.
static Fraction TWO_QUARTERS
          Fraction representation of 2/4.
static Fraction TWO_THIRDS
          Fraction representation of 2/3.
static Fraction ZERO
          Fraction representation of 0.
 
メソッドの概要
 Fraction abs()
          Gets a fraction that is the positive equivalent of this one.
 Fraction add(Fraction fraction)
          Adds the value of this fraction to another, returning the result in reduced form.
 int compareTo(Object object)
          Compares this object to another based on size.
 Fraction divideBy(Fraction fraction)
          Divide the value of this fraction by another, returning the result in reduced form.
 double doubleValue()
          Gets the fraction as a double.
 boolean equals(Object obj)
          Compares this fraction to another object to test if they are equal.
 float floatValue()
          Gets the fraction as a float.
 int getDenominator()
          Gets the denominator part of the fraction.
static Fraction getFraction(double value)
          Creates a Fraction instance from a double value.
static Fraction getFraction(int numerator, int denominator)
          Creates a Fraction instance with the 2 parts of a fraction Y/Z.
static Fraction getFraction(int whole, int numerator, int denominator)
          Creates a Fraction instance with the 3 parts of a fraction X Y/Z.
static Fraction getFraction(String str)
          Creates a Fraction from a String.
 int getNumerator()
          Gets the numerator part of the fraction.
 int getProperNumerator()
          Gets the proper numerator, always positive.
 int getProperWhole()
          Gets the proper whole part of the fraction.
static Fraction getReducedFraction(int numerator, int denominator)
          Creates a Fraction instance with the 2 parts of a fraction Y/Z.
 int hashCode()
          Gets a hashCode for the fraction.
 int intValue()
          Gets the fraction as an int.
 Fraction invert()
          Gets a fraction that is the invert (1/fraction) of this one.
 long longValue()
          Gets the fraction as a long.
 Fraction multiplyBy(Fraction fraction)
          Multiplies the value of this fraction by another, returning the result in reduced form.
 Fraction negate()
          Gets a fraction that is the negative (-fraction) of this one.
 Fraction pow(int power)
          Gets a fraction that is raised to the passed in power.
 Fraction reduce()
          Reduce the fraction to the smallest values for the numerator and denominator, returning the result..
 Fraction subtract(Fraction fraction)
          Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
 String toProperString()
          Gets the fraction as a proper String in the format X Y/Z.
 String toString()
          Gets the fraction as a String.
 
クラス java.lang.Number から継承したメソッド
byteValue, shortValue
 
クラス java.lang.Object から継承したメソッド
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

フィールドの詳細

ZERO

public static final Fraction ZERO
Fraction representation of 0.


ONE

public static final Fraction ONE
Fraction representation of 1.


ONE_HALF

public static final Fraction ONE_HALF
Fraction representation of 1/2.


ONE_THIRD

public static final Fraction ONE_THIRD
Fraction representation of 1/3.


TWO_THIRDS

public static final Fraction TWO_THIRDS
Fraction representation of 2/3.


ONE_QUARTER

public static final Fraction ONE_QUARTER
Fraction representation of 1/4.


TWO_QUARTERS

public static final Fraction TWO_QUARTERS
Fraction representation of 2/4.


THREE_QUARTERS

public static final Fraction THREE_QUARTERS
Fraction representation of 3/4.


ONE_FIFTH

public static final Fraction ONE_FIFTH
Fraction representation of 1/5.


TWO_FIFTHS

public static final Fraction TWO_FIFTHS
Fraction representation of 2/5.


THREE_FIFTHS

public static final Fraction THREE_FIFTHS
Fraction representation of 3/5.


FOUR_FIFTHS

public static final Fraction FOUR_FIFTHS
Fraction representation of 4/5.

メソッドの詳細

getFraction

public static Fraction getFraction(int numerator,
                                   int denominator)

Creates a Fraction instance with the 2 parts of a fraction Y/Z.

Any negative signs are resolved to be on the numerator.

パラメータ:
numerator - the numerator, for example the three in 'three sevenths'
denominator - the denominator, for example the seven in 'three sevenths'
戻り値:
a new fraction instance
例外:
ArithmeticException - if the denomiator is zero

getFraction

public static Fraction getFraction(int whole,
                                   int numerator,
                                   int denominator)

Creates a Fraction instance with the 3 parts of a fraction X Y/Z.

The negative sign must be passed in on the whole number part.

パラメータ:
whole - the whole number, for example the one in 'one and three sevenths'
numerator - the numerator, for example the three in 'one and three sevenths'
denominator - the denominator, for example the seven in 'one and three sevenths'
戻り値:
a new fraction instance
例外:
ArithmeticException - if the denomiator is zero
ArithmeticException - if the denomiator is negative
ArithmeticException - if the numerator is negative
ArithmeticException - if the resulting numerator exceeds Integer.MAX_VALUE

getReducedFraction

public static Fraction getReducedFraction(int numerator,
                                          int denominator)

Creates a Fraction instance with the 2 parts of a fraction Y/Z.

Any negative signs are resolved to be on the numerator.

パラメータ:
numerator - the numerator, for example the three in 'three sevenths'
denominator - the denominator, for example the seven in 'three sevenths'
戻り値:
a new fraction instance, with the numerator and denominator reduced
例外:
ArithmeticException - if the denomiator is zero

getFraction

public static Fraction getFraction(double value)

Creates a Fraction instance from a double value.

This method uses the continued fraction algorithm, computing a maximum of 25 convergents and bounding the denominator by 10,000.

パラメータ:
value - the double value to convert
戻り値:
a new fraction instance that is close to the value
例外:
ArithmeticException - if |value| > Integer.MAX_VALUE or value = NaN
ArithmeticException - if the calculated denomiator is zero
ArithmeticException - if the the algorithm does not converge

getFraction

public static Fraction getFraction(String str)

Creates a Fraction from a String.

The formats accepted are:

  1. double String containing a dot
  2. 'X Y/Z'
  3. 'Y/Z'
and a .

パラメータ:
str - the string to parse, must not be null
戻り値:
the new Fraction instance
例外:
IllegalArgumentException - if the string is null
NumberFormatException - if the number format is invalid

getNumerator

public int getNumerator()

Gets the numerator part of the fraction.

This method may return a value greater than the denominator, an improper fraction, such as the seven in 7/4.

戻り値:
the numerator fraction part

getDenominator

public int getDenominator()

Gets the denominator part of the fraction.

戻り値:
the denominator fraction part

getProperNumerator

public int getProperNumerator()

Gets the proper numerator, always positive.

An improper fraction 7/4 can be resolved into a proper one, 1 3/4. This method returns the 3 from the proper fraction.

If the fraction is negative such as -7/4, it can be resolved into -1 3/4, so this method returns the positive proper numerator, 3.

戻り値:
the numerator fraction part of a proper fraction, always positive

getProperWhole

public int getProperWhole()

Gets the proper whole part of the fraction.

An improper fraction 7/4 can be resolved into a proper one, 1 3/4. This method returns the 1 from the proper fraction.

If the fraction is negative such as -7/4, it can be resolved into -1 3/4, so this method returns the positive whole part -1.

戻り値:
the whole fraction part of a proper fraction, that includes the sign

intValue

public int intValue()

Gets the fraction as an int. This returns the whole number part of the fraction.

戻り値:
the whole number fraction part

longValue

public long longValue()

Gets the fraction as a long. This returns the whole number part of the fraction.

戻り値:
the whole number fraction part

floatValue

public float floatValue()

Gets the fraction as a float. This calculates the fraction as the numerator divided by denominator.

戻り値:
the fraction as a float

doubleValue

public double doubleValue()

Gets the fraction as a double. This calculates the fraction as the numerator divided by denominator.

戻り値:
the fraction as a double

reduce

public Fraction reduce()

Reduce the fraction to the smallest values for the numerator and denominator, returning the result..

戻り値:
a new reduce fraction instance, or this if no simplification possible

invert

public Fraction invert()

Gets a fraction that is the invert (1/fraction) of this one.

The returned fraction is not reduced.

戻り値:
a new fraction instance with the numerator and denominator inverted
例外:
ArithmeticException - if the numerator is zero

negate

public Fraction negate()

Gets a fraction that is the negative (-fraction) of this one.

The returned fraction is not reduced.

戻り値:
a new fraction instance with the opposite signed numerator

abs

public Fraction abs()

Gets a fraction that is the positive equivalent of this one.

More precisely:

(fraction >= 0 ? this : -fraction)

The returned fraction is not reduced.

戻り値:
this if it is positive, or a new positive fraction instance with the opposite signed numerator

pow

public Fraction pow(int power)

Gets a fraction that is raised to the passed in power.

The returned fraction is not reduced.

パラメータ:
power - the power to raise the fraction to
戻り値:
this if the power is one, ONE if the power is zero (even if the fraction equals ZERO) or a new fraction instance raised to the appropriate power
例外:
ArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUE

add

public Fraction add(Fraction fraction)

Adds the value of this fraction to another, returning the result in reduced form.

パラメータ:
fraction - the fraction to add, must not be null
戻り値:
a Fraction instance with the resulting values
例外:
IllegalArgumentException - if the fraction is null
ArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUE

subtract

public Fraction subtract(Fraction fraction)

Subtracts the value of another fraction from the value of this one, returning the result in reduced form.

パラメータ:
fraction - the fraction to subtract, must not be null
戻り値:
a Fraction instance with the resulting values
例外:
IllegalArgumentException - if the fraction is null
ArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUE

multiplyBy

public Fraction multiplyBy(Fraction fraction)

Multiplies the value of this fraction by another, returning the result in reduced form.

パラメータ:
fraction - the fraction to multipy by, must not be null
戻り値:
a Fraction instance with the resulting values
例外:
IllegalArgumentException - if the fraction is null
ArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUE

divideBy

public Fraction divideBy(Fraction fraction)

Divide the value of this fraction by another, returning the result in reduced form.

パラメータ:
fraction - the fraction to divide by, must not be null
戻り値:
a Fraction instance with the resulting values
例外:
IllegalArgumentException - if the fraction is null
ArithmeticException - if the fraction to divide by is zero
ArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUE

equals

public boolean equals(Object obj)

Compares this fraction to another object to test if they are equal.

.

To be equal, both values must be equal. Thus 2/4 is not equal to 1/2.

パラメータ:
obj - the reference object with which to compare
戻り値:
true if this object is equal

hashCode

public int hashCode()

Gets a hashCode for the fraction.

戻り値:
a hash code value for this object

compareTo

public int compareTo(Object object)

Compares this object to another based on size.

定義:
インタフェース Comparable 内の compareTo
パラメータ:
object - the object to compare to
戻り値:
-1 if this is less, 0 if equal, +1 if greater
例外:
ClassCastException - if the object is not a Fraction
NullPointerException - if the object is null

toString

public String toString()

Gets the fraction as a String.

The format used is 'numerator/denominator' always.

戻り値:
a String form of the fraction

toProperString

public String toProperString()

Gets the fraction as a proper String in the format X Y/Z.

The format used in 'wholeNumber numerator/denominator'. If the whole number is zero it will be ommitted. If the numerator is zero, only the whole number is returned.

戻り値:
a String form of the fraction


このドキュメントは、Ja-Jakartaにより訳されました。 コメントがある場合は report@jajakarta.orgまでお願いします。
Translated into Japanese by jajakarta.org. The original page is here.
Copyright (c) 2002-2003 - Apache Software Foundation