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

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)))

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

Wrong result after multipling floats [duplicate]

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.

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.

How to get 1 or 2 decimal places in Java with float data type [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 8 years ago.
Im having problems with float data type in getting 1 or 2 decimal places only..
but if you look at the code seems okay to me but the output is different.. I dont want to convert into string yet co'z I need to do something else before the printing them out..
Just for testing purposes I did this literally. There are variables and workarounds just to simplify everything. It suppose to end at 12.8 but it yields 12.8000001 that's why it skips my testing..
float oh=11.0f;
int retval=1;
while (oh<=12.8)
{
//System.out.printf("%-11s",dec_format.format(oh)+'K');
System.out.println(oh);
oh=oh + 0.3f;
}
output is:
11.0
11.3
11.6
11.900001
12.200001
12.500001
It should end at 12.8 and why with ....00001??
System.out.printf("%2.3f", 2.887);
prints 2.887
System.out.printf("%2.2f", 2.887);
prints 2.89
System.out.printf("%2.1f", 2.887);
prints 2.9

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