简体   繁体   中英

How to prevent loss of precision when dividing decimals?

I coded the below program in Java to determine in what type of pattern a number sequence is. However, I'm having problems when dividing doubles; even if 2 double values are very small (less than two decimal places; it loses precision even when there is only one decimal place). I was going to use the bigdecimal class, but I would like to know wether there is any easier method or alternatives since it would be tedious to add bigdecimal to larger programs with more functions and variables. Also, are there any other improvements I can make?

package Numbers;

import javax.swing.*;

public class NumberPatterns {

    public static void main(String[] args) {

        double a, b, c, d;
        a = Double.parseDouble(JOptionPane.showInputDialog("Please enter the value of the first term.")); //first term
        b = Double.parseDouble(JOptionPane.showInputDialog("Please enter the value of the second term.")); //second term
        c = Double.parseDouble(JOptionPane.showInputDialog("Please enter the value of the third term.")); //third term
        d = Double.parseDouble(JOptionPane.showInputDialog("Please enter the value of the fourth term.")); //fourth term

        double constantDifference, constantRatio, constantSecondDifference; //Creating variables to verify the sequence characteristic

        constantDifference = (b-a) - (d-c); //Constant Difference
        System.out.println("b/a value is !! "+b/a);
        System.out.println("d/c value is !! "+d/c);
        constantRatio = (b/a) - (d/c); //Constant Ratio
        constantSecondDifference = ((d-c)-(c-b)) - ((c-b)-(b-a)); // Constant Second Difference

        if (constantDifference == 0) // Arthmetic sequence
        {
            System.out.println("The given sequence is: " + a + "; " + b + "; " + c + "; " + d); // print sequence
            System.out.println("This sequence is an arthmetic sequence."); // Sequence type
            System.out.println("The constant difference is: " + (b-a)); // Constant difference
            System.out.println("The general term is: Tn = " + (a + " + " + (b-a)+ "(n -1)")); //General term
        }

        else if (constantRatio == 0) // Geometric sequence
        {
            System.out.println("The given sequence is: " + a + "; " + b + "; " + c + "; " + d); // print sequence
            System.out.println("This sequence is an geometric sequence."); // Sequence type
            System.out.println("The constant ratio is: " + (b/a)); // Constant ratio
            System.out.println("The general term is: Tn = " + a + "*" + (b/a) + "^" + "(n-1)"); //G eneral term
        }

        else if (constantSecondDifference == 0 && constantDifference != 0) // Quadratic sequence
        {
            System.out.println("The given sequence is: " + a + "; " + b + "; " + c + "; " + d); // print sequence
            System.out.println("This sequence is an quadratic sequence."); // Sequence type
            System.out.println("The constant second difference is: " + ((c-b)-(b-a))); // Constant second difference
            double x, y, z;                    // The follwing variables are used to calculate the general term 
            x = ((c-b)-(b-a));
            x = x/2;
            y = (b-a) - 3*x;
            z = a - x -y;
            System.out.println("The general term is: " + x + "n^2 + " + y + "n + (" + z + ")"); // General term
        } 
    }
}

Unfortunately there is no alternative. What you can look up is this standard IEEE 754. So this is not even something the language does, rather the processor itself works like that and you can expect doubles and floats to loose precision. The best way to get the needed precision is BigDecimal. If you can afford no division, just using Integer would help as well, however in the future you may need it and will still need Big Decimal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM