简单方程未通过单元测试

发布于 2024-12-03 18:51:11 字数 425 浏览 2 评论 0原文

我有一个接受 2 个整数作为参数并返回的方法:

 public int method(int a, int b){
   return Math.round((Math.abs(a-b)/3) - (Math.min(a,b)-1500)/10)
 }

我创建了单元测试,它通过了值 1400 和 1500(预期值为 43),但是对于 1459 和 1500 失败了。预期输出是 18,但是我的方法返回17. 我相信这可能与舍入有关,但是我看不到任何明显的错误。将 17.7(6) 舍入到 18 应该不会有任何问题。

编辑:

实际函数略有不同(我没有 Math.abs(ab) 但我因此定义了“diff”变量。我可以向你们保证,我将其声明为 double diff; - 我不知道为什么它变成 int diff :) 已解决,谢谢 :)

I have method that takes 2 integers as arguments and returns:

 public int method(int a, int b){
   return Math.round((Math.abs(a-b)/3) - (Math.min(a,b)-1500)/10)
 }

I have created unit test, which passes for values 1400 and 1500 (expected value is 43), however it fails for 1459 and 1500. The expected output is 18, however my method returns 17.
I believe this might have something to do with rounding, however i cannot see any obvious error. There should not be any problems with rounding 17.7(6) to 18.

EDIT:

the real function was slighty different (I did not have Math.abs(a-b) but instead I had defined "diff" variable as a result of this. I could promise you guys that i declared it as double diff; - I have no idea why it become int diff :) SOLVED thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

伤痕我心 2024-12-10 18:51:11

当将 int 作为参数传递时,Math.absMath.min 都返回 int,因此您的代码是进行整数除法而不是您期望的双除法。如果将 310 替换为 3.010.0,您的代码应该按您的预期工作。

Math.abs and Math.min both return ints when passed ints as arguments, so your code is doing integer division instead of the double division that you are expecting. If you replace the 3 and the 10 with 3.0 and 10.0, your code should work as you expect.

神妖 2024-12-10 18:51:11

这是因为它正在执行整数除法。对于 a = 1459b = 1500

Math.round((Math.abs(a-b)/3) - (Math.min(a,b)-1500)/10)
Math.round(Math.abs(41)/3 - (1459-1500)/10)
Math.round(41/3 - (-41)/10)
Math.round(13 - (-4))
Math.round(17)       // note that round will always get an integer as input
17

简单的解决方法是通过使用浮点数作为参数之一来强制其执行浮点除法:

Math.round((Math.abs(a-b)/3.0) - (Math.min(a,b)-1500)/10.0)

It's because it's performing integer division. With a = 1459, b = 1500:

Math.round((Math.abs(a-b)/3) - (Math.min(a,b)-1500)/10)
Math.round(Math.abs(41)/3 - (1459-1500)/10)
Math.round(41/3 - (-41)/10)
Math.round(13 - (-4))
Math.round(17)       // note that round will always get an integer as input
17

The easy fix is to force it to perform floating point division instead by using a float as one of the arguments:

Math.round((Math.abs(a-b)/3.0) - (Math.min(a,b)-1500)/10.0)
追风人 2024-12-10 18:51:11

使用整数除法时期望出现双精度数肯定是本书中最古老的问题。 ;)

您的回合不会执行任何操作,因为您的整数除法会产生整数结果。

Expecting a double when using integer division has to be the oldest gotcha in the book. ;)

Your round doesn't do anything as your integer divisions produce integer result.

时光沙漏 2024-12-10 18:51:11

预期结果是 17。

|a-b| = 41
|a-b|/3 = 13

min(a,b) = 1459
min(a,b)-1500 = -41
(min(a,b)-1500)/10 = -4

13-(-4) = 17

我怀疑您不想进行整数除法。因此,您必须将 310 替换为 3.010.0

The expected outcome is 17.

|a-b| = 41
|a-b|/3 = 13

min(a,b) = 1459
min(a,b)-1500 = -41
(min(a,b)-1500)/10 = -4

13-(-4) = 17

I suspect you do not want to have integer division. Therefore you must replace 3 and 10 by 3.0 and 10.0.

北陌 2024-12-10 18:51:11

您必须记住要考虑到这样一个事实:在 Java 中,当一个整数除以另一个整数时,结果是一个整数,而不是浮点数(任何余数都会被丢弃)。例如 6/4 == 1,而不是 1.5

方法(1459, 1500)

Math.round((Math.abs(1459-1500)/3) - (Math.min(1459,1500)-1500)/10);
Math.round((41/3) - (1459-1500)/10);
Math.round(13 - (-41)/10); //41/3 == 13, not 13.6666
Math.round(13 - -4); //-41/10 == -4, not -4.1
Math.round(17);
17

You must remember to take into account the fact that when an integer is divided by another integer in Java, the result is an integer, not a float (any remainder is dropped). For example 6/4 == 1, not 1.5.

method(1459, 1500)

Math.round((Math.abs(1459-1500)/3) - (Math.min(1459,1500)-1500)/10);
Math.round((41/3) - (1459-1500)/10);
Math.round(13 - (-41)/10); //41/3 == 13, not 13.6666
Math.round(13 - -4); //-41/10 == -4, not -4.1
Math.round(17);
17
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文