简体   繁体   English

long / int除法中出现意外结果

[英]Unexpected result in long/int division

I have values like this: 我有这样的价值观:

long millis = 11400000;
int consta = 86400000;
double res = millis/consta;

The question is: why res equals 0.0 (instead of ca. 0.131944 )? 问题是:为什么res等于0.0 (而不是大约0.131944 )? It's stored in double so there should be no rounding right? 它存储在double所以应该没有向右舍入?

When you are using a binary operator, both arguments should be of a same type and the result will be in their type too. 当您使用二元运算符时,两个参数应该是相同的类型,结果也将是它们的类型。 When you want to divide (int)/(long) it turns into (long)/(long) and the result is (long) . 当你想要除(int)/(long)它变成(long)/(long) ,结果是(long) you shouldmake it (double)/(long) or (int)/(double) to get a double result. 你应该使用它(double)/(long)(int)/(double)来获得双重结果。 Since double is greater that int and long, int and long will be turned into double in (double)/(long) and (int)/(double) 由于double大于int和long,int和long将变为double (double)/(long)(int)/(double)

Because you are dividing a long by an int you get an long results. 因为你将long除以int得到了long结果。 What you are effectively doing is 你实际做的是

double res = (double) (millis/consta);

as millis/consta is 0 , when cast to double is 0.0 millis/consta0 ,当转换为double0.0

Try the following to divide a double by an int and get a double result. 尝试以下操作将double除以int并获得double结果。

double res = (double) millis/consta;

which is the same as 这是一样的

double res = ((double) millis)/((double) consta));

You are doing long division (int gets cast to long) so you get long values, which are integers (so, 0) 你正在进行long除法(int得到转换为long)所以你得到long值,这是整数(所以,0)

You should do 你应该做

  double res = (double) millis / consta;

Once one of the values is casted to double, the other is too casted so the operation uses the same type in both operators. 一旦其中一个值转换为double,另一个值转换为so-so,因此操作在两个运算符中使用相同的类型。

millis/consta is an integer division, which results in 0 . millis/consta是一个整数除法,结果为0 the casting in the line: 线下铸造:

double res = millis/consta;

is done on the result: 完成结果:

double res = (double)(millis/consta);

What you need to do is to cast one of the operands: 你需要做的是施放一个操作数:

double res = (double)millis/consta;

The resulting type of a long and int devision will be a long, which can't hold decimals. 结果类型的long和int devision将是一个long,它不能包含小数。

you want to cast it to a double before you assign it 您希望在分配之前将其强制转换为双精度数

When this happens it automatically downcasts to int to perform the operation. 发生这种情况时,它会自动向下转换为int以执行操作。 Thy writing it like: 你写得像:

long millis = 11400000;
int consta = 86400000;
double res = ((double)millis)/((double)consta);

And it will work. 它会奏效。

This is because long: The long data type is a 64-bit signed two's complement integer. 这是因为long:long数据类型是64位带符号的二进制补码整数。 int: The int data type is a 32-bit signed two's complement integer. int:int数据类型是32位带符号的二进制补码整数。

See Primitive type It means both are integer. 请参阅原始类型它表示两者都是整数。 Since we divide long by integer, result is long 0. But you assign this value to res(double) value and print it. 由于我们将long除以整数,因此结果为长0.但是您将此值赋给res(double)值并打印它。 So the result 0.0 is shown. 因此显示结果0.0。

long millis = 11400000;
int consta = 86400000;

System.out.println("millis/consta = " + millis/consta); // print 0

double res = millis/consta;
System.out.println("Res " + res); // print 0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM