gpt4 book ai didi

java - 我可以用什么大 double 来模拟无穷大?

转载 作者:行者123 更新时间:2023-12-02 01:10:09 25 4
gpt4 key购买 nike

我想要一个可以模拟无穷大的大 double ,但又不能大到占用大量内存。因此,最大的双倍可能可以多次使用而不会导致我的程序崩溃。

对于上下文:我正在尝试创建一个函数,该函数返回从两个点创建的两条线段之间的交点,如果没有交点,则返回 null(在更高的方法中使用,以确定对象是否在碰撞中)我的平台游戏)。作为数学/代码的一部分,我需要从两个点创建一个线函数,当该线恰好是垂直的时,它需要具有无限的斜率。这是我到目前为止所拥有的:

public static Point intersect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
//calcs slopes
double m1 = bigDouble; //infinity!
if (x1==x2) m1 = (y2-y1) / (x2-x1); //if its not vertical, calc the slope
double m2 = bigDouble;
if (x3==x4) m2 = (y4-y3) / (x4-x3);
//calcs b in y=mx+b
int b1 = (int) (m1*x1+y1);
int b2 = (int) (m2*x3+y3);
//checks that lines are not parallel
if (m1==m2) return null;
//calcs intersection
int x = (int) ((b2-b1)/(m1-m2));
int y = (int) (m1*x+b1);
//checks that intersection is within bounds of segments
if (isOutside(x,x1,x2)||isOutside(y,y1,y2)||isOutside(x,x3,x4)||isOutside(y,y3,y4)) return null;
//returns intersection point
return new Point(x,y);
}

public static boolean isOutside (int num, int bound1, int bound2) {
return num<getMin(bound1,bound2) || num>getMax(bound1, bound2);
}

public static int getMin(int num1, int num2) {
if (num1>num2) return num2;
return num1;
}

public static int getMax(int num1, int num2) {
if (num1>num2) return num1;
return num2;
}

那么我可以用什么来实现这个大双倍呢?谢谢!

最佳答案

使用其中之一:

Double.POSITIVE_INFINITY
Double.MAX_VALUE

关于java - 我可以用什么大 double 来模拟无穷大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59524609/

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