Solved

numeric representation/precision error

Posted on 1997-06-19
1
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

628 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