Are there any differences between decimal.Negate(myDecimal)
and myDecimal * -1
(except maybe readability)?
views:
131answers:
4From MSDN, decimal.Negate
:
Returns the result of multiplying the specified Decimal value by negative one.
No practical difference then, though readability is important.
Personally, I find -myDecimal
to be more readable than either (I'm no math geek, but I pretty sure all three are equivalent), but then again, I generally prefer compact notation.
If that is out, I'd go with Negate since I like to avoid magic numbers floating around in my code, and while the -1
as used there isn't really a magic number, it sure looks like one at first glance.
I suspect Negate
exists because there's a unary minus operator (op_UnaryNegation
), and it's good practice to have methods representing the equivalent functionality so that languages which don't support operator overloading can still achieve the same result.
So instead of comparing it with myDecimal * -1
it may be helpful to think of it as being an alternative way of writing -myDecimal
.
(I believe it's also optimised - given the way floating point works, negating a value is much simpler than normal multiplication; there's just a bit to flip. No need to perform any actual arithmetic.)
If you look in the .NET source with .NET Reflector, you will see the following: (getting coffee until it finally opens..)
public static decimal Negate(decimal d)
{
return new decimal(d.lo, d.mid, d.hi, d.flags ^ -2147483648);
}
Looks like this is a fancy way to say -1 due to the way decimal internally works.
If you do *-1 it maps it to the following call:
FCallMultiply(ref result, yourNumber, -1M);
which will likely produce different IL code.