比较两个非整数的整数部分的最佳方法是什么?
我需要比较两个双精度数的整数部分是否不相等,我目前正在这样做:
int iA = (int)dA;
int iB = (int)dB;
if( iA != iB )
{
...
}
但我想知道是否有比这更好的方法。
谢谢。
如果我使用 Math.Truncate() 而不是转换为 int,比较两个结果双精度值是否相等仍然准确吗?
关于匈牙利表示法评论:
我自己从不使用 HN,至少不像大多数人那样。 但这是变量的语义与其类型直接相关的罕见情况之一。 我可以选择另一种语法,例如 A_As_Integer 和 B_As_NonInteger,但那会有什么区别呢?
I need to compare the integer part of two doubles for inequality and I'm currently doing this:
int iA = (int)dA;
int iB = (int)dB;
if( iA != iB )
{
...
}
but I wonder if there's a better approach than this.
Thanks.
If I used Math.Truncate() instead of a cast to int, would it still be accurate to compare the two resulting double values for equality?
About the hungarian notation comments:
I never use HN myself, not at least in the way most of people do. But this is one of these rare cases where the semantic of a variable directly relates to its type. I could have chosen another syntax like A_As_Integer and B_As_NonInteger but what would have been the difference then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Math.Truncate() 即
[编辑] 意识到,如果您要比较双精度数的整数部分,如果您的双精度数超出了可以表示为 int 的范围,则转换为 int 值首先会面临溢出的风险。
Truncate 返回 Decimal 或 double,避免了此问题。
Use Math.Truncate() i.e.
[Edit] Realized that if you are comparing integer parts of doubles, casting to int values first runs the risk of overflows should your doubles be outside the range that could be represented as int.
Truncate returns either a Decimal or a double, avoiding this issue.
是的,这是最好的方法。
假设从你问题中的措辞来看,你并不担心四舍五入......只是整数部分本身。
IE。 (int)4.1 将给出与 (int)4.9 相同的结果
正如 Ovid 所说,如果您只需要比较变量,那么您只需要在比较语句中转换双精度型。 我只关注理论。
Yes, that is best approach.
Assuming from your wording in you question you arent worried about rounding... just the integerr part itself.
ie. (int)4.1 will give the same result as (int)4.9
As Ovid stated, if you only need the variables for the comparison, then you will only need to cast the doubles in the comparison statement. I was looking only at the theory.
我同意截断是你想要的。
来自 MSDN 的一些有用信息:
截断返回任何之后保留的数字小数位已被丢弃。
它向零舍入到最接近的整数。
I agree that Truncate is what you want.
Some helpful information from MSDN:
Truncate returns the number that remains after any fractional digits have been discarded.
It rounds to the nearest integer towards zero.
就我个人而言,除非需要,否则我会尽量避免创建额外的变量。
随着代码随着时间的推移而发展,无用的额外变量会导致混乱。 当然,如果您需要这些变量,那是另一个问题:)
旁注:您似乎试图通过简单的匈牙利表示法暗示数据类型。 我可以建议不要这样做吗? 如果必须为变量添加信息前缀,请尝试为变量的用途添加前缀,而不是其类型。 如果你这样做,代码中的错误会更容易被发现:
在这种情况下,即使不知道数据是什么,看到你试图将“年龄”与“id”进行比较是一个很好的线索,表明某些东西是这里错了。
Personally, I try to avoid creating extra variables unless I need them.
As code evolves over time, having extra variables hanging around for no purpose leads to confusion. Of course, if you need those variables, that's another issue :)
Side note: you appear to be trying to hint at the data type via a simple Hungarian notation. May I recommend not doing that? If you must prefix information to your variables, try to prefix what the variable is for, rather than its type. If you do that, mistakes in code can be easier to see:
In this case, without even knowing what the data is, seeing that you're trying to compare an 'age' with an 'id' is a good clue that something is wrong here.