Overview
The module provides extended math primitives.
MathEx
MathEx
is a class provided by Gapotchenko.FX.Math
module.
It offers extended mathematical functions,
and serves as an addendum to a conventional System.Math
class.
Some functions provided by MathEx
class are described below.
Swap
The swap operation is a widely demanded primitive:
Swap<T>(ref T val1, ref T val2)
Despite a trivial implementation, swap operation was found highly desirable during real-life coding sessions.
Swap
primitive allows to keep a mind of developer more focused on important things,
instead of writing tedious code like:
T temp = val1;
val1 = val2;
val2 = temp;
For comparison, please take a look at a concise version of the same:
MathEx.Swap(ref val1, ref val2);
The expression above gives an immediate improvement in readability.
Note that some .NET languages have a built-in support for swap operation.
For example, a modern C# code can swap val1
and val2
values using tuples, which is a highly recommended approach:
(val1, val2) = (val2, val1);
Min/Max for Three Values
The conventional Math
class provides ubiquitous Min
/Max
primitives for two values.
However, such a limitation on number of values was proven counter-productive on more than several occasions.
MathEx
fixes that by providing Min
/Max
operations for three values:
using Gapotchenko.FX.Math;
Console.WriteLine(MathEx.Max(1, 2, 3));
Min/Max for Any Comparable Type
Ever found yourself trying to find the maximum System.DateTime
value? Or System.Version
?
MathEx
provides Min
/Max
operations for any comparable type:
using Gapotchenko.FX.Math;
var currentProgress = new DateTime(2012, 1, 1);
var desiredProgress = new DateTime(2025, 1, 1);
var fxProgress = MathEx.Max(currentProgress, desiredProgress);
Console.WriteLine(fxProgress);
Commonly Used Types
Gapotchenko.FX.Math.MathEx
Other Modules
Let's continue with a look at some other modules provided by Gapotchenko.FX:
Or look at the full list of modules.