Sign function

Hi All,

Sign function doesn’t work correctly in AX 2009 and 2012. It should returns -1 for negative numbers, 0 for zero and +1 for positive numbers. But it gives -1 for negative and +1 for others.

Sign at Global:

static real sign(real num)
{
return num >= 0 ? 1 : -1;
}

My correction:

static int sign(real num)
{

if (num < 0)

return -1;

else
return num >= 0 ? 1 : 0;
}

Hi Metin,

Do you have any idea where this method is used in the standard and what regressions can be caused changing this code?
Also I’m sorry to say, but the outcome of your code is still the same as the standard code…
If you really want to have a zero, probably estimate the impact first. Maybe it would be better to create a new “MetinSign” function, but then also don’t use “num >= 0” but “num > 0”.

static int sign(real num)
{

if (num < 0)

return -1;

else
return num > 0 ? 1 : 0;
}

Hi Andre,
Unfortunately seem I did a mistake. Thanks Kunal to correction. :slight_smile:

I just used for NoYes enums when value is 0 should be No and when value is positive should be Yes. In this case that function cause return Yes everytime.
I found sign function used at standart developments just for compare negative - positive compares. In that case you can accept zero as positive, doesn’t matter. So I think so there’s no problem with standart developments.