Table of Contents

Class MathUtil

Namespace
NetTopologySuite.Mathematics
Assembly
NetTopologySuite.dll

Various utility functions for mathematical and numerical operations.

public class MathUtil
Inheritance
MathUtil
Inherited Members

Constructors

MathUtil()

public MathUtil()

Fields

PhiInv

The inverse of the Golden Ratio phi.

public static readonly double PhiInv

Field Value

double

Methods

Average(double, double)

Computes the average of two numbers.

public static double Average(double x1, double x2)

Parameters

x1 double

A number

x2 double

A number

Returns

double

The average of the inputs

Ceiling(int, int)

Computes the ceiling function of the dividend of two integers.

public static int Ceiling(int num, int denom)

Parameters

num int

The numerator

denom int

The denominator

Returns

int

The ceiling of num / denom

Clamp(double, double, double)

Clamps a double value to a given range.

public static double Clamp(double x, double min, double max)

Parameters

x double

The value to clamp

min double

The minimum value of the range

max double

The maximum value of the range

Returns

double

The clamped value

Clamp(int, int, int)

Clamps a int value to a given range.

public static int Clamp(int x, int min, int max)

Parameters

x int

The value to clamp

min int

The minimum value of the range

max int

The maximum value of the range

Returns

int

The clamped value

ClampMax(int, int)

Clamps an integer to a given maximum limit.

public static int ClampMax(int x, int max)

Parameters

x int

The value to clamp

max int

The maximum value of the range

Returns

int

The clamped value

Log10(double)

Computes the base-10 logarithm of a double value.

  • If the argument is NaN or less than zero, then the result is NaN.
  • If the argument is positive infinity, then the result is positive infinity.
  • If the argument is positive zero or negative zero, then the result is negative infinity.
public static double Log10(double x)

Parameters

x double

A positive number

Returns

double

The value log a, the base-10 logarithm of the input value

Max(double, double, double)

Computes the maximum fo three values

public static double Max(double v1, double v2, double v3)

Parameters

v1 double

A number

v2 double

A number

v3 double

A number

Returns

double

The maximum value of v1, v2 and v3

Max(double, double, double, double)

Computes the maximum of four values

public static double Max(double v1, double v2, double v3, double v4)

Parameters

v1 double

A number

v2 double

A number

v3 double

A number

v4 double

A number

Returns

double

The maximum value of v1, v2, v3 and v4

Min(double, double, double)

Computes the minimum of four values

public static double Min(double v1, double v2, double v3)

Parameters

v1 double

A number

v2 double

A number

v3 double

A number

Returns

double

The minimum value of v1, v2 and v3

Min(double, double, double, double)

Computes the minimum of four values

public static double Min(double v1, double v2, double v3, double v4)

Parameters

v1 double

A number

v2 double

A number

v3 double

A number

v4 double

A number

Returns

double

The minimum value of v1, v2, v3 and v4

QuasiRandom(double)

Generates a quasi-random sequence of numbers in the range [0,1]. They are produced by an additive recurrence with 1/φ as the constant. This produces a low-discrepancy sequence which is more evenly distribute than random numbers.

See Wikipedia: Low-discrepancy Sequences - Additive Recurrence.

The sequence is initialized by calling it with any positive fractional number; 0 works well for most uses.
public static double QuasiRandom(double curr)

Parameters

curr double

The current number in the sequence

Returns

double

The next value in the sequence

QuasiRandom(double, double)

Generates a quasi-random sequence of numbers in the range [0,1]. They are produced by an additive recurrence with constant α.

R(α) :  tn = { t0 + nα },  n = 1,2,3,...

When α is irrational this produces a Low discrepancy sequence which is more evenly distributed than random numbers.

The sequence is initialized by calling it with any positive fractional number. 0 works well for most uses.
public static double QuasiRandom(double curr, double alpha)

Parameters

curr double

The current number in the sequence

alpha double

the sequence's additive constant

Returns

double

The next value in the sequence

Wrap(int, int)

Computes an index which wraps around a given maximum value. For values >= 0, this is equals to val % max. For values < 0, this is equal to max - (-val) % max

public static int Wrap(int index, int max)

Parameters

index int

The index to wrap

max int

The maximum value (or modulus)

Returns

int

The wrapped index