aspose file tools*
The moose likes Beginning Java and the fly likes Multiplication Issue when Variables as Double Datatype Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiplication Issue when Variables as Double Datatype" Watch "Multiplication Issue when Variables as Double Datatype" New topic
Author

Multiplication Issue when Variables as Double Datatype

N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
package test1;

public class TestApp {

public static void main(String[] args) {

double a = 0.43;
double b = 0.1;
System.out.println(a * b);

}
}

The Output shows 0.043000000000000003.

But the 0.43 * 0.1 is 0.043.

May i know why the Value have 3 at last in the Output ----> 0.04300000000000000 3.

Thanks in Advance.


N.Senthil Kumar
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92

Use float it will work.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Do you need that much precision? A 3 in that far down seems pretty negligible to me.


-Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
N.Senthil Kumar wrote:May i know why the Value have 3 at last in the Output ----> 0.04300000000000000 3.


Not every decimal number (which humans prefer) can be exactly converted to a binary number (which computers prefer). So conversions back and forth between number systems tend to produce small errors.

Also the number of bits used to represent numbers internally in the computer is fixed. As you probably know a float uses 32 bits and a double 64. This means that when arithmetical operations are performed, the result may not be exact. So operations on numbers in fixed-size representation also tend to introduce small errors.

The result is what you see in your example. The combination of conversion errors and fixed-size arithmetic errors show up as a "ripple" in the result.

These kinds of errors are fundamental to digital computers so they will always be present. One way to handle them is to round the result of a calculation. As you can see the error is far smaller than the 2 digits of precision you've used in the input numbers. If you round the output to the same precision the error disappears and the result is "correct".

Also note that programmers must watch out so formulas they use don't magnify those small fundamental and unavoidable errors. Such formulas are called ill-conditioned and there's a whole branch of math called numerical mathematics which is dealing with that.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42583
    
  65
Please UseAMeaningfulSubjectLine. You can edit the subject using the button.

As to your question, see #20 in the http://faq.javaranch.com/java/JavaBeginnersFaq.


Ping & DNS - my free Android networking tools app
N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Friends,
Thanks for the Reply .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiplication Issue when Variables as Double Datatype