Most visited

Recently visited

Added in API level 21

Rational

public final class Rational
extends Number implements Comparable<Rational>

java.lang.Object
   ↳ java.lang.Number
     ↳ android.util.Rational


一个不可变的数据类型表示有理数。

包含一对代表 int分子和分母的 int

Summary

Fields

public static final Rational NEGATIVE_INFINITY

常量为 Rational类型的负无穷大值。

public static final Rational NaN

用于 Rational类型 的非数字(NaN)值的 Rational

public static final Rational POSITIVE_INFINITY

用于 Rational类型的正无穷大值的 Rational

public static final Rational ZERO

用于 Rational类型的零值的 Rational

Public constructors

Rational(int numerator, int denominator)

用给定的分子和分母创建一个 Rational

Public methods

int compareTo(Rational another)

比较这个理性与指定的理性来确定它们的自然秩序。

double doubleValue()

double返回指定数字的值。

boolean equals(Object obj)

将这个Rational与另一个对象进行比较,看看它们是否相等。

float floatValue()

float返回指定数字的值。

int getDenominator()

获得理性的分母

分母可能会返回 0 ,在这种情况下,理性可能表示正无穷大(如果分子为正),负无穷大(如果分子为负)或 NaN (如果分子为 0 )。

int getNumerator()

获取理性的分子。

int hashCode()

返回对象的哈希码值。

int intValue()

int返回指定数字的值。

boolean isFinite()

指示这个理性是否代表有限的值。

boolean isInfinite()

指出这个理性是否代表无限的价值。

boolean isNaN()

指示此有理数是否 为非数字(NaN)值。

boolean isZero()

指示此理性是否表示零值。

long longValue()

long返回指定数字的值。

static Rational parseRational(String string)

将指定的字符串解析为有理数值。

short shortValue()

short返回指定数字的值。

String toString()

返回这个有理数的字符串表示,例如

Inherited methods

From class java.lang.Number
From class java.lang.Object
From interface java.lang.Comparable

Fields

NEGATIVE_INFINITY

Added in API level 21
Rational NEGATIVE_INFINITY

常量,用于 Rational类型的负无穷大值。

相当于构造一个负数分子和分母等于 0的新理性。

NaN

Added in API level 21
Rational NaN

用于 Rational类型 的非数字(NaN)值的 Rational

