JAVA: Difference in Two Floats [duplicate] - java

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 2 years ago.
double double1 = 0.174;
double double2 = 0.175;
double diff = Math.abs(double1 - double2);
diff returns 0.0010000000000000009
Now I type:
double double1 = 3.174;
double double2 = 3.175;
double diff = Math.abs(double1 - double2);
I am expecting diff to return the same result, but it returns 9.999999999998899E-4.
Is there a reason for this behaviour?

can use BigDecimal
BigDecimal decimal1=BigDecimal.valueOf(1.744);
BigDecimal decimal2=BigDecimal.valueOf(1.745);
BigDecimal result=decimal2.subtract(decimal1);
double diff=Math.abs(result.doubleValue());

Related

Division in Android Gone Wrong Value [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
When I divided the large number into a small number then the division is correct but when I write the small number to divide a large number the answer returns wrong. In my scenario, the small number always be first. here is my code this code return 7.4074074074074075E-6 but the correct result is 0.0000074074.
double itf = 0.0;
double a = 4.0;
double b = 540000;
itf = a / b;
Log.i(TAG, "savedata: outputvalue=" + itf);
BigDecimal a = new BigDecimal("4");
BigDecimal b = new BigDecimal("540000");
// 0.0000074074
a.divide(b, MathContext.DECIMAL128);
You should use a decimal type. double is outside the scope of support

Why do I get 0.0 when I divide these two integers? [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 2 years ago.
I am trying to show the percentage of day passed using a fixed time. However, when I divide the time passed already by the total amount of time (in seconds) of a day, I get 0.0. I put the current values into the console. Any help is appreciated.
You are performing integer division, and then casting it to a double. You should be doing:
int numOfSecondsSinceMidnight = 61960;
int totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = (((double)numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Or better yet, changing numOfSecondsSinceMidnight and totalDay to doubles:
double numOfSecondsSinceMidnight = 61960;
double totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = ((numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Both of which print:
71.71296296296296

Wrong result when calculating floats in Eclipse [duplicate]

This question already has answers here:
Retain precision with double in Java
(24 answers)
Is floating point math broken?
(31 answers)
Closed 4 years ago.
Eclipse gives the wrong result when trying to calculate the sum of two floats.
In my code, there are 2 float variables: float from = 0.025 and float to = 1.
Then result has double variable: double value = 7 * from / to.
Eclipse compiler shows: value = 0.174999997019767760
In excel calculator, this result was value = 0.175
How can I solve this an issue?
This is simply due to your Java program not rounding the result the same way the excel calculator does. This is a result of the way computers handle floating point arithmetic. You have two options: round the result, or use the java BigDecimal class. If you want to round the result, you can use:
float from = 0.025f;
float to = 1;
double value = 7 * from / to;
DecimalFormat ds = new DecimalFormat("#.###");
double rounded = Double.parseDouble(ds.format(value));
System.out.println(rounded);
If you would rather not have to round, you can use the BigDecimal class.
Java BigDecimal

Multiplication of java float variable [duplicate]

This question already has answers here:
Double calculation producing odd result [duplicate]
(3 answers)
Closed 7 years ago.
I have a piece of code as below.
String wtf = "8.40";
float ft = Float.parseFloat(wtf); //8.4
ft *= 100.0F;
the value of "ft" above is coming as 839.99994
I expected output as 840.00000
How can I correct my code so that it gives me 840.00000 as output
String wtf = "8.40";
double ft = Double.parseDouble(wtf); //8.4
ft *= 100.0F;
There's is a lot of rounding when you use float. This is why you get this "unexpected" value. And I use parseDouble because the cost is less.
I suggest using BigDecimal for arithmetic and calculations.
BigDecimal BDa = new BigDecimal("8.40");
BigDecimal BDc = BDa.multiply(new BigDecimal("100.0"));
Use Double.valueOf(wtf) instead of Float.parseFloat(wtf)

How to roundup a Double value up to two precision [duplicate]

This question already has answers here:
Round a double to 2 decimal places [duplicate]
(13 answers)
Closed 9 years ago.
I have a Double value Double val = 49.569632
How can I roundup the val to get 49.57
You can use the DecimalFormat.
double d = 4.569632;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
Or you can use the below method as mentioned in this answer as Luiggi Mendoza suggested.
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
}
A simple way to round is when printing
double val = 49.569632; // this should be a primitive, not an object
System.out.printf("%.2f%n", val);
or you can round the value first
double rounded = Math.round(val * 1e2) / 1e2;
System.out.println(rounded);
IMHO Using BigDecimal is slower, more complicated to write and no less error prone than using double if you know what you are doing. I know many developer prefer to use a library than write code themselves. ;)

Categories