[java] Java double.MAX_VALUE?

For my assignment I have to create a Gas Meter System for a Gas company to allow employees to create new costumer accounts and amend data such as name and unit costs along with taking(depositing) money from their account.

I have created my constructor and even added in a method of overloading although I'm currently running into a problem when initiating one of my methods I named deposit, this is supposed to take money from the users account while other methods such as recordUnits allows the employee to import a gas meter reading of how many units the customer has used and updates the balance of that customers account which is essentially what the customer owes the company.

When testing the program with just preset information when trying to initiate the deposit method I get this

Account.deposit(Double.MAX_VALUE);

I am not too sure what this means and cannot seem to find anyway of getting past it!

test data and code seen below:

public class TestGasAccount 

{
    public static void main (String [] args)
    {
        GasAccount Account = new GasAccount (223,"Havana","TQ",1000);

        Account.getAccNo();
        Account.getName();
        Account.getAddress();
        Account.getUnits();
        Account.getBalance();
        Account.recordUnits(1000);
        Account.getUnits();
        Account.getBalance();
        Account.deposit(Double.MAX_VALUE);
    }
}

break

public class GasAccount 
{
    private int intAccNo;
    private String strName;
    private String strAddress; 
    private double dblBalance;
    private double dblUnits;
    protected double dblUnitCost = 0.02; 

     public GasAccount(int intNewAccNo,String strNewName,String strNewAddress,double dblNewUnits)
     {
         intAccNo = intNewAccNo;
         strName = strNewName;
         strAddress = strNewAddress;
         dblUnits = dblNewUnits;
         dblBalance = dblNewUnits * dblUnitCost;
     }

     public GasAccount (int intNewAccNo, String strNewName, String strNewAddress)
     {
         intAccNo = intNewAccNo;
         strName = strNewName;
         strAddress = strNewAddress;
     }

     public double deposit (Double dblDepositAmount)
     {
        dblBalance = dblBalance - dblDepositAmount; 
        return dblBalance;
     }

     public String recordUnits (double dblUnitsUsed)
     {
         double dblTempBalance;

         dblTempBalance = dblUnitsUsed * dblUnitCost;
         dblBalance = dblBalance + dblTempBalance;
         dblUnits = dblUnits + dblUnitsUsed;

         return "Transaction Successful"; 
     }

     public int getAccNo ()
     {
         System.out.println(intAccNo);
         return intAccNo;
     }

     public String getName()
     {
         System.out.println(strName);
         return strName; 
     }

      public String getAddress()
     {
         System.out.println(strAddress);
         return strName; 
     }

     public double getBalance()
     {
         System.out.println("£"+dblBalance);
         return dblBalance; 
     }

     public double getUnitCost()
     {

         return dblUnitCost;
     }

     public double getUnits ()
     {
         System.out.println(dblUnits);
         return dblUnits;
     }

     public void updateUnitCost (double dblNewUnitCost)
     {
         dblUnitCost = dblNewUnitCost;

     }

}

This question is related to java double

The answer is


Double.MAX_VALUE is the maximum value a double can represent (somewhere around 1.7*10^308).

This should end in some calculation problems, if you try to subtract the maximum possible value of a data type.

Even though when you are dealing with money you should never use floating point values especially while rounding this can cause problems (you will either have to much or less money in your system then).


this states that Account.deposit(Double.MAX_VALUE); it is setting deposit value to MAX value of Double dataType.to procced for running tests.


Resurrecting the dead here, but just in case someone stumbles against this like myself. I know where to get the maximum value of a double, the (more) interesting part was to how did they get to that number.

double has 64 bits. The first one is reserved for the sign.

Next 11 represent the exponent (that is 1023 biased). It's just another way to represent the positive/negative values. If there are 11 bits then the max value is 1023.

Then there are 52 bits that hold the mantissa.

This is easily computed like this for example:

public static void main(String[] args) {

    String test = Strings.repeat("1", 52);

    double first = 0.5;
    double result = 0.0;
    for (char c : test.toCharArray()) {
        result += first;
        first = first / 2;
    }

    System.out.println(result); // close approximation of 1
    System.out.println(Math.pow(2, 1023) * (1 + result));
    System.out.println(Double.MAX_VALUE);

} 

You can also prove this in reverse order :

    String max = "0" + Long.toBinaryString(Double.doubleToLongBits(Double.MAX_VALUE));

    String sign = max.substring(0, 1);
    String exponent = max.substring(1, 12); // 11111111110
    String mantissa = max.substring(12, 64);

    System.out.println(sign); // 0 - positive
    System.out.println(exponent); // 2046 - 1023 = 1023
    System.out.println(mantissa); // 0.99999...8