Package org.apfloat
Class AprationalMath
java.lang.Object
org.apfloat.AprationalMath
Various mathematical functions for arbitrary precision rational numbers.
- Version:
- 1.14.0
- Author:
- Mikko Tommila
-
Method Summary
Modifier and TypeMethodDescriptionstatic Aprational
abs
(Aprational x) Absolute value.static Aprational
bernoulli
(long n) Returns the specified Bernoulli number.static Aprational
bernoulli
(long n, int radix) Returns the specified Bernoulli number in the given radix.static Aprational
binomial
(Aprational n, Aprational k) Binomial coefficient.static Apint[]
continuedFraction
(Aprational x, int n) Generates the firstn
terms in the continued fraction representation ofx
.static Aprational[]
convergents
(Aprational x, int n) Generates the firstn
convergents corresponding to the continued fraction ofx
.static Aprational
copySign
(Aprational x, Aprational y) Copy sign from one argument to another.static Aprational
Harmonic number.static Aprational
harmonicNumber
(Apint n, Apint r) Generalized harmonic number.static Aprational
max
(Aprational x, Aprational y) Returns the greater of the two values.static Aprational
min
(Aprational x, Aprational y) Returns the smaller of the two values.static Aprational
negate
(Aprational x) Deprecated.static Aprational
pochhammer
(Aprational x, Apint n) Pochhammer symbol.static Aprational
pow
(Aprational x, long n) Integer power.static Aprational
product
(Aprational... x) Product of numbers.static Apfloat
round
(Aprational x, long precision, RoundingMode roundingMode) Deprecated.static Apint
roundToInteger
(Aprational x, RoundingMode roundingMode) Roundsx
to integer using the specified rounding mode.static Aprational
roundToMultiple
(Aprational x, Aprational y, RoundingMode roundingMode) Roundsx
to the nearest multiple ofy
using the specified rounding mode.static Apfloat
roundToPlaces
(Aprational x, long places, RoundingMode roundingMode) Roundsx
to the specified number of places using the specified rounding mode.static Apfloat
roundToPrecision
(Aprational x, long precision, RoundingMode roundingMode) Rounds the given number to the specified precision with the specified rounding mode.static Aprational
scale
(Aprational x, long scale) Multiply by a power of the radix.static Aprational
sum
(Aprational... x) Sum of numbers.
-
Method Details
-
pow
public static Aprational pow(Aprational x, long n) throws ArithmeticException, ApfloatRuntimeException Integer power.- Parameters:
x
- Base of the power operator.n
- Exponent of the power operator.- Returns:
x
to then
:th power, that isxn
.- Throws:
ArithmeticException
- If bothx
andn
are zero.ApfloatRuntimeException
-
negate
Deprecated.UseAprational.negate()
.Returns an aprational whose value is-x
.- Parameters:
x
- The argument.- Returns:
-x
.- Throws:
ApfloatRuntimeException
-
abs
Absolute value.- Parameters:
x
- The argument.- Returns:
- Absolute value of
x
. - Throws:
ApfloatRuntimeException
-
copySign
Copy sign from one argument to another.- Parameters:
x
- The value whose sign is to be adjusted.y
- The value whose sign is to be used.- Returns:
x
with its sign changed to match the sign ofy
.- Throws:
ApfloatRuntimeException
- Since:
- 1.1
-
scale
Multiply by a power of the radix. Note that this method is prone to intermediate overflow errors. Also, scaling by a very large negative number won't result in an underflow and a zero result, but an overflow of the denominator and an exception thrown.- Parameters:
x
- The argument.scale
- The scaling factor.- Returns:
x * x.radix()scale
.- Throws:
ApfloatRuntimeException
-
round
@Deprecated public static Apfloat round(Aprational x, long precision, RoundingMode roundingMode) throws IllegalArgumentException, ArithmeticException, ApfloatRuntimeException Deprecated.Rounds the given number to the specified precision with the specified rounding mode.- Parameters:
x
- The number to round.precision
- The precision to round to.roundingMode
- The rounding mode to use.- Returns:
- The rounded number.
- Throws:
IllegalArgumentException
- Ifprecision
is less than zero or zero.ArithmeticException
- If rounding is necessary (result is not exact) and rounding mode isRoundingMode.UNNECESSARY
.ApfloatRuntimeException
- Since:
- 1.7.0
-
roundToPrecision
public static Apfloat roundToPrecision(Aprational x, long precision, RoundingMode roundingMode) throws IllegalArgumentException, ArithmeticException, ApfloatRuntimeException Rounds the given number to the specified precision with the specified rounding mode.- Parameters:
x
- The number to round.precision
- The precision to round to.roundingMode
- The rounding mode to use.- Returns:
- The rounded number.
- Throws:
IllegalArgumentException
- Ifprecision
is less than zero or zero.ArithmeticException
- If rounding is necessary (result is not exact) and rounding mode isRoundingMode.UNNECESSARY
.ApfloatRuntimeException
- Since:
- 1.11.0
-
roundToInteger
public static Apint roundToInteger(Aprational x, RoundingMode roundingMode) throws IllegalArgumentException, ArithmeticException, ApfloatRuntimeException Roundsx
to integer using the specified rounding mode.- Parameters:
x
- The number to round.roundingMode
- The rounding mode to use.- Returns:
- The rounded number.
- Throws:
ArithmeticException
- If rounding is necessary (result is not exact) and rounding mode isRoundingMode.UNNECESSARY
.IllegalArgumentException
ApfloatRuntimeException
- Since:
- 1.11.0
-
roundToPlaces
public static Apfloat roundToPlaces(Aprational x, long places, RoundingMode roundingMode) throws IllegalArgumentException, ArithmeticException, ApfloatRuntimeException Roundsx
to the specified number of places using the specified rounding mode.- Parameters:
x
- The number to round.places
- The number of places to round to (in base 10, the number of decimal places).roundingMode
- The rounding mode to use.- Returns:
- The rounded number.
- Throws:
ArithmeticException
- If rounding is necessary (result is not exact) and rounding mode isRoundingMode.UNNECESSARY
.IllegalArgumentException
ApfloatRuntimeException
- Since:
- 1.11.0
-
roundToMultiple
public static Aprational roundToMultiple(Aprational x, Aprational y, RoundingMode roundingMode) throws IllegalArgumentException, ArithmeticException, ApfloatRuntimeException Roundsx
to the nearest multiple ofy
using the specified rounding mode.- Parameters:
x
- The number to round.y
- The integer multiple to round to.roundingMode
- The rounding mode to use.- Returns:
- The rounded number.
- Throws:
ArithmeticException
- If rounding is necessary (result is not exact) and rounding mode isRoundingMode.UNNECESSARY
.IllegalArgumentException
ApfloatRuntimeException
- Since:
- 1.11.0
-
product
Product of numbers. This method may perform significantly better than simply multiplying the numbers sequentially.If there are no arguments, the return value is
1
.- Parameters:
x
- The argument(s).- Returns:
- The product of the given numbers.
- Throws:
ApfloatRuntimeException
- Since:
- 1.3
-
sum
Sum of numbers. This method may perform significantly better than simply adding the numbers sequentially.If there are no arguments, the return value is
0
.- Parameters:
x
- The argument(s).- Returns:
- The sum of the given numbers.
- Throws:
ApfloatRuntimeException
- Since:
- 1.3
-
continuedFraction
Generates the firstn
terms in the continued fraction representation ofx
.Note that the result length might be less than
n
, depending on the input value.- Parameters:
x
- The number whose continued fraction terms should be generated.n
- The maximum number of terms to generate.- Returns:
- The continued fraction.
- Throws:
IllegalArgumentException
- Ifn
is less than one.- Since:
- 1.12.0
-
convergents
Generates the firstn
convergents corresponding to the continued fraction ofx
.Note that the result length might be less than
n
, depending on the input value.- Parameters:
x
- The number whose continued fraction convergents should be generated.n
- The maximum number of convergents to generate.- Returns:
- The convergents.
- Throws:
IllegalArgumentException
- Ifn
is less than one.- Since:
- 1.12.0
-
max
Returns the greater of the two values.- Parameters:
x
- An argument.y
- Another argument.- Returns:
- The greater of the two values.
- Throws:
ApfloatRuntimeException
- Since:
- 1.9.0
-
min
Returns the smaller of the two values.- Parameters:
x
- An argument.y
- Another argument.- Returns:
- The smaller of the two values.
- Throws:
ApfloatRuntimeException
- Since:
- 1.9.0
-
binomial
public static Aprational binomial(Aprational n, Aprational k) throws ArithmeticException, ApfloatRuntimeException Binomial coefficient.- Parameters:
n
- The first argument.k
- The second argument.- Returns:
- Throws:
ArithmeticException
- If the result is not finite or not a rational number.ApfloatRuntimeException
- Since:
- 1.11.0
-
pochhammer
Pochhammer symbol.- Parameters:
x
- The first argument.n
- The second argument.- Returns:
(x)n
- Since:
- 1.13.0
- Implementation notes:
- This implementation is slow, meaning that it isn't a fast algorithm. The asymptotic complexity is at least O(n2log n) and it is impractically slow beyond a precision of a few thousand digits. At the time of implementation no generic fast algorithm is known for the pochhammer symbol.
-
bernoulli
Returns the specified Bernoulli number. The default radix is used.- Parameters:
n
- The argument.- Returns:
- The Bernoulli number
Bn
. - Throws:
IllegalArgumentException
- Ifn < 0
.ApfloatRuntimeException
- Since:
- 1.11.0
-
bernoulli
public static Aprational bernoulli(long n, int radix) throws IllegalArgumentException, NumberFormatException, ApfloatRuntimeException Returns the specified Bernoulli number in the given radix.- Parameters:
n
- The argument.radix
- The radix.- Returns:
- The Bernoulli number
Bn
. - Throws:
IllegalArgumentException
- Ifn < 0
.NumberFormatException
- If the radix is not valid.ApfloatRuntimeException
- Since:
- 1.11.0
-
harmonicNumber
public static Aprational harmonicNumber(Apint n) throws ArithmeticException, ApfloatRuntimeException Harmonic number.- Parameters:
n
- The argument.- Returns:
- Hn
- Throws:
ArithmeticException
- Ifn
is negative.ApfloatRuntimeException
- Since:
- 1.14.0
-
harmonicNumber
public static Aprational harmonicNumber(Apint n, Apint r) throws ArithmeticException, ApfloatRuntimeException Generalized harmonic number.- Parameters:
n
- The first argument.r
- The second argument.- Returns:
- Hn(r)
- Throws:
ArithmeticException
- Ifn
is negative andr
is positive.ApfloatRuntimeException
- Since:
- 1.14.0
-
Aprational.negate()
.