Java:帮助进行基本输出基本值算术。输出为0,不知道为什么?
我在使用 Java 进行基本数学计算时遇到一些问题。我不知道为什么仅在一种情况下乘以 n*n*n 就会得到 0 。 (见下文)我需要这个不为零,因为我必须除以timing/n*n*n 才能获得大O 性能。另外,如果我能做到这一点,输出可能看起来像 0.00000,但我想将其乘以 100,000,这样我就可以看到数字并找到性能趋势。
您可以在前两个数字列中看到 n 的值和计时。
- n 是整数,
- 计时是长
这是我的输出语句,
System.out.println(fmt.format("%20s %20d %20d %20d %20d %20d%n", "Alg. 1", n, timing, n*n*n, timing/(n*n), timing /*((double)timing/((double)n*Math.log((double)n)))*/));
我的结果,
Alg. 1 256 4 16777216 0 4
Alg. 1 512 22 134217728 0 22
Alg. 1 1024 173 1073741824 0 173
Alg. 1 2048 1362 0 0 1362
请记住我也需要执行这个对数数学。任何提示或修复也将不胜感激!
注意:我在语句 n*n*n 中根本没有除法,并且在第四列第四行中得到 0。
有人也可以告诉我如何让它输出准确的小数位数而不仅仅是 0.000000 吗?我的新算术是 ((float)(timing/((long)n)*n*n)*100000。如上所述,我乘以 100000,因为我想看到小数位中的内容。我应该看到 0.0159139当 n 为 2048,计时为 1362 时,这个方程。我只看到 0.000000 有什么建议吗?
I am having some issues doing basic math with Java. I don't know why I am getting 0 as a result of multiplying n*n*n only in one case. (see below) I need this not to be zero because I have to divide timing/n*n*n to get that big O performance. Also if I can get that working the output may look like 0.00000 but I want to multiply that by like 100,000 just so I can see numbers and find any trends in performance.
You can see the values of n and timing in the first two number columns.
- n is Integer
- timing is Long
This is my output statement,
System.out.println(fmt.format("%20s %20d %20d %20d %20d %20d%n", "Alg. 1", n, timing, n*n*n, timing/(n*n), timing /*((double)timing/((double)n*Math.log((double)n)))*/));
My results,
Alg. 1 256 4 16777216 0 4
Alg. 1 512 22 134217728 0 22
Alg. 1 1024 173 1073741824 0 173
Alg. 1 2048 1362 0 0 1362
Please keep in mind I need to perform this log math also. Any tips or fixes for that would also be appreciated!
Note: I am not dividing at all in the statement n*n*n and I am getting 0 in column four row four.
Can someone also please tell me how to get this to output decimal places that are accurate not just 0.000000. My new arithmetic is ((float)(timing/((long)n)*n*n)*100000. I am multiplying by 100000 as said above because I want to see something in the decimal places. I should be seeing 0.0159139 with this equation when n is 2048 and timing is 1362. I just see 0.000000 though. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
20483 是 233,使用 32 位算术会溢出。使用
long
或double
来处理这么大的数字。(ideone.com 上的演示)
20483 is 233 which overflows using 32-bit arithmetic. Use a
long
or adouble
to handle numbers this big.(Demo at ideone.com)
如果将一个整数除以另一个大于该整数的整数,则会得到零结果。
将分子或分母设为双精度,就可以了。
If you divide one integer by another that's greater than it, you'll get a zero result.
Make either numerator or denominator a double and you'll be fine.
2048 的立方是 8589934592,比 int 的最大值大得多。实际上是 2 * (2^32)。如果您尝试将其转换为有符号的 32 位 int,它会环绕两次,最终得到零。使用多头!
2048 cubed is 8589934592, much bigger than the max value of an int. It's actually 2 * (2^32). If you try to convert it to a signed 32-bit int, it wraps around twice and you end up with zero. Use longs!