Class Fixed64Extensions
- Namespace
- FixedMathSharp
- Assembly
- FixedMathSharp.dll
Provides extension methods for the Fixed64 type, enabling additional mathematical, conversion, and comparison operations using a fluent syntax.
public static class Fixed64Extensions
- Inheritance
-
Fixed64Extensions
- Inherited Members
Methods
Abs(Fixed64)
Returns the absolute value of a Fixed64 number.
public static Fixed64 Abs(this Fixed64 value)
Parameters
valueFixed64
Returns
AbsLessThan(Fixed64, Fixed64)
Checks if the absolute value of x is less than y.
public static bool AbsLessThan(this Fixed64 x, Fixed64 y)
Parameters
Returns
- bool
True if |x| < y; otherwise false.
Acos(Fixed64)
Returns the arccosine of the specified number x, calculated using a combination of the atan and sqrt functions.
public static Fixed64 Acos(this Fixed64 f)
Parameters
fFixed64
Returns
- Fixed64
The arccosine of x in radians.
Exceptions
- ArgumentOutOfRangeException
Thrown if x is outside the domain [-1, 1].
Asin(Fixed64)
Returns the arc-sine of a fixed-point number x, which is the angle in radians whose sine is x, using a combination of a Taylor series expansion and trigonometric identities.
For values of x near ±1, the identity asin(x) = π/2 - acos(x) is used for stability. For values of x near 0, a Taylor series expansion is used.
public static Fixed64 Asin(this Fixed64 f)
Parameters
fFixed64
Returns
- Fixed64
The arc-sine of x in radians.
Exceptions
- ArithmeticException
Thrown if x is outside the domain [-1, 1].
Atan(Fixed64)
Returns the arctangent of the specified number, using a more accurate approximation for larger values. This function has at least 7 decimals of accuracy.
public static Fixed64 Atan(this Fixed64 f)
Parameters
fFixed64
Returns
Atan2(Fixed64, Fixed64)
Computes the angle whose tangent is the quotient of two specified numbers.
public static Fixed64 Atan2(this Fixed64 a, Fixed64 b)
Parameters
Returns
- Fixed64
An angle, θ, measured in radians, such that -π ≤ θ ≤ π, and tan(θ) = y / x, taking into account the quadrants of the inputs to determine the sign of the result.
Remarks
Uses a fixed-point arithmetic approximation for the arc tangent function, which is more efficient than using floating-point arithmetic, especially on systems where floating-point operations are expensive.
Ceil(Fixed64)
Returns the smallest integral value that is greater than or equal to the specified number.
public static Fixed64 Ceil(this Fixed64 value)
Parameters
valueFixed64
Returns
CeilToInt(Fixed64)
Rounds up the Fixed64 value to the nearest integer.
public static int CeilToInt(this Fixed64 x)
Parameters
xFixed64
Returns
CeilToLong(Fixed64)
Rounds up the Fixed64 value to the nearest long.
public static long CeilToLong(this Fixed64 x)
Parameters
xFixed64
Returns
Clamp(Fixed64, Fixed64, Fixed64)
Clamps a fixed-point value between the given minimum and maximum values.
public static Fixed64 Clamp(this Fixed64 value, Fixed64 min, Fixed64 max)
Parameters
Returns
Clamp01(Fixed64)
Clamps value between 0 and 1 and returns value.
public static Fixed64 Clamp01(this Fixed64 f1)
Parameters
f1Fixed64
Returns
ClampOne(Fixed64)
Clamps the value between -1 and 1 inclusive.
public static Fixed64 ClampOne(this Fixed64 f1)
Parameters
f1Fixed64The Fixed64 value to clamp.
Returns
- Fixed64
Returns a value clamped between -1 and 1.
Cos(Fixed64)
Computes the cosine of a given angle in radians using a sine-based identity transformation.
public static Fixed64 Cos(this Fixed64 f)
Parameters
fFixed64
Returns
- Fixed64
The cosine of the given angle, in fixed-point format.
Remarks
- Instead of directly approximating cosine, this function derives
cos(x)using the identitycos(x) = sin(x + π/2). - The underlying sine function uses complementary reduced-range sine and cosine polynomials so quadrant anchors remain continuous.
- The function automatically normalizes input values to the range [-π, π] for stability.
CubicInterpolate(Fixed64, Fixed64, Fixed64, Fixed64, Fixed64)
Performs cubic interpolation between two points with tangents at those points.
public static Fixed64 CubicInterpolate(this Fixed64 p0, Fixed64 p1, Fixed64 m0, Fixed64 m1, Fixed64 t)
Parameters
p0Fixed64The first point.
p1Fixed64The second point.
m0Fixed64The tangent at
p0.m1Fixed64The tangent at
p1.tFixed64A value between 0 and 1 that represents the interpolation factor.
Returns
- Fixed64
The interpolated value between
p0andp1.
FastAdd(Fixed64, Fixed64)
Adds two fixed-point numbers by adding their raw Q32.32 payloads without saturation.
public static Fixed64 FastAdd(this Fixed64 a, Fixed64 b)
Parameters
Returns
Remarks
This is an unchecked hot-path helper. Use it only when the raw sum is known to fit in long or raw wraparound is an intentional part of the algorithm. Use operator +(Fixed64, Fixed64) for the public saturating add contract.
FastDiv(Fixed64, Fixed64)
Divides two fixed-point numbers with an optimized path for known-positive divisors.
public static Fixed64 FastDiv(this Fixed64 a, Fixed64 b)
Parameters
Returns
Remarks
This helper preserves the same deterministic rounding, divide-by-zero, and saturation semantics
as operator /(Fixed64, Fixed64). The fast path is only used when
y is positive; non-positive divisors fall back to the guarded division operator.
Prefer the operator unless the divisor positivity invariant is already proven by the caller.
FastMod(Fixed64, Fixed64)
Computes the raw remainder of two fixed-point numbers without special-case guards.
public static Fixed64 FastMod(this Fixed64 a, Fixed64 b)
Parameters
Returns
Remarks
This is an unchecked hot-path helper. It delegates directly to the raw long remainder operation, so raw zero divisors and integer edge cases follow runtime integer remainder behavior. Use operator %(Fixed64, Fixed64) for the public guarded remainder contract.
FastMul(Fixed64, Fixed64)
Multiplies two fixed-point numbers using unchecked Q32.32 partial products.
public static Fixed64 FastMul(this Fixed64 a, Fixed64 b)
Parameters
Returns
Remarks
This is an unchecked hot-path helper. It skips the full-width overflow/saturation path used by operator *(Fixed64, Fixed64) and truncates discarded fractional bits instead of applying the operator's round-half-to-even behavior. Use it only when inputs are constrained and that precision tradeoff is acceptable. Integral operands take a direct raw multiplication path.
FastSub(Fixed64, Fixed64)
Subtracts two fixed-point numbers by subtracting their raw Q32.32 payloads without saturation.
public static Fixed64 FastSub(this Fixed64 a, Fixed64 b)
Parameters
Returns
Remarks
This is an unchecked hot-path helper. Use it only when the raw difference is known to fit in long or raw wraparound is an intentional part of the algorithm. Use operator -(Fixed64, Fixed64) for the public saturating subtract contract.
Floor(Fixed64)
Returns the largest integer less than or equal to the specified number (floor function). Efficiently zeroes out the fractional part.
public static Fixed64 Floor(this Fixed64 value)
Parameters
valueFixed64
Returns
FloorToInt(Fixed64)
Rounds down the Fixed64 value to the nearest integer.
public static int FloorToInt(this Fixed64 x)
Parameters
xFixed64
Returns
FloorToLong(Fixed64)
Rounds down the Fixed64 value to the nearest long.
public static long FloorToLong(this Fixed64 x)
Parameters
xFixed64
Returns
FuzzyComponentEqual(Fixed64, Fixed64, Fixed64)
Helper method to compare individual vector components for approximate equality, allowing a fractional difference. Handles zero components by only using the allowed percentage difference.
public static bool FuzzyComponentEqual(this Fixed64 a, Fixed64 b, Fixed64 percentage)
Parameters
Returns
Hypot(Fixed64, Fixed64)
Calculates the hypotenuse of a right triangle given sides a and b using the Pythagorean theorem: sqrt(a^2 + b^2).
public static Fixed64 Hypot(this Fixed64 a, Fixed64 b)
Parameters
Returns
- Fixed64
The length of the hypotenuse.
IsInteger(Fixed64)
Returns true if the number has no decimal part (i.e., if the number is equivalent to an integer) and False otherwise.
public static bool IsInteger(this Fixed64 value)
Parameters
valueFixed64
Returns
Lerp(Fixed64, Fixed64, Fixed64)
Linearly interpolates between two fixed-point values based on a given interpolation factor.
public static Fixed64 Lerp(this Fixed64 a, Fixed64 b, Fixed64 t)
Parameters
Returns
- Fixed64
The interpolated value between
fromandto.
Remarks
The interpolation is clamped between from and to based on the value of t.
If t is less than 0, the result is from. If t is greater than 1, the result is to.
LessThanEpsilon(Fixed64)
Checks if the value is less than epsilon (i.e., effectively zero). Useful for determining if a value is close enough to zero with a given precision.
public static bool LessThanEpsilon(this Fixed64 d)
Parameters
dFixed64
Returns
Ln(Fixed64)
Returns the natural logarithm of a specified fixed-point number. Provides at least 7 decimals of accuracy.
public static Fixed64 Ln(this Fixed64 f)
Parameters
fFixed64
Returns
Log2(Fixed64)
Returns the base-2 logarithm of a specified number. Provides at least 9 decimals of accuracy.
public static Fixed64 Log2(this Fixed64 f)
Parameters
fFixed64
Returns
Remarks
This implementation is based on Clay. S. Turner's fast binary logarithm algorithm (C. S. Turner, "A Fast Binary Logarithm Algorithm", IEEE Signal Processing Mag., pp. 124,140, Sep. 2010.)
MoreThanEpsilon(Fixed64)
Checks if the value is greater than epsilon (positive or negative). Useful for determining if a value is effectively non-zero with a given precision.
public static bool MoreThanEpsilon(this Fixed64 d)
Parameters
dFixed64
Returns
Pow(Fixed64, Fixed64)
Raises the base number b to the power of exp. Uses logarithms to compute power efficiently for fixed-point values.
public static Fixed64 Pow(this Fixed64 f, Fixed64 exponent)
Parameters
Returns
Exceptions
- DivideByZeroException
The base was Fixed64.Zero, with a negative expFixed64.Onent
- ArgumentOutOfRangeException
The base was negative, with a non-Fixed64.Zero expFixed64.Onent
Pow2(Fixed64)
Raises 2 to the power of x. Provides high accuracy for small values of x.
public static Fixed64 Pow2(this Fixed64 f)
Parameters
fFixed64
Returns
Round(Fixed64, MidpointRounding)
Rounds a fixed-point number to the nearest integral value, based on the specified rounding mode.
public static Fixed64 Round(this Fixed64 value, MidpointRounding mode = MidpointRounding.ToEven)
Parameters
valueFixed64modeMidpointRounding
Returns
RoundToInt(Fixed64)
Rounds the Fixed64 value to the nearest integer.
public static int RoundToInt(this Fixed64 x)
Parameters
xFixed64
Returns
RoundToLong(Fixed64)
Rounds the Fixed64 value to the nearest long.
public static long RoundToLong(this Fixed64 x)
Parameters
xFixed64
Returns
RoundToPrecision(Fixed64, int, MidpointRounding)
Rounds a fixed-point number to a specific number of decimal places.
public static Fixed64 RoundToPrecision(this Fixed64 value, int places, MidpointRounding mode = MidpointRounding.ToEven)
Parameters
valueFixed64placesintmodeMidpointRounding
Returns
Sign(Fixed64)
Returns a number indicating the sign of a Fix64 number. Returns 1 if the value is positive, 0 if is 0, and -1 if it is negative.
public static int Sign(this Fixed64 value)
Parameters
valueFixed64
Returns
Remarks
Optimized for branchless comparison.
Sin(Fixed64)
Computes the sine of a given angle in radians using complementary reduced-range polynomial approximations.
public static Fixed64 Sin(this Fixed64 f)
Parameters
fFixed64
Returns
- Fixed64
The sine of the given angle, in fixed-point format.
Remarks
The input is normalized to [-π, π], reflected into [0, π/2], and evaluated as sine on [0, π/4] or cosine on [0, π/4]. Exact quadrant anchors remain exact without introducing a discontinuity beside them.
SmoothStep(Fixed64, Fixed64, Fixed64)
Performs a smooth step interpolation using a cubic Hermite curve between two values.
public static Fixed64 SmoothStep(this Fixed64 a, Fixed64 b, Fixed64 t)
Parameters
aFixed64The starting value.
bFixed64The ending value.
tFixed64A value between 0 and 1 that represents the interpolation factor.
Returns
- Fixed64
The interpolated value between
aandb.
Remarks
The interpolation follows a cubic Hermite curve where the function starts at a,
accelerates, and then decelerates towards b, ensuring smooth transitions.
Sqrt(Fixed64)
Returns the square root of a specified fixed-point number.
public static Fixed64 Sqrt(this Fixed64 f)
Parameters
fFixed64
Returns
Squared(Fixed64)
Squares the Fixed64 value.
public static Fixed64 Squared(this Fixed64 value)
Parameters
valueFixed64The Fixed64 value to square.
Returns
- Fixed64
The squared value.
Tan(Fixed64)
Returns the tangent of x.
public static Fixed64 Tan(this Fixed64 f)
Parameters
fFixed64
Returns
Remarks
This function is not well-tested. It may be wildly inaccurate.
ToDegrees(Fixed64)
Converts the angle in radians to degrees.
public static Fixed64 ToDegrees(this Fixed64 angleInRadians)
Parameters
angleInRadiansFixed64
Returns
ToRadians(Fixed64)
Converts the angle in degrees to radians.
public static Fixed64 ToRadians(this Fixed64 angleInDegrees)
Parameters
angleInDegreesFixed64