Precise class
Precise
represents an arbitrary precision number.
It can be used anywhere a Real
Number
is used.
Arbitrary precision can be used to combat the effects of cumulative rounding errors in circumstances where those errors can be significant.
This class enables arbitrary precision calculations in both Dart and when transpiled to JavaScript by representing numbers as digits that stay within the limitations of the underlying number system.
In JavaScript the maximum value of a number, because they are 64-bit floating point values, is 2^53, or 9007199254740992, and the maximum number of significant digits as a result is 16.
Constructors
- Precise(String value, {int sigDigits = 50})
- Constructs an arbitrary precision number from a string.
-
Precise.fromMap(Map<
String, String> ? m) -
Constructs a Precise number, applying the values found in map
m
. SeetoJson
for the expected format.factory - Precise.num(num value, {int? sigDigits})
-
Constructs a Precise number equal to
value
. The number of significant digits defaults to 19 for integers and 15 for doubles but may be specified.factory -
Precise.raw(List<
Digit> digits, {int power = 0, bool neg = false, int sigDigits = 50}) - Creates a arbitrary precision number directly from digits.
Properties
- decimalPortion → Precise
-
Return only the decimal portion as a Precise number.
no setter
-
digits
→ List<
Digit> -
Returns a copy of the internal digits list, from least significant to most.
no setter
- hashCode → int
-
If an integer value, returns the same hash as an int with the same value.
If not an integer value, uses the hashing utility from the quiver package to
create a high quality hash based on the digits, sign and power.
no setteroverride
- isFinite → bool
-
Whether this Number represents a finite value.
no setterinherited
- isInfinite → bool
-
Whether this Number represents infinity.
no setterinherited
- isInteger → bool
-
Returns true if the stored value can be represented exactly as an integer.
no setteroverride
- isNaN → bool
-
Whether this Number represents a value .
no setterinherited
- isNegative → bool
-
Whether this number is less than zero.
no setteroverride
- power → int
-
The offset of the decimal point.
no setter
- precision ↔ int
-
Optional precision cutoff (maximum number of significant digits allowed).
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- sign → num
-
Returns minus one, zero or plus one depending on the sign and numerical value of the number.
Returns minus one if the number is less than zero, plus one if the number is greater than zero,
and zero if the number is equal to zero. Returns NaN if the number is NaN.
Returns an
int
if this Number's value is an integer, adouble
otherwise.no setterinherited - value → num
-
All Real subclasses must be able to provide their value as a dart:core num.
no setteroverride
Methods
-
abs(
) → Precise -
Returns the absolute value of this Number.
override
-
ceil(
) → Precise -
Returns the least Number having integer components no smaller than this Number.
override
-
clamp(
dynamic lowerLimit, dynamic upperLimit) → Precise -
Returns this num clamped to be in the range lowerLimit-upperLimit.
The comparison is done using compareTo and therefore takes -0.0 into account.
This also implies that double.NAN is treated as the maximal double value.
lowerLimit
andupperLimit
are expected to benum
or `Number' objects.override -
compareTo(
dynamic n2) → int -
Compares this Number to another Number by comparing values.
n2
is expected to be a num or Number. If it is not it will be considered to have a value of 0.inherited -
digitAtPlace(
int place) → Digit -
Returns the digit at
place
, where place is with respect to the decimal number system (that is, place 1 is tens, place 2 is hundreds, place -3 is thousandths, etc). -
floor(
) → Precise -
Returns the greatest Number with an integer value no greater than this Number.
If this is not finite (NaN or infinity), throws an UnsupportedError.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reciprocal(
) → Number -
Returns the Number that is the reciprocal of this Number.
This Number is unaffected.
override
-
remainder(
dynamic divisor) → Precise -
Returns the remainder of the truncating division of this Number by
divisor
. The result r of this operation satisfies: this == (this ~/ other) * other + r. As a consequence the remainder r has the same sign as the operator /(divisor).override -
round(
) → Precise -
Returns the Precise integer value closest to this Precise value.
Rounds away from zero when there is no closest integer:
(3.5).round() == 4 and (-3.5).round() == -4.
override
-
toDouble(
) → double -
Converts this Number to a
dart:core double
.override -
toInt(
) → int -
Converts this Number to a
dart:core int
.override -
toJson(
) → Map< String, dynamic> -
Support
dart:json
stringify.override -
toString(
) → String -
A string representation of this object.
override
-
truncate(
) → Precise -
Returns a truncated value.
override
Operators
-
operator %(
dynamic divisor) → Number -
Modulo operator.
override
-
operator *(
dynamic multiplier) → Precise -
Multiplication operator.
The precision of the product is the lesser of the precision of the two
numbers so as not to imply a level of accuracy not present in the inputs.
override
-
operator +(
dynamic addend) → Precise -
Addition operator.
The precision of the product is the lesser of the precision of the two
numbers so as not to imply a level of accuracy not present in the inputs.
override
-
operator -(
dynamic subtrahend) → Precise -
Subtraction operator.
The precision of the difference is the lesser of the precision of the two
numbers so as not to imply a level of accuracy not present in the inputs.
override
-
operator /(
dynamic divisor) → Number -
Division operator.
The precision of the product is the lesser of the precision of the two
numbers so as not to imply a level of accuracy not present in the inputs.
override
-
operator <(
dynamic other) → bool -
Less than operator.
override
-
operator <=(
dynamic other) → bool -
Less than or equals operator.
override
-
operator ==(
Object other) → bool -
Equals operator.
override
-
operator >(
dynamic other) → bool -
Greater than operator.
override
-
operator >=(
dynamic other) → bool -
Greater than or equals operator.
override
-
operator ^(
dynamic exponent) → Number -
Power operator.
override
-
operator unary-(
) → Precise -
Negation operator.
override
-
operator ~/(
dynamic divisor) → Number -
Truncating division operator.
override
Static Properties
Static Methods
-
determinePlaceExtents(
Precise p1, Precise p2) → List< int> -
Returns the minimum and maximum place extents for the combination of
two Precise objects,
p1
andp2
. -
toPrecise(
dynamic obj, {int? desiredPrecision}) → Precise - Converts a num, Number or String into a Precise object. int and Integer precision defaults to 19. double and Double precision defaults to 15. Any other kind of object defaults to 50. The precision parameter can override these defaults. If obj is a Precise object, a copy will be returned and its precision will be transferred unless overridden.