Java double value problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsrinivasan
    New Member
    • Mar 2007
    • 221

    #1

    Java double value problem

    Hi all,
    I have found loss of precision of java program given below.

    class DoubleValue
    {
    public static void main(String[] args)
    {
    double d1 = 83.7875;
    double d2 = 70.0;
    double d;
    d = d1 - d2;
    System.out.prin tln(d);
    }
    }

    Instead of getting 13.7875, i got only 13.787499999999 994. Where im going to wrong?

    Thanks in advance..
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by rsrinivasan
    Hi all,
    I have found loss of precision of java program given below.

    class DoubleValue
    {
    public static void main(String[] args)
    {
    double d1 = 83.7875;
    double d2 = 70.0;
    double d;
    d = d1 - d2;
    System.out.prin tln(d);
    }
    }

    Instead of getting 13.7875, i got only 13.787499999999 994. Where im going to wrong?

    Thanks in advance..
    What every computer scientist needs to know about floating point arithmetic.

    Comment

    • heat84
      New Member
      • Nov 2007
      • 118

      #3
      The result appearing is the correct one, floating point arithmetic follows the IEEE754 specifiction. You can read more about that , then you will have an answer to your problem.

      Comment

      Working...