Algebraic provides an exact number type for algebraic numbers. Algebraic numbers are roots of polynomials with
rational coefficients. With it, we can represent expressions involving addition, multiplication, division, n-roots
(eg. sqrt
or cbrt
), and roots of rational polynomials. So, it is similar Rational, but adds roots as a valid,
exact operation. The cost is that this will not be as fast as Rational for many operations.
In general, you can assume all operations on this number type are exact, except for those that explicitly construct
approximations to an Algebraic number, such as toBigDecimal
.
For an overview of the ideas, algorithms, and proofs of this number type, you can read the following papers:
- "On Guaranteed Accuracy Computation." C. K. Yap.
- "Recent Progress in Exact Geometric Computation." C. Li, S. Pion, and C. K. Yap.
- "A New Constructive Root Bound for Algebraic Expressions" by C. Li & C. K. Yap.
- "A Separation Bound for Real Algebraic Expressions." C. Burnikel, et al.
- Companion:
- object
Value members
Concrete methods
Return a non-negative Algebraic
with the same magnitude as this one.
Return a non-negative Algebraic
with the same magnitude as this one.
Returns an integer with the same sign as this - that
. Specifically, if this < that
, then the sign is
negative, if this > that
, then the sign is positive, otherwise this == that
and this returns 0.
Returns an integer with the same sign as this - that
. Specifically, if this < that
, then the sign is
negative, if this > that
, then the sign is positive, otherwise this == that
and this returns 0.
Returns a Double
that approximates this value. If the exponent is too large to fit in a double, the
Double.PositiveInfinity
or Double.NegativeInfinity
is returned.
Returns a Double
that approximates this value. If the exponent is too large to fit in a double, the
Double.PositiveInfinity
or Double.NegativeInfinity
is returned.
Evaluates this algebraic expression with a different number type. All Algebraic
numbers store the entire
expression tree, so we can use this to replay the stored expression using a different type. This will accumulate
errors as if the number type had been used from the beginning and is only really suitable for more exact number
types, like Real.
Evaluates this algebraic expression with a different number type. All Algebraic
numbers store the entire
expression tree, so we can use this to replay the stored expression using a different type. This will accumulate
errors as if the number type had been used from the beginning and is only really suitable for more exact number
types, like Real.
TODO: Eq/ClassTag come from poly.map - would love to get rid of them.
Returns a Float
that approximates this value. If the exponent is too large to fit in a float, the
Float.PositiveInfinity
or Float.NegativeInfinity
is returned.
Returns a Float
that approximates this value. If the exponent is too large to fit in a float, the
Float.PositiveInfinity
or Float.NegativeInfinity
is returned.
Returns the nearest, valid Int
value to this Algebraic, without going further away from 0 (eg. truncation).
Returns the nearest, valid Int
value to this Algebraic, without going further away from 0 (eg. truncation).
If this Algebraic
represented 1.2, then this would return 1. If this represented -3.3, then this would return -3.
If this value is greater than Int.MaxValue
, then Int.MaxValue
is returned. If this value is less than
Int.MinValue
, then Int.MinValue
is returned.
Returns true
iff this is a rational expression (ie contains no n-root expressions). Otherwise it is a radical
expression and returns false.
Returns true
iff this is a rational expression (ie contains no n-root expressions). Otherwise it is a radical
expression and returns false.
Returns true
if this Algebraic number is a whole number (no fractional part) and fits within the bounds of an
Int
. That is, if x.isValidInt
, then Algebraic(x.toInt) == x
.
Returns true
if this Algebraic number is a whole number (no fractional part) and fits within the bounds of an
Int
. That is, if x.isValidInt
, then Algebraic(x.toInt) == x
.
- Definition Classes
Returns true
if this Algebraic number is a whole number (no fractional part) and fits within the bounds of an
Long
. That is, if x.isValidLong
, then Algebraic(x.toLong) == x
.
Returns true
if this Algebraic number is a whole number (no fractional part) and fits within the bounds of an
Long
. That is, if x.isValidLong
, then Algebraic(x.toLong) == x
.
Returns true
iff this Algebraic exactly represents a valid BigInt
.
Returns true
iff this Algebraic exactly represents a valid BigInt
.
Returns true
iff this Algebraic number is exactly 0.
Returns true
iff this Algebraic number is exactly 0.
Returns the nearest, valid Long
value to this Algebraic, without going further away from 0 (eg. truncation).
Returns the nearest, valid Long
value to this Algebraic, without going further away from 0 (eg. truncation).
If this Algebraic
represented 1.2, then this would return 1. If this represented -3.3, then this would return -3.
If this value is greater than Long.MaxValue
, then Long.MaxValue
is returned. If this value is less than
Long.MinValue
, then Long.MinValue
is returned.
Returns the sign of this Algebraic number. Algebraic numbers support exact sign tests, so this is guaranteed to be accurate.
Returns the sign of this Algebraic number. Algebraic numbers support exact sign tests, so this is guaranteed to be accurate.
Returns an Int
with the same sign as this algebraic number. Algebraic numbers support exact sign tests, so this
is guaranteed to be accurate.
Returns an Int
with the same sign as this algebraic number. Algebraic numbers support exact sign tests, so this
is guaranteed to be accurate.
Returns an Algebraic
whose value is the difference between this
and (this /~ that) * that
-- the remainder of
truncated division.
Returns an Algebraic
whose value is the difference between this
and (this /~ that) * that
-- the remainder of
truncated division.
Absolute approximation to scale
decimal places with the given rounding mode. Rounding is always exact.
Absolute approximation to scale
decimal places with the given rounding mode. Rounding is always exact.
Relative approximation to the precision specified in mc
with the given rounding mode. Rounding is always exact.
The sign is always correct; the sign of the returned BigDecimal
matches the sign of the exact value this
Algebraic
represents.
Relative approximation to the precision specified in mc
with the given rounding mode. Rounding is always exact.
The sign is always correct; the sign of the returned BigDecimal
matches the sign of the exact value this
Algebraic
represents.
- Value parameters:
- mc
the precision and rounding mode of the final result
- Returns:
an approximation to the value of this algebraic number
Returns the nearest, valid BigInt
value to this Algebraic, without going further away from 0 (eg. truncation).
Returns the nearest, valid BigInt
value to this Algebraic, without going further away from 0 (eg. truncation).
If this Algebraic
represented 1.2, then this would return 1. If this represented -3.3, then this would return -3.