BigDecimal Exception

I

inspired

Based on advice provided on an earlier post, I am trying to use the
BigDecimal class. However, I`m having a fundamental problem.

In my application class, I have the following:

private static final BigDecimal LSBvalue = (new BigDecimal (180 *
( Math.pow(2, 1 - 15)))).setScale(5, RoundingMode.HALF_UP);

public static int convertToInt(BigDecimal degrees) throws
ArithmeticException
{
return (degrees.divide(LSBvalue)).intValue();
}


The simple call below:

BigDecimal AngleDegrees = new BigDecimal(0.9118);
int AngleBams = convertToInt(AngleDegrees);

results in the following exception:

java.lang.ArithmeticException: Non-terminating decimal expansion; no
exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1603)
at BamBam.Angle.convertToInt(Angle.java:47)
at BamBam.Angle.main(Angle.java:73)


I am new to BigDecimal and not sure what I am doing wrong. Can
anyone help?

Alan
 
A

Arne Vajhøj

Based on advice provided on an earlier post, I am trying to use the
BigDecimal class. However, I`m having a fundamental problem.

In my application class, I have the following:

private static final BigDecimal LSBvalue = (new BigDecimal (180 *
( Math.pow(2, 1 - 15)))).setScale(5, RoundingMode.HALF_UP);

public static int convertToInt(BigDecimal degrees) throws
ArithmeticException
{
return (degrees.divide(LSBvalue)).intValue();
}


The simple call below:

BigDecimal AngleDegrees = new BigDecimal(0.9118);
int AngleBams = convertToInt(AngleDegrees);

results in the following exception:

java.lang.ArithmeticException: Non-terminating decimal expansion; no
exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1603)
at BamBam.Angle.convertToInt(Angle.java:47)
at BamBam.Angle.main(Angle.java:73)


I am new to BigDecimal and not sure what I am doing wrong. Can
anyone help?

BigDecimal is exact.

And it seems as you are making a calculation that requires an
infinite number of decimals to represent the result.

Maybe you really want floating point and just have to live
with its behavior.

Arne
 
C

ClassCastException

BigDecimal is exact.

And it seems as you are making a calculation that requires an infinite
number of decimals to represent the result.

Maybe you really want floating point and just have to live with its
behavior.

BigDecimal can be used at finite precision, or you can set a precision in
a MathContext object and pass it to all your BigDecimal arithmetic
operations.

(Or you can use a JVM language like Clojure that has a ratio type, which
is exact and can represent any rational fraction. :))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top