This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 6 years ago.
When dividing two 'int' variables and saving the result into a 'double' variable, anything to the right of the decimal point is just zero?
See the three examples below.
Thank you in advance, Mike
Example 1
public class MyClass {
public static void main(String[] args) {
int x, y, answer;
x = 70;
y = 30;
answer = x / y;
System.out.print(answer);
}
}
Output = 2 (I understand the result, all variables defined as 'int')
Example 2
public class MyClass {
public static void main(String[] args) {
int x, y;
double answer;
x = 70;
y = 30;
answer = x / y;
System.out.print(answer);
}
}
Output = 2.0 (I don't understand the result, the variable answer is 'double' and I expected 2.3333333333333335)
Example 3
public class MyClass {
public static void main(String[] args) {
double x, y, answer;
x = 70;
y = 30;
answer = x / y;
System.out.print(answer);
}
}
Output = 2.3333333333333335 (I understand the result, all variables defined as 'double')
Let's get a closer look at how Java executes this line in the second piece of code:
answer = x / y;
First, Java sees a = operator so it knows that this is an assignment statement. To evaluate an assignment, evaluate the expression on the rignt then put the result into the variable on the left. Therefore, it evaluates the right hand side first.
x / y
Hmm... what could be the result of that? x is an int and y is an int and you have a / operator. I know the division operator can be applied to two int operands, so let me get the value of x and y. Ah! It's 70 / 30! Since it is an integer divided by an integer, the result must be an integer! The result is 2!
Now the assignment becomes:
answer = 2;
Java finds an integer on the right and a double variable on the left, so it converts 2 into a double and puts it in the variable.
Dividing two integers will yield an integer (which is 2 in this case). It just so happens that you chose to store this integer in a double, so it is now represented as 2.0.
The first code reads everything as int and gives an int. The second code reads two ints and gives the double of the ints (example: 10/5 = 2, but 2 in double is 2.0). In the end the third code reads everything as double so for example 70/30 (read as 70.0/30.0) = 2.33...
The Example 2, when compute answer = x / y, the first compute x / y and result is 2 then convert the double type 2.0.
Related
My task is to implement the cos(x) function withou using Math. library and with the taylor polynom, my code looks like this:
public class Cosinus {
public static void main(String[] args) {
/*if(args.length == 0){
System.out.println("ERROR: Geben Sie ein Argument für x ein!");
return;
}*/
double x = 5;
double summand1 = (x*x) / 2;
double summand2 = (x*x*x*x) / (2*3*4);
double summand3 = (x*x*x*x*x*x) / (2*3*4*5*6);
double summand4 = (x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8);
double summand5 = (x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10);
double summand6 = (x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12);
double summand7 = (x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14);
//double summand8 = (x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16);
//double summand9 = (x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18);
//double summand10 = (x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x) / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20);
double cosinusFunktion = (((((((1 - summand1) + summand2) - summand3) + summand4) - summand5) + summand6) - summand7);
System.out.println(cosinusFunktion);
}
}
For x = 1, 2, 3, and 4 Y is between 1 and -1
but with x = 5 it goes too -4 and if the x are even getting bigger this continues too 1287918274.
I cant solve this task but tthe task says it is enough to implement this funktion iwth the taylor polynom and the first 11 summand. I tried this too, but then even with x = 1 the bounds are broken. How can i solve this, so x = 42.5 is in bound of -1 and 1?
Tried more summands to make the result more excact, but the bounds get broken even more.
tried implement the periodicity of x-2*PI, but I dont know where to put it and results get messed up eeven more.
you are getting an integer overflow for the factorial in the summand7 line
as a simple fix you can change the line to:
double summand7 = (x*x*x*x*x*x*x*x*x*x*x*x*x*x) / ((double) 2*3*4*5*6*7*8*9*10*11*12*13*14);
The Taylor expansion will always blow up for larger inputs. However, since:
sin(x) = sin(x + n*2*pi) // for any integer n
You can simply pre-process you input with a modulus function to prevent your output from blowing up.
I can't test compile right now, but if memory serves, you would add one of the following lines prior to computing your first summand:
x = x%(Math.PI*2)
Or, if you can't use Math
x = x%((double)3.14159265358979323846*2)
I know that in Python you can do floor division like this:
5 // 2 #2
The // is used for something totally different in Java. Is there any way to do floor division in Java?
You can do
double val = 5 / 2;
int answer = Math.floor(val);
OR
int answer = Math.floorDiv(5, 2);
If you were to call System.out.println(answer); the output would be
2
You can easily use Math.floorDiv() method.
For example:
int a = 15, b = 2;
System.out.println(Math.floorDiv(a, b));
// Expected output: 7
If you're using integers in the division and you cast the solution to another integer (by storing the result in another integer variable), the division is already a floor division:
int a = 5;
int b = 2;
int c = 5/2;
System.out.println(c);
>> 2
As khelwood stated, it also works with negative values but the rounding is done towards 0. For example, -1.7 would be rounded to -1.
use floorDiv()
int x = 10;
int y = 3;
System.out.println(Math.floorDiv(x,y));
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 6 years ago.
If i print the value of x for :
int a=1;
int b=6;
float x=(a/b);
The output is 0.0
But if i change the third line to
float x = (float)a/(float)b;
The output is 0.1666667(which it should be)
Why the difference?
a/b is calculated as the division of two integers and therefore its result is an integer, and so everything beyond the decimal floating point is truncated and you end up with 0.0.
For example: if you had:
int a = 6;
int b = 4;
float x =(a/b);
You would get that x = 1.0 because the result of the calculation is truncated, and then when you assign it to a float, the integer result value 1 gets cast to a float, i.e. 1.0.
When you calculate (float)a/(float)b, you're calculating the division of two float variables and the result is float (the same would happen if only one of the variables / values was float and the other one was integer).
That is because both of the variables are of type int and hence the division returns an int.
You'll have to cast at least one of them to float in order to force the division to return the result in float.
Here is the code snippet:
public static void main (String[] args) throws Exception {
int a = 1;
int b = 6;
float x = ((float)a/b);
System.out.println("Result: " + x);
}
Output:
Result: 0.16666667
Note: In the above example, I have casted a to float. You can also cast b to float instead of a as follows:
float x = (a/(float)b);
Hi I'm trying to run a calculation but I can't seem to put it all on one line, I have to split the result into two seperate lines to achieve the desired result (which seems a bit long winded).
Can anyone explain where I'm going wrong?
both x and y are doubles.
Example 1: (incorrect)
y=0.68
x= (Math.round(y* 10))/10;
result x=0
Example 2: (correct)
y=0.68
x= Math.round(y* 10);
x = x/10;
result x=0.7
thanks for your time.
Math.round returns variable of type long (see: Javadoc), which means that the division by 10 is performed on a long variable resulting in another long variable - that's why you lose the precision.
To make it calculate on it as on double and return double - you have to cast the result of Math.round like this:
x= ((double)Math.round(y* 10))/10;
Have you tried to explicitly specify double in your calculation:
x = ((double)Math.round( y * 10.0)) / 10.0;
Math.round returns a long....
It's hard to tell from your snippets, because they don't include variable types, but it's likely to be integer division that's killing you. When you divide two integers x and y, where x < y, you get zero:
int x = 4;
int y = 10;
int z = x/y; // this is zero.
y=0.68
x= (Math.round(y* 10)) <--- Evaluated as int since Math.round returns int /10; <-- integer division
result x=0
y=0.68
x= Math.round(y* 10) <-- x is stored as double
x = x/10; <-- double division
result x=7
I guess it's because Math.round returns either a long or an int, depending on whether y is double or float. an then you have an integer division.
in the second example x is already a double and that's why you have a double division.
When you write:
double x = (Math.round(y* 10))/10;
(Math.round(y* 10)) is a long (= 7), which you divide by 10, that gives another long (= 0). The result is then converted back to a double and stored in x.
In your second snippet:
double x = Math.round(y* 10);
This is equal to 7 and converted into a double. x / 10 is then a double operation that returns 0.7.
This is how I am creating q
Double q = ((r * (i/5)) + y);
at this point the values of the other variables are
r = 3.470694142992069E-5
i = 1
y = -116.30237535361584
but
q = -116.30237535361584
is there something wrong with this math? ( Java )
q should be -116.30236841222755
i and 5 are both integers, so the (i/5) portion evaluates to an integer (0). That negates the multiplication by r, so you're left with only the value for y.
Try
Double q = ((r * ((double)i/5)) + y);
Here's the complete code.
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
double r = 3.470694142992069E-5;
int i = 1;
double y = -116.30237535361584;
Double q = ((r * ((double)i/5)) + y);
System.out.println(q);
}
}
Output:
-116.30236841222755
If i is an integer (which seems to be the case), then the i/5 expression will perform integer math resulting in zero.
i is not a double. Integer division floors. Anything times 0 is 0.
maybe you can try
Double q = ((r * i/5.0) + y);
Floating point values are notoriously imprecise. The difference you're showing can be expected for double arithmetic. If you really need the extra precision, jquantity is an open source Java library for precise math.