.NET十进制。求和与-1相乘 [英] .NET decimal.Negate vs multiplying by -1

查看:86
本文介绍了.NET十进制。求和与-1相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

decimal.Negate(myDecimal) myDecimal * -1 之间是否有区别(也许可读性除外) ?

Are there any differences between decimal.Negate(myDecimal) and myDecimal * -1 (except maybe readability)?

推荐答案

我怀疑 Negate 存在,因为存在一元减号( op_UnaryNegation ),并且最好的方法是使用代表相同功能的方法,这样不支持运算符重载的语言仍然可以达到相同的结果。

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.

因此,将它与 myDecimal * -1 进行比较可能会有助于将其视为编写 -myDecimal 。

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.)

这篇关于.NET十进制。求和与-1相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