Solved

numeric representation/precision error

Posted on 1997-06-19
1
218 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

821 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