Unexpected result in long/int division(意外结果导致长/整数除法)
问题描述
我有这样的价值观:
long millis = 11400000;int 常数 = 86400000;双分辨率 = 毫/常数;
问题是:为什么 res
等于 0.0
(而不是 ca. 0.131944
)?它存储在 double
中,所以应该没有四舍五入吧?
当你使用二元运算符时,两个参数的类型应该相同,结果也应该是它们的类型.当你想划分 (int)/(long)
它变成 (long)/(long)
结果是 (long)
.您应该将其设为 (double)/(long)
或 (int)/(double)
以获得双重结果.由于 double 大于 int 和 long,所以 int 和 long 在 (double)/(long)
和 (int)/(double)
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
)? It's stored in double
so there should be no rounding right?
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)
. you shouldmake it (double)/(long)
or (int)/(double)
to get a double result. Since double is greater that int and long, int and long will be turned into double in (double)/(long)
and (int)/(double)
这篇关于意外结果导致长/整数除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:意外结果导致长/整数除法
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01