Based on the implementation for Int32
:
public static int MinMagnitude(int x, int y)
{
int absX = x;
if (absX < 0)
{
absX = -absX;
if (absX < 0)
{
return y;
}
}
int absY = y;
if (absY < 0)
{
absY = -absY;
if (absY < 0)
{
return x;
}
}
if (absX < absY)
{
return x;
}
if (absX == absY)
{
return IsNegative(x) ? x : y;
}
return y;
}
This method returns number with minimal absolute value, i.e. magnitude (if equal - the negative of the two preferred).
UPD
I can't get access to IEEE 754 2019 where minimumMagnitude
is defined, but from the PDF on "The Removal/Demotion of MinNum and MaxNum Operations from IEEE 754™-2018", the original minNumMag
was defined as:
minNumMag(x, y) is the canonicalized number x if | x| < | y|
, y if | y| < | x|
, otherwise minNum(x,y)
.
As the doc states - reason for removal was non-associativity for NaN
handling which, I understand is "fixed" in the new standard.
For this the double
implementation can give mode detail, I think:
public static double MinMagnitude(double x, double y)
{
// This matches the IEEE 754:2019 `minimumMagnitude` function
//
// It propagates NaN inputs back to the caller and
// otherwise returns the input with a lesser magnitude.
// It treats +0 as lesser than -0 as per the specification.
double ax = Abs(x);
double ay = Abs(y);
if ((ax < ay) || double.IsNaN(ax))
{
return x;
}
if (ax == ay)
{
return double.IsNegative(x) ? x : y;
}
return y;
}