Are you really taking powers of 105 or similar? Then you get 100000 × 105 × 105 which is 1102500000. Now next time you multiply by 105, you get an overflow error. Meanwhile the divisor is continuing to increase, and when the divisor is larger than the dividend, your integer division gives 0.

I am not sure what to recommend. You could try using long arithmetic, but that will eventually run out of space. The best way to calculate money is with the BigDecimal class.

David Herron
Greenhorn

Joined: Jul 27, 2011
Posts: 12

posted

0

I realize these numbers are huge and I figured that was possibly the issue. I guess I am taking the problem too literally.

After the book shows this program as an example, it goes on to say that this (using double) is not the best method for achieving accurate results, then it states "you've been warned!". It never gives a solution apart from a quick reference to the existence of BigDecimal but not how to use it.

Then I am given this problem, so that how I got here!

Note the %f tag defaults to 6 decimal places.
So far, so good. You can get 1 from 0.00625 if you repeatedly multiply by 2 or 5, so the division will eventually come out exactly. But what if the division isn't exact?

java BigDecimalDemo 9.0 0.7
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1616)
at BigDecimalDemo.ShowOperations(BigDecimalDemo.java:25)
at BigDecimalDemo.main(BigDecimalDemo.java:41)

We now need a version 2.Apart from the imports, the change is on lines 27-28. You supply a rounding mode and a precision. In this case 4 appears to mean 4 significant figures, not four places after the decimal point. If you try to find the divide() method, you find it is overloaded about 6 times to allow for that problem. Any overloading can be used, but they may give slightly different results.
Another thing is, BigDecimal is an immutable class. You will see the values of number1 and number2 do not change in the arithmetic.
Its equals() method includes the scale as well as the value, so 1.0 and 1.00 are not the same, but new BigDecimal("1.0").compareTo(new BigDecimal("1.000") will return 0, so you can use the compareTo method.
Don't use the constructor which takes a double, but use one of the other constructors. You can see if you try System.out.println(new BigDecimal(1.23)); and with "1.23", and see the differences.
Read the BigDecimal and RoundingMode and MathContext documentation.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43412

32

posted

0

There is a recent discussion about problems with double and BigDecimalhere.

David Herron
Greenhorn

Joined: Jul 27, 2011
Posts: 12

posted

0

Thank you for this. More than thorough. I now have this code to reference. Upon further reading, I realized the assignment is not asking us to implement BigDecimal. But since this is the proper way to do things i will be going over it as well as the RoundingMode and MathContext documentation.

Thank you again!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43412

32

posted

0

You're welcome

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43412

32

posted

0

Last year, I wrote: . . . the divide() method . . . is overloaded about 6 times to allow for that problem. Any overloading can be used, but they may give slightly different results. . . .

So, try this:. . . as an alternative to line 27 above. That seems to give 4 places after the decimal point.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43412

32

posted

0

In the first post in this thread, the image has vanished. I think it should be a printout like thisThat is another reason for not using screenshots: text posted doesn't mysteriously vanish.