将代码从使用双打转换为bigdecimal

发布于 2025-02-06 19:06:57 字数 847 浏览 2 评论 0原文

我一直在写一个CAD类型程序,以供爪哇娱乐。前几天,我编写了一些代码来定义与2个圆圈相切的线路。我一直在使用商业CAD计划检查我的电话号码,它们相当接近。通常到第9点。我的结果实际上只需要存储在一个小数点的阵列中。在成功定义了与两个圆圈切线的线后,我决定对其进行测试并定义一个点,即线的相交和一个圆圈。 在一种情况下,我得到了我正在寻找的结果,在另一种情况下,我没有交叉点。在查看了一些计算之后,我意识到我的变化很小,大概是9或十进制位置。我正在考虑使用BigDecimal重写代码。

这是我需要重写的一些代码的小片段。一旦我开始,它就变得比我想做的要繁琐。我正在考虑仅使用BigDecimal并使用原始代码转换结果,除非有一种简单的方法将以下代码转换为GimDecimal类型的格式。

    private float[] offsetLine(float lnx1, float lny1, float lnz1, float lnx2, float lny2, float lnz2, String direction, float offset) {
        
        
        double deltax = Math.abs(lnx2 - lnx1);
        double deltay = Math.abs(lny2 - lny1);
        double lineLength = Math.sqrt(deltax * deltax + deltay * deltay);
        
        double stepx = (offset * deltay) / lineLength;
        double stepy = (offset * deltax) / lineLength;
        
        
    

I've been writing a CAD type program for fun in JAVA. The other day I wrote some code to define a line which was tangent to 2 circles. I've been checking my numbers with a commercial CAD program and they have been fairly close. Usually to the 9th decimal point. My results really only need to be stored in an array to 7 decimal points. After successfully defining the line tangent to the 2 circles, I decided to test it and define a point which was the intersection of the line and one of the circles.
In one case I got the result I was looking for, in another case I got no intersection. After looking at a few of the calculations I realized I was getting a very very small variation of maybe 9 or ten decimal places. I'm thinking of rewriting the code using BigDecimal.

This is a small snippet of some of the code I need to rewrite. Once I started it became much more cumbersome than I was wanting to do. I'm thinking about just converting the results using BigDecimal and using the original code unless there is an easy way to convert the following code to a BigDecimal type of format.

    private float[] offsetLine(float lnx1, float lny1, float lnz1, float lnx2, float lny2, float lnz2, String direction, float offset) {
        
        
        double deltax = Math.abs(lnx2 - lnx1);
        double deltay = Math.abs(lny2 - lny1);
        double lineLength = Math.sqrt(deltax * deltax + deltay * deltay);
        
        double stepx = (offset * deltay) / lineLength;
        double stepy = (offset * deltax) / lineLength;
        
        
    

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

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

发布评论

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

评论(1

为你鎻心 2025-02-13 19:06:57

好的,我会回答自己的问题。这是我挖出的一些代码。我只能到达6个小数点的位置,以获取想要的舍入。一旦我以双重值进行计算,我称之为subroutine rounddbl


double checkRadius1 = Math.sqrt(((cir1x - offsetpts[0])*(cir1x - offsetpts[0])) + ((cir1y - offsetpts[1]) * (cir1y - offsetpts[1])));

double checkRadiusRounded = roundDbl(checkRadius1, 6); //round to 6 decimal places

public static Float roundDbl(Double dblValue, int decimalPlace) {

        String tempDblString = Double.toString(dblValue);

        String tempDbl = new BigDecimal(tempDblString).setScale(decimalPlace, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
        
        return Float.valueOf(tempDbl);
    }

Ok I'll answer my own question. Here's some code I dug up. I could only round to 6 decimal places to get the rounding I wanted. Once I did my calculations in double values I called the subroutine roundDbl


double checkRadius1 = Math.sqrt(((cir1x - offsetpts[0])*(cir1x - offsetpts[0])) + ((cir1y - offsetpts[1]) * (cir1y - offsetpts[1])));

double checkRadiusRounded = roundDbl(checkRadius1, 6); //round to 6 decimal places

public static Float roundDbl(Double dblValue, int decimalPlace) {

        String tempDblString = Double.toString(dblValue);

        String tempDbl = new BigDecimal(tempDblString).setScale(decimalPlace, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
        
        return Float.valueOf(tempDbl);
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文