Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

So I've read thisthis post, and the best answer tells me:

So I've read this post, and the best answer tells me:

So I've read this post, and the best answer tells me:

Source Link

How to overcome inaccuracy in Java

I came to know about the accuracy issues when I executed the following following program:

public static void main(String args[])
{
    double table[][] = new double[5][4];
    int i, j;
    for(i = 0, j = 0; i <= 90; i+= 15)
    {
        if(i == 15 || i == 75)
            continue;
        table[j][0] = i;
        double theta = StrictMath.toRadians((double)i);
        table[j][1] = StrictMath.sin(theta);
        table[j][2] = StrictMath.cos(theta);
        table[j++][3] = StrictMath.tan(theta);
    }
    System.out.println("angle#sin#cos#tan");
    for(i = 0; i < table.length; i++){
        for(j = 0; j < table[i].length; j++)
            System.out.print(table[i][j] + "\t");
        System.out.println();
    }
}

And the output is:

angle#sin#cos#tan
0.0 0.0 1.0 0.0 
30.0    0.49999999999999994 0.8660254037844387  0.5773502691896257  
45.0    0.7071067811865475  0.7071067811865476  0.9999999999999999  
60.0    0.8660254037844386  0.5000000000000001  1.7320508075688767  
90.0    1.0 6.123233995736766E-17   1.633123935319537E16    

(Please forgive the unorganised output). I've noted several things:

  • sin 30 i.e. 0.5 is stored as 0.49999999999999994.
  • tan 45 i.e. 1.0 is stored as 0.9999999999999999.
  • tan 90 i.e. infinity or undefined is stored as 1.633123935319537E16 (which is a very big number).

Naturally, I was quite confused to see the output (even after deciphering the output).

So I've read this post, and the best answer tells me:

These accuracy problems are due to the internal representation of floating > point numbers and there's not much you can do to avoid it.

By the way, printing these values at run-time often still leads to the correct results, at >least using modern C++ compilers. For most operations, this isn't much of an issue.

answered Oct 7 '08 at 7:42

Konrad Rudolph

So, my question is: Is there any way to prevent such inaccurate results (in Java)?

Should I round-off the results? In that case, how would I store infinity i.e. Double.POSITIVE_INFINITY?