If you are referring to the1.2000000000000002E-8 where you have an actual math error, it is because double and float math don't work properly, for whatever reason.  If you switch to using BigDecimal then you are able to obtain a "better" answer.
ie:
 
    public static void main(String[] args) {
 
        Double op1 = new Double(12.0),
                op2 = new Double(1E-9),
                res = new Double(op1.doubleValue() * op2.doubleValue());
 
        System.out.println("op1: " + op1);
        System.out.println("op2: " + op2);
        System.out.println("res: " + res);
 
        BigDecimal bd1 = new BigDecimal("12.0");
        BigDecimal bd2 = new BigDecimal("1E-9");
        BigDecimal res2 = bd1.multiply(bd2);
 
        System.out.println("bd1: " + bd1);
        System.out.println("bd2: " + bd2);
        System.out.println("res2: " + res2);
    }
 
 
results in:
 
op1: 12.0
op2: 1.0E-9
res: 1.2000000000000002E-8
bd1: 12.0
bd2: 0.000000001
res2: 0.0000000120
 
-----Original Message-----
From: Ivan Bradac [mailto:[EMAIL PROTECTED]]
Sent: Thursday, September 19, 2002 10:40 AM
To: JDJList
Subject: [jdjlist] Re: Double multiplication

Sorry, the actual output was:
 
op1: 12.0
op2: 1.0E-9
res: 1.2000000000000002E-8
----- Original Message -----
To: JDJList
Sent: Thursday, September 19, 2002 4:31 PM
Subject: [jdjlist] Double multiplication

Hi
 
I have written a sample test program:
.........
 
public static void main (String args[]) 
    {
        Double
            op1 = new Double(12.0),
            op2 = new Double(1E-9),
            res = new Double ( op1.doubleValue()*op2.doubleValue() );
       
        System.out.println("op1: " + op1);
        System.out.println("op2: " + op2);
        System.out.println("res: " + res);
    }
The output is:
 
op1: 12.0000000001
op2: 1.0E-9
res: 1.2000000000100001E-8
What the @!# is going on?
 
How can I fix this?
 
Any help appreciated.
 
Thanks
 
Ivan
To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm

Reply via email to