Are there any differences between decimal.Negate(myDecimal) and myDecimal * -1 (except maybe readability)?
Are there any differences between decimal.Negate(myDecimal) and myDecimal * -1 (except maybe readability)?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I suspect
Negateexists 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 * -1it 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.)