Solved

numeric representation/precision error

Posted on 1997-06-19
1
213 Views
Last Modified: 2008-03-10
I am relatively new to the java programming world, and having a really annoying problem with this piece of code:

import java.io.*;

class doubleTest {
    public static void main(String argv[]) {
        double sigDigits = 9.74;
        double shiftLeft = 9.0;  // number of places to                                         shift decimal left

        for (int i = 1; i <= shiftLeft; i++) {
            sigDigits = sigDigits/10.0;
            System.out.println(sigDigits);
        }
    }
}


this produces the following output:
0.974
0.0974
0.00974
9.74E-4
9.740000000000001E-5
9.740000000000002E-6
9.740000000000001E-7
9.740000000000001E-8
9.740000000000002E-9  // I get this number also if I                                  substitute for loop with:
    sigDigits = Math.pow(sigDigits,-shiftLeft);


Why the extra digit at the end of the number?  Is there a better way to divide doubles that don't produce erroneous output?  If you change shiftLeft to 19.0,
you get the following output:
0.974
0.0974
0.00974
9.74E-4
9.740000000000001E-5
9.740000000000002E-6
9.740000000000001E-7
9.740000000000001E-8
9.740000000000002E-9
9.740000000000002E-10
9.740000000000002E-11
9.740000000000002E-12
9.74E-13
9.740000000000001E-14
9.740000000000001E-15
9.74E-16
9.74E-17
9.74E-18
9.74E-19

this is very strange to me.  Thanks in advance for you help.
-Matt
0
Comment
Question by:mclaren
1 Comment
 
LVL 6

Accepted Solution

by:
jpk041897 earned 50 total points
ID: 1221833
All floating point libraries introduce presition errors, the good ones simply produce them less often. Unfortunatley Java's FP lib is not amongst the best.

There are 3 possible work arrounds:

1.- If the error affect your results somhow, you will have to use native methods. See:

http://www.inf.uni-hohenheim.de/top/java/tutorial/native1.1/index.html

on how to use them.

2.- If the error only affects your results for display purposes (I.e.: the accumulated effect of the errors does not affect the values you wish to display), use formatting classes on the  output to get rid of the error. See:

http://www.acme.com/java/software/Acme.Fmt.html

for a class that will allow you to do this.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now