gpt4 book ai didi

Java Math.atan() 为某些输入返回 0?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:36 29 4
gpt4 key购买 nike

Java Math.atan() 函数是否已知有任何问题?我在我的代码中使用它,由于一些我无法解决的奇怪原因,该函数将在 45 度运动时返回 0,跳转到 45 并保持在 45 直到它达到 63.4 度,它开始正确映射直到它达到 -63.4 度,问题再次出现。我很困惑!我已经三次检查我的输入始终正确,但问题再次出现!

这是我的代码

int adjacent = p2.x - p1.x;
if (adjacent == 0) adjacent = 1; //Prevent division by 0
int opposite = p2.y - p1.y;
double pheta = Math.atan(opposite/adjacent);
System.out.println(opposite + "/" + adjacent + ", pheta=" + Math.toDegrees(pheta));

下面是一些测试数据的打印输出

73/102, angle=0.0
73/101, angle=0.0
74/100, angle=0.0
74/99, angle=0.0
75/99, angle=0.0
76/97, angle=0.0
76/96, angle=0.0
77/95, angle=0.0
78/95, angle=0.0
78/94, angle=0.0
79/92, angle=0.0
80/91, angle=0.0
81/90, angle=0.0
81/89, angle=0.0
82/87, angle=0.0
83/86, angle=0.0
84/85, angle=0.0
84/84, angle=45.0
84/83, angle=45.0
85/83, angle=45.0
85/82, angle=45.0
85/81, angle=45.0
85/80, angle=45.0
86/79, angle=45.0
87/78, angle=45.0
87/77, angle=45.0
87/76, angle=45.0
88/75, angle=45.0
88/74, angle=45.0
88/73, angle=45.0
89/72, angle=45.0
90/70, angle=45.0
90/69, angle=45.0
91/68, angle=45.0
92/67, angle=45.0
92/66, angle=45.0
93/65, angle=45.0
93/64, angle=45.0
93/62, angle=45.0
94/62, angle=45.0
94/61, angle=45.0
95/61, angle=45.0
95/60, angle=45.0
95/59, angle=45.0
95/57, angle=45.0
95/56, angle=45.0
96/55, angle=45.0
96/54, angle=45.0
96/53, angle=45.0
97/53, angle=45.0
97/52, angle=45.0
97/51, angle=45.0
98/51, angle=45.0
98/50, angle=45.0
98/49, angle=63.43494882292201
98/47, angle=63.43494882292201
99/45, angle=63.43494882292201
99/42, angle=63.43494882292201
100/40, angle=63.43494882292201
100/38, angle=63.43494882292201
100/36, angle=63.43494882292201
100/34, angle=63.43494882292201
100/33, angle=71.56505117707799
101/33, angle=71.56505117707799
101/32, angle=71.56505117707799
101/31, angle=71.56505117707799
101/30, angle=71.56505117707799
102/29, angle=71.56505117707799
102/28, angle=71.56505117707799
102/27, angle=71.56505117707799
103/27, angle=71.56505117707799
103/25, angle=75.96375653207353
103/24, angle=75.96375653207353
103/23, angle=75.96375653207353
103/22, angle=75.96375653207353
104/21, angle=75.96375653207353
104/19, angle=78.69006752597979
105/18, angle=78.69006752597979
105/16, angle=80.53767779197439
106/15, angle=81.86989764584403
106/14, angle=81.86989764584403
106/13, angle=82.87498365109819
106/12, angle=82.87498365109819
106/11, angle=83.6598082540901
106/10, angle=84.28940686250037
106/9, angle=84.8055710922652
107/8, angle=85.60129464500447
108/6, angle=86.82016988013577

有什么想法吗?

最佳答案

你执行整数除法,结果是整数,试试:

Math.atan((double)opposite/adjacent);

关于Java Math.atan() 为某些输入返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9419716/

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