我可以用来模拟无穷大的两倍吗? [重复]

问题描述 投票:0回答:1

我想要一个可以模拟无穷大的双精度,但又不能太大以至于占用大量内存。因此,最大可能的两倍可以在不使我的程序崩溃的情况下被大量使用。

对于上下文:我正在尝试创建一个函数,该函数返回从每个点创建的两个线段之间的相交点;如果不存在相交,则返回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;
}

那么我能为那双大双人床使用什么?谢谢!

java double
1个回答
0
投票

使用任一:

Double.POSITIVE_INFINITY
Double.MAX_VALUE
© www.soinside.com 2019 - 2024. All rights reserved.