Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

numeric representation/precision error

Posted on 1997-06-19
1
Medium Priority
?
223 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 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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 …
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…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

705 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