java double precision [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Precision of Floating Point
double a=0.000001,b=50000;
b=a*b;
System.out.println("double:"+b); // -> double:0.049999999999999996
float a=0.000001f,b=50000;
b=a*b;
System.out.println("float"+b); // -> float0.05
I have used double in most part of my code and today I found this problem.
How should I handle this?
Context:
double []refValues= {100,75,50,25,15,5,1};
double bInMilliGram=b*1000;
double pickedVal=0;
for(double ref:refValues)
if(ref<=bInMilliGram) {
pickedVal=ref;
break;
}
System.out.println("bInMilliGram:"+bInMilliGram+", pickedVal:"+pickedVal);
o/p: -> bInMilliGram:49.99999999999999, pickedVal:25.0

If you need arbitrarily good precision, use the java.math.BigDecimal class.

It is not a problem. It is how double works. You do not have to handle it and care about it. The precision of double is enough. Think, the difference between you number and the expected result is in the 19 position after decimal point.
The only conclusion from this fact is never try to compare floating point values using == - the results may be confusing.

Related

Strange output when float value is assigned to a double in JAVA. Getting weird decimals [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I'm facing issues understanding typecasting in the case of floats and doubles
float a = 9.8f;
double b = g;
double c = 9.8;
System.out.println(b);
System.out.println(c);
Output -
9.800000190734863
9.8
Why is there extra stuff present in the case of b? From where do the extra decimals come from?
You're moving from a smaller data type to a larger one through an implicit data type conversion and this is just the JVM trying to provide a rounding to the closest value it can represent in a double from this float. I would look at Chapter 2. The Structure of the Java Virtual Machine on Oracle's website. I think what OH GOD SPIDERS was getting, for your example here, was to use something like BigDecimal.valueOf((long)(a*100),2).doubleValue().

Conversion of integer division to double [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 4 years ago.
I want the help to produce the result of below calculation as 1.12 but the result is coming up 1.0
double k=(112)/100;
System.out.println(k);
You are doing Integer division causing it to lose the precision:
Replace
double k=(112)/100;
with
double k=(112.0)/100;
double k-=((double)112/100) worked for me as suggested by agni
when we do division like (112/100) JVM gives int as an o/p, you are storing that 'int' in 'double' so JVM adds '.0' to the o/p causing loss of precision. so, here you have to tell the JVM to give o/p without any loss. for that reason we have to mention like `double k = (double)112/100; which is similar to "typecasting".

Double data type not returning desired output in java [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
In Java, for the following code
Double d = 2.0-1.1;
System.out.println(d);
The result is
0.8999999999999999
If the program is dealing with sensitive information such as precentile/money or cents how do I solve this problem?
I also tried the following piece of code:
new BigDecimal(d)
which outputs
0.899999999999999911182158029987476766109466552734375
What should I do to get 0.90 for the above case?
Since double cannot accurately represent the result of 2 - 1.11, the precision has already been lost by the time the constructor is used.Therefore you need to chain BigDecimal using the String based constructor
BigDecimal result = new BigDecimal("2").subtract(new BigDecimal("1.1"));
The Standard Reference is What Every Computer Scientist Should Know About Floating-Point Arithmetic
A more digestable read: Floating Point Numbers

Java does not print decimal places [duplicate]

This question already has answers here:
How to make the division of 2 ints produce a float instead of another int?
(9 answers)
Closed 9 years ago.
I'm trying to do a very basic operation as follows:
double a=21/5;
System.out.println(a);
However, each time I get 4.0 as the output and not 4.2. I'm encountering this for the first time. I've been using Java for years, but never came across this obscurity.
You are using integer division, which result will always be integer
You should use something like this.
double a=(double)21/5;
You are doing integer division...
Try:
double a = 21.0/5;
Cast the division or specify one of the arguments as a decimal to force the return as a double:
double a = (double)21/5;
-or-
double a = 21.0/5;
Just cast one of the numbers to double:
double a = 21/5.0;
Force the cast to double.
double a = 21.0/5
This is called Arithmetic promotion. This means that all terms in an equation are made equal to the variable type with the highest precision. In this case double.

Java - Double precision floating point [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to fix double precision issue in java
I have a small piece of code like this:
double number1 = 6;
double number2 = 5.99;
double result = number1 - number2;
However, the result == 0.009999999999999787 instead of 0.01
I know it is the issue of IEEE 754 standard, but I don't understand why. Could you please explain it for me?
This is because float point numbers cannot be exactly represented with in binary system with limited bits (not without precision loss)
See: http://en.wikipedia.org/wiki/Loss_of_significance
Because there is no .01 in floating point numbers. The fractional bits are expressed as 1/root 2 so you can get something like .0125 or what you have there but there is not .01 in floating point numbers. If you need exact precision use integers instead.

Categories