Wrong result when calculating floats in Eclipse [duplicate] - java

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

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

How to devide a BigInteger by a double in Java? [duplicate]

This question already has answers here:
How can I divide properly using BigDecimal
(2 answers)
Closed 5 years ago.
The title says it all: How do I divide a BigInteger by a floating point number in Java? I don’t need the fraction part of the division, it is okay to have it either rounded or truncated (however I would be interested which one applies).
The “obvious” does not even compile:
BigInteger x = BigInteger.valueOf(73).pow(42);
BigInteger y = x.divide(Math.PI); // The method divide(BigInteger) in the type BigInteger is
// not applicable for the arguments (double)
System.out.println(y);
I expected this one to work:
BigInteger y = new BigDecimal(x).divide(BigDecimal.valueOf(Math.PI)).toBigInteger();
Unluckily, it gives an ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. This is true for π, of course…
Of course, this one works, but it is way too slow…
BigInteger y = BigInteger.valueOf(-1);
BigDecimal σ = BigDecimal.ZERO;
while(σ.compareTo(new BigDecimal(x)) < 0) {
y = y.add(BigInteger.ONE);
σ = σ.add(BigDecimal.valueOf(Math.PI));
}
What’s the correct, canonical way?
You have to add RoundingMode to divide function, otherwise java doesn't know how to round the division and gives you ArithmeticException
BigInteger y = new BigDecimal(y).divide(BigDecimal.valueOf(Math.PI), RoundingMode.HALF_UP).toBigInteger();
All Rounding types are well explained in the documentation link above.

Conversion of doubles to int [duplicate]

This question already has answers here:
convert double into int
(1 answer)
Cast Double to Integer in Java
(19 answers)
Closed 5 years ago.
A project I have requires the movement of a player at a coordinate using getX and getY, however, I am confused on how to convert getX and getY from a double to an int so i can play them in the drawing panel.
Just casting to int will truncate the double. So you need to specify what result you really want before you decide how to get the int. For example, if the double value is 2.999, do you want your int to be 2 or 3?
If you want the closest int (3 above), then use Math.round(d) which returns a long.
You want to cast your double as an int but you need to be careful about how you cast because if you want your movement to be accurate you want values of x.5 and over to be rounded up and values below x.5 to be rounded down.
Casting will always round down so a good way to properly round is to add .5 to all of your doubles before you cast to an int.
Here are a few examples
double = 1.1
int (double) = 1
double = 1.7
int (double) = 1 \\ Note that we will likely want this to be 2
Using our method lets see how these 2 doubles would be casted
double = 1.1
double + .5 = 1.6
int (double) = 1
double 1.7 = 1.7
double + .5 = 2.2
int (double) = 2
Note that now our doubles that are above x.5 will be rounded up properly.

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)

very simple: 1/20 returns 0.0 [duplicate]

This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Closed 7 years ago.
In Processing I have this code:
int numberOfSteps = 20;
float numberOfStepsCalculated = (1/20);
println(numberOfStepsCalculated);
But my print keeps returning 0.0
I can not figure out why!
Thanks for my dumb question
Integer divison.
float numberOfStepsCalculated = (1.0f / numberOfSteps); // 20
When you divide two int(s) the result is an int (or 0). You then widen the result to a float (or 0.0). Promote one of the values in the calculation to a float and you'll get a float result.

Categories