Java Operator Precedence issue for a simple equation [duplicate] - java

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I am trying to implement a simple equation in Java but keep getting the wrong answer apparently due to operator precedence which I am unable to understand.
The equation is:
NewMean = ((N-1) / N) * OldMean + (Xn / N)
in a simple example:
N = 6 ; OldMean = 6 ; Xn = 16
So,
NewMean = 5/6 * 6 + 16/6 = 7.6667 (Correct answer)
but in code implementation on Java i get wrong answer (2.6665):
double NewMean = ((N-1)/N)*oldMean + (Xn/N);

If the N variable is type int, then ((N-1) / N) is computed using integer division and will round 5/6 down to 0. Change N to a floating-point type and you should get the correct answer.

Related

Convert Number in Java [duplicate]

This question already has answers here:
java how to make user friendly percentage output from float number
(9 answers)
Closed 3 years ago.
How to round a number in Java?
Input: 0.655308; Expected output: 65.53.
Input: 1.0583104; Expected output: 105.83.
In power builder compute expression I use
act_qty *work_hour /
if (on_hour < work_hour ) /
sec_setm_gole_qty ,4)
How to run it in Java?
I would just multiply the value with 100
double roundedValue = value * 100;
System.out.printf("%.2f", roundedValue);
String.format("%.2f%%",value*100)?

double d = 1000 / 3600; result in to a 0? [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I doing a simple calculation from 2 variable and I got in to an issue...
In fact when I try to do " double d = 1000 (which is the first var) / 3600 (which is the 2nd var); it result in a 0. So why ? Any hint about that ?
1000 and 3600 are ints, so when you do 1000 / 3600 you get 0. Then, you are assigning double d to this result of zero. You can instead write 1000.0/3600.0 or if these two numbers are variables, you can cast them to doubles first.

Java casting (cast operator expressions) [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 6 years ago.
The question require me to write a Java program to show the results of the following cast operator expressions:
(double) (23 / 14) + 7.65
My Code:
public class op {
public static void main(String [] args) {
int num = 23/14;
double r1 = (double) num;
double result = r1 + 7.65;
System.out.println("Results: "+ result);
}
}
I don't think I have done correctly, what are the problems of my code?
By the way, can someone tell me what are the differences between long, double, int, float? How do we know when to use these primitive data types? I read an explanation here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
but is there any 'human-version' of the explanation?
Thank you for your help.
The problem is due to the used types.
Since you divide two integers (23 and 14), the result is considered and int as well. Therefor, 23/14 = 1.642857142857143, which is truncated to fit in an int result, more specifically, 1.
result is the sum of 1 (int) and 7.65 (double). Since one of them is a double, to other is converted to the "upper" type as well (double) and the operation becomes 1.0+7.65 = 8.65.
The result is correct, because you asked the result of (double) (23 / 14) + 7.65 which means the result of casting the result of the operations in brackets to double summed with 7.65. Which is 8.65 as previously explained.
If you want to use a division using doubles, consider:
double r1 = 1.0 * 23/14;
Lets see step-by-step:
int num = 23/14; // int division of 23/14 results in 1
So, here num = 1
When you cast num to double value of r1 is setted to 1.0.
double result = r1 + 7.65; //1.0 + 7.65 = 8.65
ok, int is short term for INTEGER which are natural numbers that we use normally but with no decimal places and if your number has some value in between roughly -2 billion to +2 billion. if your range exceeds that and you still want an integer then go for long data type.
floats are for decimal values like 3.147 with a range of +10*38 to -10*38 or so, but if your range exceeds this(practically this happens rarely) go for double.
coming to the code you put here , if you divide a int by another int (like 23/14) you get only get the integer part of the answer(only '1' in 23/14=1.642...) , next when you cast it to double you get 1.0 and next you are going to add that to 7.65 which will make the ultimate answer as 8.65 hope this answers your Q....
You could change this int num = 23/14
to double num = ((double) 23)/14
or double num = (23 * 1.0)/14

How to get a int from a long double in java? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
I wanna do this math in java:
int index = 3 * (9568/20001);
in my calculator it shows 3 *( 0.47837608... ) which is 1.43512824..
but, In Java that always give me 0, even I were trying use format, or java.lang.Math.round.
The first postion int 1 of 1.43512824 is what I want to get.
Try this
int index = (int)3 * (9568.0/20001);
Because an integer divided by an integer gives a integer in java thus your answer will not be accurate. If you write 9568.0/20001 it gives a double result and so result is more accurate.

Java divider is giving Zero value [duplicate]

This question already has answers here:
Why double width = 50/110000; the output is 0.000000000000000?
(3 answers)
Closed 9 years ago.
I know this is very stupid question but need to ask,
I have values
int i = 12;
int j = 11;
float k = (i*j)/100;
result is giving 0.0 but here i want more that 2 digits decimal points , How can i achieve it,
I am getting wrong data, it is showing 0 which is wrong
Because all the calculation on the right hand side are of in integer, that is why the result 0.
At least one of the operand should be a floating point number like:
float k= (i * j) / 100.0;
In primary school I learnt integer division. We used to calculate things like 13 divides by 5 is 2 remainder 3. The maths you learnt at school still applies in the computing world. 11 * 12 is 132 and 132 / 100 is 1 and 132 % 100 is 32 (the remainder)
I wouldn't use float as double has half a billion times the accuracy.
double k = (i * j) / 100.0;

Categories