cworner1
cworner1

Reputation: 451

Understanding Apfloat and its precision

I'm using an application which uses mm and inches to draw scaled drawings. I need to convert from one to another without losing precision. I'm struggling with accuracy and have turned to the Apfloat package. Can someone explain... if I start with 100mm, convert it to inches and then back again to Mm why I get a loss of precision??

public static void main(String[] args) {

    Apint mm = new Apint(100);
    System.out.println("mm = " + mm);

    // 25.4mm in 1 inch
    Apfloat toInches = new Apfloat("25.4");

    // 100mm / 25.4 = x-Inches      
    Apfloat inches = mm.divide(toInches);
    System.out.println("mm - > Inch = " + inches);

    // x-Inches x 25.4 = 100mm
    Apfloat backToMm = inches.multiply(toInches);
    System.out.println("Inch - > mm = " + backToMm);
}

Outputs:

mm = 100
mm - > Inch = 3.93
Inch - > mm = 9.99e1


No matter how big I set the precision property I still get the reoccurring 9.999...

Upvotes: 0

Views: 486

Answers (2)

Mikko Tommila
Mikko Tommila

Reputation: 1

The rounding mode is undefined. To get a rounded result, use ApfloatMath.round()

Upvotes: 0

Powerlord
Powerlord

Reputation: 88796

OK, I wasn't familiar with Apfloat, so I started reading its documentation.

Apparently, if you use the public Apfloat(String value) constructor, the precision of the number is the number of digits in the string.

In this case, you've specified "25.4", which is 3 digits.

However, to get around this issue, you can use the public Apfloat(String value, long precision) constructor which allows you to specify the precision so that you can handle numbers with more decimal places.

Upvotes: 1

Related Questions