gpt4 book ai didi

Java double 不会否定!为什么?

转载 作者:行者123 更新时间:2023-11-29 07:21:16 27 4
gpt4 key购买 nike

出于某种原因,我无法否定 double ,然后在满足特定条件(正在)时返回它。尽管有任何其他哄骗,它仍然是积极的。

 public static double angleDiffNoAbs(double from, double to, double maxValue) {  
double diff = to - from;
double absdiff = Math.abs(diff);

if (absdiff > maxValue) {
double newdiff;

boolean clockwise = false;

if (from < to) {
newdiff = maxValue;
clockwise = true;
System.out.println("c");
} else {
System.out.println("ac");
newdiff = maxValue * -1.0d;
}

System.out.println("from: " + from + ", to: " + to + ", diff: " + diff + ", absdiff: " + absdiff + ", newdiff(limited): " + newdiff + ", wound: " + (clockwise ? "clockwise" : "anti-clockwise"));

return newdiff;
}

double result = ((180d + diff) % 360d) - 180d;
if (result < -180d) result = 360d + result;

return result;
}

我的 JUnit 测试结果是:

c
from: -10.0, to: 20.0, diff: 30.0, absdiff: 30.0, newdiff(limited): 10.0, wound: clockwise
ac
from: 20.0, to: -10.0, diff: -30.0, absdiff: 30.0, newdiff(limited): 10.0, wound: anti-clockwise

我一直在研究这个问题,但无法找出问题所在。 JUnit 测试如下(如果有帮助):

public class AngleDiffTest {
@Test
public void testAngleDiff() {
double diff = Math.abs(Player.angleDiffNoAbs(353, 12, 360f));
Assert.assertFalse("diff > 200: " + diff, diff > 200);
}

@Test
public void testAngleDiff2() {
double diff2 = Math.abs(Player.angleDiffNoAbs(360, 0, 360f));
Assert.assertFalse("diff2 > 200: " + diff2, diff2 > 200);
}

@Test
public void testAngleDiff3() {
double diff2 = Math.abs(Player.angleDiffNoAbs(-10d, 20d, 10d));
Assert.assertEquals("diff2 != 10.0: " + diff2, 10d, diff2);
}


@Test
public void testAngleDiff4() {
double diff2 = Math.abs(Player.angleDiffNoAbs(20d, -10d, -10d));
Assert.assertEquals("diff2 != -10.0: " + diff2, -10d, diff2);
}
}

最佳答案

from: 20.0, to: -10.0, diff: -30.0, absdiff: 30.0, newdiff(limited): 10.0, wound: anti-clockwise

此结果似乎来自您的第四个测试用例 ( testAngleDiff4 ),您在其中传递了 -10d 的负值对于 maxValue .当你做 newdiff = maxValue * -1.0d ,两个否定相抵消,留下一个肯定的答案。

关于Java double 不会否定!为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4120543/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com