.NET Micro Math library - or lack of it

I was greatly disappointed to see that the System.Math library does not offer Max, Min, or Abs functions on floating point numbers.

Does a GHI math library fill in this gap? Or do I need to write/circumvent the functions myself?

Yup GHI offers that:
http://www.ghielectronics.com/downloads/NETMF/Library%20Documentation/Index.html
GHIElectronics.NETMF.System.MathEx class

No it doesn’t!

[url]http://www.ghielectronics.com/downloads/NETMF/Library%20Documentation/html/a425963a-a256-3ad3-acaa-0502eb566ff4.htm[/url]

Max, Min, and Abs aren’t on that list!

Would it be possible to add them?

In my Visual C # 2010 Express installation, the Abs, Max and Min functions simply there

The Min, Max and Abs methods are in the core NETMF libs, GHI adds the other bigger part of the common math functions you might need.

Indeed they are included. However, those methods only take [italic]integer[/italic] arguments. These is no way for me to find the absolute value of a float, for instance.

Is this what you need?



if(f<0)
   f = -f;


That’s what I’m using at the moment. I’m just irritated that the Math library doesn’t have a floating point Abs operation. Ditto for Max and Min.

Also, technically that solution is not correct, as it gives the wrong result (-0) for -0. Not that it really matters

Negative zero?

“That’s what I’m using at the moment. I’m just irritated that the Math library doesn’t have a floating point Abs operation. Ditto for Max and Min.”

There is much better things to be irritated about. This is easy to workaround or create an extention method. Libraries on a micro device always involve choice. Add one thing, means your removing something else.

That was also my thought… Too bad you can’t write extension methods for a static class like Math and MathEx. So that is not an option.

But those functions are so easy to implement inline an extension method would be overkill :wink:


v = v < 0 ? -v : v;
v = v1 < v2 ? v1 : v2;
v = v1 > v2 ? v1 : v2; 

Not sure Math is the right place to hang these either. To me, seems more logical to have dot access on the type. I like to codify thes little algos and then just bring in the ones I use for a project and can reuse tested code and not have to think as much. Preferences will vary, but at least we have the option available to extend per your taste.

int x = -100;
int y = 100;
int min = x.Min(y);
int max = x.Max(y);
int r = 4;
bool inR = r.Between(-1, 4);

public static int Min(this int value1, int value2)
{
    if (value1 < value2) return value1;
    return value2;
}
public static int Max(this int value1, int value2)
{
    if (value1 > value2) return value1;
    return value2;
}
public static bool Between(this int value, int low, int high)
{
    if (value >= low && value <= high) return true;
    return false;
}