Wrong result after multipling floats [duplicate] - java

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have odd problem in my java code. Why does every number lower than 0.4f multiplied by 0.0025f gives me wrong results.
Correct result:
0.4f * 0.0025f = 0.001f
Wrong results:
0.399999f * 0.0025f = 9.999975E-4
instead of 0.000999998f
0.33333f * 0.0025f = 8.33325E-4
instead of 0.000833325f
0.11111f * 0.0025f = 2.77775E-4
instead of 0.000277775f

How are you printing your results. You should maybe look at that. It sounds like you don't understand exponential notation.
9.999975E-4 == 0.000999975
The E-4 just means shift the decimal 4 places to the right.
Furthermore, you're doing your own math wrong. You have a number ending in a 9 multiplied by a number ending in 5, which means the answer is going to end in a 5 (9 x 5 is 45, after all). So it's NOT going to be 0.000999998. You got that answer from something that rounded it, perhaps a calculator that won't show it all the way out.
You don't have a math problem. You have a display problem, and not really. It's that you don't understand the display.
Perhaps look up the printf methods and use a format string with lots of room for data after the decimal.

Related

How do I round a double to the thirds place? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 2 years ago.
double1 = .05100000000003;
double2 = .05295;
How do I round a decimal to three places? Respectively, I'd like the output to do this 0.051 and 0.053.
I tried Math.round(double1); but I got back 0.0.
Any ideas would be greatly appreciated thanks.
Try the following code in Java. The number of zeros indicates how many decimal places you would like to round to. (In this case there are 3 zeros).
(double)Math.round(value * 1000d) / 1000d
EDIT: same answer is provided in this previous StackOverflow Post

Java - Math.asin() only returns 0 [duplicate]

This question already has an answer here:
Division between integers in Java
(1 answer)
Closed 3 years ago.
I've been struggling with Math.asin() for a while.
System.out.println(Math.toDegrees(Math.asin(1 / 2)));
This very simple line of code pretty much sums up my desperation: The result should be 30°, however, I only get 0.0 as a result. Its not just these numbers, no matter which numbers I use, the result is 0.0.
My question is, is this a known bug of Java, or am I missing something?
Bonus Information:
I need asin() to calculate the angle between the centerpoints of two objects in my game.
You’re doing integer division, please add a decimal point in the end of each number like this:
System.out.println(Math.toDegrees(Math.asin(1.0/2.0)))

integer overflow in java [duplicate]

This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
Closed 3 years ago.
I though that the following pieces of code does not compile. However after running it I got unexpected result, I don't understand how it is printing -2 ? can you explain how addition is done here?
int x = 2147483647+2147483647; // it compiles
System.out.print(x); // prints -2
any explain is welcome
Short answer: When Java integers reach their maximum value plus one, they start at their minimum again. It's like going in a circle.
It's like that due to the technical representation of integers as bits. Imagine having 3 bits available to represent a number. You could have the number 111. If you add 1 to it, you'll end up at 1000 but since you only have 3 bits available, it cuts off the first and you end up with 000 which is why you're at the minimum value again after adding 1 to the maximum.

Round a double value to 10 digits in Java? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I was looking for a simple way to round a double value to 10 digits but I didn't found even one way that look good to me, everything was too complicated.
I hope someone could help me, for example: the value 0.83426945721236485 will become 0.8342694572
Thank you in advance.
Simple.
DecimalFormat df = new DecimalFormat("0.0000000000");
System.out.println(df.format(0.83426945721236485));
Take a look at the documentation:
https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html
Decimal places are not meaningful internally for double, because it is a binary floating point.
You can, of course, choose to display it in any format supported by DecimalFormat, as suggested in a prior answer.
If you want a ten decimal place internal representation, you should be using BigDecimal with scale factor 10.

Double always returns 0 when dividing ints [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Dividing by 100 returns 0
(6 answers)
Closed 9 years ago.
Would someone mind explaining why this doesn't work? All variables except for chance are ints, whereas chance is a double. When I print all the values they are definitely correct... but chance always comes out as 0.0. I know this has something to do with converting ints to doubles, as I have had an issue like this a couple of times before. What is the key to getting it to do what you want?
gladValue = (glad.dexterity+glad.tactical+weaponSkill);
oppValue = (glad.target.dexterity+glad.target.tactical+glad.target.agility);
chance = (gladValue/oppValue)*10.0;
Thanks
You should write gladValue * 10.0 / oppValue instead.
The reason is quite subtle. Your brackets mean that gladValue / oppValue is computed first. But these variables are integers so the result is an integer and therefore you lose the fraction part. Only when it is multipled by 10.0 will it get promoted to a double; but by then it's too late.
If you do as I say then, bearing mind that * and / have the same precedence and the operations happen from left to right, then when computing gladValue * 10.0, gladValue is promoted to floating point and that floating point result is divided by oppValue.

Categories