Class MathExtensions
- Namespace
- Thunder.UnitsNET.Vectors.Extensions
- Assembly
- Thunder.UnitsNET.Vectors.dll
Extension methods for UnitsNet math operations.
public static class MathExtensions
- Inheritance
-
MathExtensions
- Inherited Members
Methods
Abs(Angle)
Returns the absolute value of the angle, stripping any negative sign.
The result is always non-negative. For example, −90° becomes 90°.
This is useful when you need a magnitude of rotation regardless of direction.
public static Angle Abs(this Angle value)
Parameters
valueAngleThe angle, which may be negative.
Returns
- Angle
A non-negative UnitsNet.Angle with the same magnitude.
Examples
Angle abs = Angle.FromDegrees(-90).Abs(); // 90°
Abs(Length)
Returns the absolute value of the length, stripping any negative sign.
Lengths are normally non-negative, but signed length differences (e.g. computing how far a point is past an endpoint) can produce negative values. This method returns the magnitude without direction.
public static Length Abs(this Length value)
Parameters
valueLengthThe length, which may be negative.
Returns
- Length
A non-negative UnitsNet.Length with the same magnitude.
Examples
Length abs = Length.FromMeters(-3).Abs(); // 3 m
Acos(double)
Returns the angle whose cosine is value (inverse cosine / arc-cosine).
The result is in [0°, 180°]. The input is clamped to [−1, 1] before the computation
to prevent NaN from floating-point values that drift just outside the valid range
(e.g. a dot product that computes as 1.0000000002 instead of exactly 1.0).
public static Angle Acos(this double value)
Parameters
valuedoubleThe cosine value; automatically clamped to [−1, 1].
Returns
- Angle
An UnitsNet.Angle in [0°, 180°].
Examples
Angle a = 1.0.Acos(); // 0°
Angle b = 0.0.Acos(); // 90°
Angle c = 1.0000000002.Acos(); // 0° — FP drift clamped safely
Asin(double)
Returns the angle whose sine is value (inverse sine / arc-sine).
The result is in [−90°, 90°]. The input must be in [−1, 1]; values outside that range
cause NaN (use Acos(double) note for FP-safety guidance).
Example use: computing the angle of incidence from a ratio, or finding an angle from a known sine value.
public static Angle Asin(this double value)
Parameters
valuedoubleThe sine value, in [−1, 1].
Returns
- Angle
An UnitsNet.Angle in [−90°, 90°].
Examples
Angle a = 1.0.Asin(); // 90°
Angle b = 0.5.Asin(); // 30°
Atan2(double, double)
Returns the angle whose tangent is the quotient y/x,
using the signs of both arguments to determine the correct quadrant (two-argument arc-tangent).
This is the standard atan2(y, x) function. Unlike single-argument arctangent it
returns a full 360° range: East (positive X) = 0°, North (positive Y) = 90°,
West (negative X) = 180°, South (negative Y) = −90° (i.e. 270° in definite form).
Common use: computing a direction angle from a displacement vector's Y and X components.
public static Angle Atan2(this double y, double x)
Parameters
ydoubleThe Y component of the vector (the numerator).
xdoubleThe X component of the vector (the denominator).
Returns
- Angle
An UnitsNet.Angle in [−180°, 180°].
Examples
Angle east = 0.0.Atan2(1.0); // 0° — pointing right
Angle north = 1.0.Atan2(0.0); // 90° — pointing up
Angle diag = 1.0.Atan2(1.0); // 45° — north-east
Cos(Angle)
Returns the cosine of the angle.
public static double Cos(this Angle value)
Parameters
valueAngleThe angle.
Returns
DivideBy(Torque, MassMomentOfInertia)
Divides a UnitsNet.Torque by a UnitsNet.MassMomentOfInertia to produce a
UnitsNet.RotationalAcceleration. Implements Newton's second law for rotation:
α = τ / I.
The result is always expressed in SI units (rad/s²) because radians are dimensionless — there is no unit-preserving mapping between torque units, inertia units, and angular acceleration units.
public static RotationalAcceleration DivideBy(this Torque torque, MassMomentOfInertia inertia)
Parameters
torqueTorqueThe applied torque τ (N·m or equivalent).
inertiaMassMomentOfInertiaThe moment of inertia I (kg·m² or equivalent).
Returns
- RotationalAcceleration
The angular acceleration α in rad/s².
Lerp(Angle, Angle, double)
Interpolates between two angles along the shortest arc, returning the angle
at parameter t along that arc.
Standard linear interpolation (a + (b − a) * t) breaks near the 0°/360° seam:
lerping from 350° to 10° naively gives 180° at t = 0.5, but the correct answer
is 0° (the short way through 360°). This method always takes the shortest path.
t = 0 returns from; t = 1 returns to.
Values outside [0, 1] extrapolate beyond the endpoints.
public static Angle Lerp(this Angle from, Angle to, double t)
Parameters
fromAngleThe starting angle.
toAngleThe target angle.
tdoubleThe interpolation parameter (0 = start, 1 = end).
Returns
- Angle
An interpolated UnitsNet.Angle normalised to [0°, 360°).
Examples
// Short-path wrap: 350° to 10° at midpoint = 0°, not 180°
Angle mid = Angle.FromDegrees(350).Lerp(Angle.FromDegrees(10), 0.5); // 0°
// Normal case
Angle q = Angle.FromDegrees(0).Lerp(Angle.FromDegrees(90), 0.5); // 45°
MultiplyBy(RotationalAcceleration, Duration)
Multiplies a UnitsNet.RotationalAcceleration by a UnitsNet.Duration to produce
a UnitsNet.RotationalSpeed. Implements angular kinematics: ω = α·t.
The result is always expressed in SI units (rad/s) because radians are dimensionless — there is no unit-preserving mapping between angular acceleration units, duration units, and angular speed units.
public static RotationalSpeed MultiplyBy(this RotationalAcceleration acceleration, Duration duration)
Parameters
accelerationRotationalAccelerationThe angular acceleration α (rad/s² or equivalent).
durationDurationThe elapsed time t.
Returns
- RotationalSpeed
The angular velocity ω in rad/s.
NormalizeToDefinite(Angle)
Normalizes an angle to the half-open range [0°, 360°), wrapping negative angles and angles ≥ 360° into that range.
Examples: −45° → 315°, 405° → 45°, 180° → 180°.
This is the canonical form used when storing a Direction2's angle: every
direction has exactly one representation in [0°, 360°).
public static Angle NormalizeToDefinite(this Angle value)
Parameters
valueAngleThe angle to normalize.
Returns
- Angle
An equivalent UnitsNet.Angle in [0°, 360°).
Examples
Angle a = Angle.FromDegrees(-45).NormalizeToDefinite(); // 315°
Angle b = Angle.FromDegrees(405).NormalizeToDefinite(); // 45°
NormalizeToSigned(Angle)
Normalizes an angle to the half-open range (−180°, 180°], wrapping values outside that range into the equivalent signed angle.
Examples: 350° → −10°, −270° → 90°, 180° → 180°.
This form is useful when the sign of the angle carries meaning — for example,
a positive arc sweep means counter-clockwise and a negative sweep means clockwise.
public static Angle NormalizeToSigned(this Angle value)
Parameters
valueAngleThe angle to normalize.
Returns
- Angle
An equivalent UnitsNet.Angle in (−180°, 180°].
Examples
Angle a = Angle.FromDegrees(350).NormalizeToSigned(); // −10°
Angle b = Angle.FromDegrees(-270).NormalizeToSigned(); // 90°
Angle c = Angle.FromDegrees(180).NormalizeToSigned(); // 180°
Sin(Angle)
Returns the sine of the angle.
public static double Sin(this Angle value)
Parameters
valueAngleThe angle.
Returns
Sqrt(Area)
Returns the square root of an area as a length.
This is the inverse of Squared(Length): converting an area back to a length. It appears naturally when computing distances from squared quantities, such as finding the side length of a square from its area, or turning a squared-distance result back into a distance.
The result preserves the input unit: Area.FromSquareFeet(9).Sqrt() returns
a UnitsNet.Length in feet with value 3. For units with no direct square-root
counterpart (e.g. UnitsNet.Units.AreaUnit.Acre), the result falls back to meters.
public static Length Sqrt(this Area value)
Parameters
valueAreaThe area to take the square root of. Must be non-negative.
Returns
- Length
A UnitsNet.Length equal to
√area.
Examples
Length side = Area.FromSquareMeters(9).Sqrt(); // 3 m
Squared(Length)
Returns the square of a length as an area.
This is equivalent to length * length but reads more naturally in geometric
formulas. For example, the squared distance between two points appears in the distance
formula before taking the square root.
The result preserves the input unit: Length.FromFeet(4).Squared() returns
Area.FromSquareFeet(16), not Area.FromSquareMeters(...). For units with
no direct squared counterpart (e.g. UnitsNet.Units.LengthUnit.Hectometer), the result
falls back to square meters.
public static Area Squared(this Length value)
Parameters
valueLengthThe length to square.
Returns
- Area
An UnitsNet.Area equal to
length².
Examples
Area a = Length.FromMeters(4).Squared(); // 16 m²
Tan(Angle)
Returns the tangent of the angle.
Tangent is defined as sin(θ) / cos(θ). It is undefined (±∞) at 90° and 270°.
This is useful for slope calculations and right-triangle geometry: if you know one
side and the angle, tan(θ) gives the ratio of the opposite to adjacent side.
public static double Tan(this Angle value)
Parameters
valueAngleThe angle.
Returns
Examples
double slope = Angle.FromDegrees(0).Tan(); // => 0.0
double rise = Angle.FromDegrees(30).Tan(); // 0.577