整型和双打做除法 [英] Ints and Doubles doing division

查看:124
本文介绍了整型和双打做除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短版:为什么不让我有强迫60,和INT,到双,这样我就可以利用分区与另一双,如果我在乎的小数部分。

Short version: Why don't I have to coerce 60, and int, into a double, so that I can use division with another double if I DO care about the fractional part?

长版:我的老板叫我出去线code。我测试了它,它是工作完全正常,但他认为我有一个bug等待发生。

Long version: my boss called me out on a line of code. I tested it and it is working perfectly fine, but he thinks I have a bug waiting to happen.

int durationinseconds = 61;  // This is actually filled from a double.tryparse 
                             // from a string value out of an xml doc.
int durationinminutes = (int)Math.Ceiling((double)durationinseconds / 60);

我的code应该接受从XML文档秒#,然后找出分钟#,始终围捕。 60秒= 1分钟,61秒= 2分钟,等等。

My code is supposed to get the # of seconds from the XML doc, then figure out the # of minutes, always rounding up. 60 seconds = 1 minute, 61 seconds = 2 minutes, etc.

我测试过我的code,但他坚持认为,/ 60的一部分应该是/ 60.0。

I've tested my code, but he INSISTS that the "/ 60" part should be "/ 60.0".

我已经与0,1,2,59,60,61,119,120,121,599,600,601,等等,它总是正确的工作了测试我的code。

I've tested my code with 0, 1, 2, 59, 60, 61, 119, 120, 121, 599, 600, 601, etc, and it always works out correctly.

在我去保护自己给他,我大多明白为什么他认为我需要强迫60是一个小数,因为他认为,如果我用一个int值,然后双击/ INT会导致整数值,有效地丢弃的小数部分。

Before I go defend myself to him, I mostly understand why he thinks that I need to coerce 60 to be a decimal, because he thinks that if I use an int value then a double / int will result in an integer value, effectively dropping the fractional part.

不过,这并没有发生在这里,我也解释不清楚为什么。所以,这是我的问题:为什么不把我当分裂双重使用60.0,如果我想使用的小数部分

However, that is not happening here, and I can't exactly explain why. So, that is my question: WHY don't I have to use 60.0 when dividing a double if I want to use the fractional part?

推荐答案

下面是相关的除法运算符:

Here are the relevant division operators:

public static double operator /(double x, double y)
public static int operator /(int x, int y)

有一个从 INT 的隐式转换为,而不是其他全面...因此,如果你把一个 INT INT ,您将使用以整数形式......但是如果操作数是一个双层,它会使用双形式。

There is an implicit conversion from int to double, but not the other way round... so if you divide an int by an int, you'll use the integer form... but if either operand is a double, it will use the double form.

有没有必要让两个操作数 - 但你的code的的至少缩短,如果你做除数操作数双倍代替铸造:

There's no need to make both operands double - but your code would be at least shorter if you made the divisor operand a double instead of casting:

int durationInMinutes = (int) Math.Ceiling(durationInSeconds / 60.0);

我个人而言,更容易阅读...但它是一个个人的选择。

Personally I find that easier to read... but it's a personal choice.

如果你想的证明的给你的老板,它的真正做浮点除法,使用iladsm或反射器(以IL模式)的code - 它会显示一个 ldc.r8 指令的常量,这意味着一个值。

If you want to prove to your boss that it's really doing floating point division, use iladsm or reflector (in IL mode) on your code - it will show an ldc.r8 instruction for the constant, which means a double value.

这篇关于整型和双打做除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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