Class PrecisionModel
- Namespace
- NetTopologySuite.Geometries
- Assembly
- NetTopologySuite.dll
Specifies the precision model of the Coordinate
s in a Geometry
.
In other words, specifies the grid of allowable points for a Geometry
.
A precision model may befloating (Floating or FloatingSingle),
in which case normal floating-point value semantics apply.
public class PrecisionModel : IComparable, IComparable<PrecisionModel>
- Inheritance
-
PrecisionModel
- Implements
- Inherited Members
Remarks
For a Fixed precision model the MakePrecise(Coordinate) method allows rounding a coordinate to a "precise" value; that is, one whose precision is known exactly.
Coordinates are assumed to be precise in geometries. That is, the coordinates are assumed to be rounded to the precision model given for the geometry. All internal operations assume that coordinates are rounded to the precision model. Constructive methods (such as bool operations) always round computed coordinates to the appropriate precision model.Three types of precision model are supported:
Floating | Represents full double precision floating point. This is the default precision model used in NTS |
FloatingSingle | Represents single precision floating point |
Fixed | Represents a model with a fixed number of decimal places. A Fixed Precision Model is specified by a scale factor. The scale factor specifies the size of the grid which numbers are rounded to. |
jtsPt.X = Math.Round( inputPt.X * scale, MidPointRounding.AwayFromZero ) / scale )
jtsPt.Y = Math.Round( inputPt.Y * scale, MidPointRounding.AwayFromZero ) / scale )
For example, to specify 3 decimal places of precision, use a scale factor of 1000. To specify -3 decimal places of precision (i.e. rounding to the nearest 1000), use a scale factor of 0.001.
It is also supported to specify a precise grid size by providing it as a negative scale factor. This allows setting a precise grid size rather than using a fractional scale, which provides more accurate and robust rounding. For example, to specify rounding to the nearest 1000 use a scale factor of -1000. Coordinates are represented internally as Java double-precision values. .NET uses the IEEE-394 floating point standard, which provides 53 bits of precision. (Thus the maximum precisely representable integer is 9,007,199,254,740,992 - or almost 16 decimal digits of precision).Constructors
PrecisionModel()
Creates a PrecisionModel
with a default precision
of Floating.
public PrecisionModel()
PrecisionModel(PrecisionModel)
Copy constructor to create a new PrecisionModel
from an existing one.
public PrecisionModel(PrecisionModel pm)
Parameters
PrecisionModel(PrecisionModels)
Creates a PrecisionModel
that specifies
an explicit precision model type.
If the model type is Fixed the scale factor will default to 1.
public PrecisionModel(PrecisionModels modelType)
Parameters
modelType
PrecisionModelsThe type of the precision model.
PrecisionModel(double)
Creates a PrecisionModel
that specifies Fixed precision.
Fixed-precision coordinates are represented as precise internal coordinates,
which are rounded to the grid defined by the scale factor.
public PrecisionModel(double scale)
Parameters
scale
doubleAmount by which to multiply a coordinate, to obtain a precise coordinate. Must be non-zero
Fields
MaximumPreciseValue
The maximum precise value representable in a double. Since IEE754 double-precision numbers allow 53 bits of mantissa, the value is equal to 2^53 - 1. This provides almost 16 decimal digits of precision.
public const double MaximumPreciseValue = 9007199254740992
Field Value
Properties
Fixed
Gets a value indicating a precision model with a scale of 1
.
public static Lazy<PrecisionModel> Fixed { get; }
Property Value
- Lazy<PrecisionModel>
A fixed precision model
Floating
Gets a value indicating a precision model with double precision.
public static Lazy<PrecisionModel> Floating { get; }
Property Value
- Lazy<PrecisionModel>
A double precision model
FloatingSingle
Gets a value indicating a precision model with single precision.
public static Lazy<PrecisionModel> FloatingSingle { get; }
Property Value
- Lazy<PrecisionModel>
A single precision model
GridSize
Computes the grid size for a fixed precision model. This is equal to the reciprocal of the scale factor. If the grid size has been set explicity (via a negative scale factor) it will be returned.
public double GridSize { get; }
Property Value
- double
The grid size at a fixed precision scale.
IsFloating
Tests whether the precision model supports floating point.
public bool IsFloating { get; }
Property Value
- bool
true
if the precision model supports floating point.
MaximumSignificantDigits
Returns the maximum number of significant digits provided by this precision model. Intended for use by routines which need to print out precise values.
public int MaximumSignificantDigits { get; }
Property Value
- int
The maximum number of decimal places provided by this precision model.
PrecisionModelType
Gets the type of this PrecisionModel.
public PrecisionModels PrecisionModelType { get; }
Property Value
Scale
Returns the scale factor used to specify a fixed precision model.
public double Scale { get; set; }
Property Value
- double
The scale factor for the fixed precision model
Remarks
The number of decimal places of precision is equal to the base-10 logarithm of the scale factor. Non-integral and negative scale factors are supported. Negative scale factors indicate that the places of precision is to the left of the decimal point.
Methods
CompareTo(PrecisionModel)
public int CompareTo(PrecisionModel other)
Parameters
other
PrecisionModel
Returns
CompareTo(object)
Compares this PrecisionModel
object with the specified object for order.
A PrecisionModel is greater than another if it provides greater precision.
The comparison is based on the value returned by the
{getMaximumSignificantDigits) method.
This comparison is not strictly accurate when comparing floating precision models
to fixed models; however, it is correct when both models are either floating or fixed.
public int CompareTo(object o)
Parameters
o
objectThe
PrecisionModel
with which thisPrecisionModel
is being compared.
Returns
- int
A negative integer, zero, or a positive integer as this
PrecisionModel
is less than, equal to, or greater than the specifiedPrecisionModel
.
Equals(PrecisionModel)
public bool Equals(PrecisionModel otherPrecisionModel)
Parameters
otherPrecisionModel
PrecisionModel
Returns
Equals(object)
public override bool Equals(object other)
Parameters
other
object
Returns
GetHashCode()
public override int GetHashCode()
Returns
MakePrecise(Coordinate)
Rounds a Coordinate to the PrecisionModel grid.
public void MakePrecise(Coordinate coord)
Parameters
coord
Coordinate
MakePrecise(double)
Rounds a numeric value to the PrecisionModel grid. Symmetric Arithmetic Rounding is used, to provide uniform rounding behaviour no matter where the number is on the number line.
public double MakePrecise(double val)
Parameters
val
double
Returns
Remarks
This method has no effect on NaN values
MostPrecise(PrecisionModel, PrecisionModel)
Determines which of two PrecisionModels is the most precise
public static PrecisionModel MostPrecise(PrecisionModel pm1, PrecisionModel pm2)
Parameters
pm1
PrecisionModelA precision model
pm2
PrecisionModelA precision model
Returns
- PrecisionModel
The PrecisionModel which is most precise
ToString()
public override string ToString()
Returns
Operators
operator ==(PrecisionModel, PrecisionModel)
public static bool operator ==(PrecisionModel obj1, PrecisionModel obj2)
Parameters
obj1
PrecisionModelobj2
PrecisionModel
Returns
operator !=(PrecisionModel, PrecisionModel)
public static bool operator !=(PrecisionModel obj1, PrecisionModel obj2)
Parameters
obj1
PrecisionModelobj2
PrecisionModel