Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

numeric representation/precision error

Posted on 1997-06-19
1
Medium Priority
?
224 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 200 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

824 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