Explanation of result for Double.valueOf [duplicate] - java

This question already has answers here:
Strange floating-point behaviour in a Java program [duplicate]
(4 answers)
Closed 8 years ago.
I want to know the reason why i am getting so many decimal points while doing this sum of double numbers
System.out.println(Double.parseDouble(".56"));
double dvalue=1.12+Double.parseDouble(".56");
System.out.println(dvalue);
output is as follows
0.56 and
1.6800000000000002
why second summation is adding those decimals it should simply 1.68

It's because the addition of the doubles 1.12 and 0.56 does not yield an exact representation of 1.68 (it doesn't precisely result in 1.68). You can see the difference with this code:
System.out.println(1.12+0.56); // 1.6800000000000002
System.out.println(1.68); // 1.68

Related

Rounding off issue clarification in java [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
I am using Math.round(). This is the sample code:
double value = 0.14499999970197677;
value = Math.round( value* 100.0) / 100.0;
My expectation is it should return 0.15 but it is returning 0.14
Also, if the value is 0.13499999970197677, then it is returning 0.13, why not 0.14
I have already gone through the link round up to 2 decimal places in java?
Please tell me clearly that why this is happening with both the numbers?
In Java, Math.round rounds up/down to closest long value (in mathematics terms, closest whole number).
14.49 will be rounded to 14 (because it is treated as 14.4). It won't take the .09 into account.

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".

Java - How to proper rising a number to fractional power? [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
I used the following code:
double pow = 3/7;
double num = 85;
System.out.println(Math.pow(num, pow));
Expected result:
6.71...
The output is
1.0
Any idea why?
3/7 is evaluated to 0, since you are dividing two integers, so Math.pow(num, pow) becomes Math.pow(num, 0.0) which is 1.0.
Change it to 3.0/7 in order to get a floating point result.

Raising number to fractional power java [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
I used the following code:
double pow = 3/7;
double num = 85;
System.out.println(Math.pow(num, pow));
Expected result:
6.71...
The output is
1.0
Any idea why?
3/7 is evaluated to 0, since you are dividing two integers, so Math.pow(num, pow) becomes Math.pow(num, 0.0) which is 1.0.
Change it to 3.0/7 in order to get a floating point result.

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

Categories