一个 NaN值被认为等于它自己(即 NaN.equals(NaN)将返回 true ;它总是大于任何非 NaN值(即 NaN.compareTo(notNaN)将返回大于 0的数字)。

相当于用分子和分母构造一个新的理性等于 0

POSITIVE_INFINITY

Added in API level 21
Rational POSITIVE_INFINITY

用于 Rational类型的正无穷大值的 Rational

等价于构造一个具有正分子和分母等于 0的新理性。

ZERO

Added in API level 21
Rational ZERO

用于 Rational类型的零值的 Rational

相当于用分子等于 0和任何非零分母构造一个新的有理数。

Public constructors

Rational

Added in API level 21
Rational (int numerator, 
                int denominator)

用给定的分子和分母创建一个 Rational

分子和分母的符号可能会翻转,因此分母总是正数。 分子和分母都将转换为简化形式(有关更多详细信息,请参阅equals(Object) )。

例如,

  • a rational of 2/4 will be reduced to 1/2.
  • a rational of 1/-1 will be flipped to -1/1
  • a rational of 5/0 will be reduced to 1/0
  • a rational of 0/5 will be reduced to 0/1

Parameters
numerator int: the numerator of the rational
denominator int: the denominator of the rational

也可以看看:

Public methods

compareTo

Added in API level 21
int compareTo (Rational another)

比较这个理性与指定的理性来确定它们的自然秩序。

NaN被认为等于自身,并且大于所有其他Rational值。 否则,如果对象不是equal ,则适用以下规则:

  • Positive infinity is greater than any other finite number (or negative infinity)
  • Negative infinity is less than any other finite number (or positive infinity)
  • The finite number represented by this rational is checked numerically against the other finite number by converting both rationals to a common denominator multiple and comparing their numerators.

Parameters
another Rational: the rational to be compared
Returns
int a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified rational.
Throws
NullPointerException if another was null

doubleValue

Added in API level 21
double doubleValue ()

double返回指定数字的值。

The double is calculated by converting both the numerator and denominator to a double; then returning the result of dividing the numerator by the denominator.

Returns
double the divided value of the numerator and denominator as a double.

equals

Added in API level 21
boolean equals (Object obj)

将这个Rational与另一个对象进行比较,看看它们是否相等。

一个Rational对象只能等于另一个Rational对象(与任何其他类型进行比较将返回 false )。

一个Rational对象被认为与另一个Rational对象相等当且仅当以下之一成立时:

  • Both are NaN
  • Both are infinities of the same sign
  • Both have the same numerator and denominator in their reduced form

A reduced form of a Rational is calculated by dividing both the numerator and the denominator by their greatest common divisor.

(new Rational(1, 2)).equals(new Rational(1, 2)) == true   // trivially true
 (new Rational(2, 3)).equals(new Rational(1, 2)) == false  // trivially false
 (new Rational(1, 2)).equals(new Rational(2, 4)) == true   // true after reduction
 (new Rational(0, 0)).equals(new Rational(0, 0)) == true   // NaN.equals(NaN)
 (new Rational(1, 0)).equals(new Rational(5, 0)) == true   // both are +infinity
 (new Rational(1, 0)).equals(new Rational(-1, 0)) == false // +infinity != -infinity
 

Parameters
obj Object: a reference to another object
Returns
boolean A boolean that determines whether or not the two Rational objects are equal.

floatValue

Added in API level 21
float floatValue ()

float返回指定数字的值。

The float is calculated by converting both the numerator and denominator to a float; then returning the result of dividing the numerator by the denominator.

Returns
float the divided value of the numerator and denominator as a float.

getDenominator

Added in API level 21
int getDenominator ()

获得理性的分母

分母可能会返回 0 ,在这种情况下,有理数可能表示正无穷大(如果分子为正数),负无穷大(如果分子为负数)或 NaN (如果分子为 0 )。

分母将始终返回 1如果分子为 0

Returns
int

getNumerator

Added in API level 21
int getNumerator ()

获取理性的分子。

The numerator will always return 1 if this rational represents infinity (that is, the denominator is 0).

Returns
int

hashCode

Added in API level 21
int hashCode ()

返回对象的哈希码值。 为了散列表的好处而支持该方法,例如由HashMap提供的HashMap

hashCode的总合同是:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

尽可能合理实用,类Object定义的hashCode方法确实为不同的对象返回不同的整数。 (这通常通过将对象的内部地址转换为整数来实现,但Java TM编程语言不需要此实现技术。)

Returns
int a hash code value for this object.

intValue

Added in API level 21
int intValue ()

int返回指定数字的值。

Finite通过将分子除以分子将其转换为int值; 非有限值转换的发生与将浮点值转换为int ,特别是:

  • Positive infinity saturates to the largest maximum integer MAX_VALUE
  • Negative infinity saturates to the smallest maximum integer MIN_VALUE
  • Not-A-Number (NaN) returns 0.

Returns
int the divided value of the numerator and denominator as a int.

isFinite

Added in API level 21
boolean isFinite ()

指示这个理性是否代表有限的值。

当分母不是0时出现有限值; 换句话说,理性既不是无限也不是NaN

Returns
boolean true if this rational is a (positive or negative) infinite value; false if this is a finite number value (or NaN)

isInfinite

Added in API level 21
boolean isInfinite ()

指出这个理性是否代表无限的价值。

当分母是 0 (但分子不是)时出现无限值。

Returns
boolean true if this rational is a (positive or negative) infinite value; false if this is a finite number value (or NaN)

isNaN

Added in API level 21
boolean isNaN ()

指示此有理数是否 为非数字(NaN)值。

当分子和分母均为 0时,将出现 NaN值。

Returns
boolean true if this rational is a Not-a-Number (NaN) value; false if this is a (potentially infinite) number value

isZero

Added in API level 21
boolean isZero ()

指示此理性是否表示零值。

零值为 finite ,分子为 0

Returns
boolean true if this rational is finite zero value; false otherwise

longValue

Added in API level 21
long longValue ()

long返回指定数字的值。

Finite通过将分子除以分母将有理数转换为long值; 非有限值转换的发生与将浮点值转换为long ,特别是:

  • Positive infinity saturates to the largest maximum long MAX_VALUE
  • Negative infinity saturates to the smallest maximum long MIN_VALUE
  • Not-A-Number (NaN) returns 0.

Returns
long the divided value of the numerator and denominator as a long.

parseRational

Added in API level 21
Rational parseRational (String string)

将指定的字符串解析为有理数值。

ASCII字符 \ u003a (':')和 \ u002f ('/')被识别为分子和分子之间的分隔符。

对于任何Rational rRational.parseRational(r.toString()).equals(r) 但是,该方法也处理以下列形式表示的有理数:

num / den ”或“ num : den=> new Rational(num, den); ,其中 numden是可能包含符号(例如“-10”,“+7”或“5”)的字符串整数。

Rational.parseRational("3:+6").equals(new Rational(1, 2)) == true
 Rational.parseRational("-3/-6").equals(new Rational(1, 2)) == true
 Rational.parseRational("4.56") => throws NumberFormatException
 

Parameters
string String: the string representation of a rational value.
Returns
Rational the rational value represented by string.
Throws
NumberFormatException if string cannot be parsed as a rational value.
NullPointerException if string was null

shortValue

Added in API level 21
short shortValue ()

short返回指定数字的值。

Finite有理数被转换为short值相同地intValue() ; 在返回值之前, int结果将被截断为short

Returns
short the divided value of the numerator and denominator as a short.

toString

Added in API level 21
String toString ()

返回此有理数的字符串表示形式,例如 "1/2"

以下转换规则适用:

  • NaN values will return "NaN"
  • Positive infinity values will return "Infinity"
  • Negative infinity values will return "-Infinity"
  • All other values will return "numerator/denominator" where numerator and denominator are substituted with the appropriate numerator and denominator values.

Returns
String a string representation of the object.

Hooray!