Dividing with double, 0 as a result? [duplicate] - java

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Integer division: How do you produce a double?
(11 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Closed 4 years ago.
So it is obvious that
int x = 3/4; // x is 0 here
But i do not understand why the following is also the case:
double x = 3/4; // x is also 0 here
With the following i am able to get the result i want:
double x = (double) 3/4; // x is 0.75 here, but why do i have to cast?

Related

Why doesn't Math.pow return me the correct value? [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 2 years ago.
I am trying to get the correct value for:
double poweredValue = Math.pow((1.1),(1 / 365));
Using my scientific calculator I get 1.0002611578760678121616866817054‬. But Math.pow returns a 1.
How do I get the full value returned?
double poweredValue = Math.pow((1.1),(1 / 365.0));
System.out.println(poweredValue);
Output -
1.0002611578760678

how to set a variable to 0.001 in JAVA (android) [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 3 years ago.
when i want to get the result of 100/100000..
i got only 0.
Example
int one = 100;
int two = 100000;
int result = one/two;
toast(result); //Result is 0
Hey there "int" data type only stores integer values and not the decimals.
So if you divide 3 with 2 you would get 1 as answer instead of 1.5 .
Int just ignores the decimals .
You need to choose float or double data type for this to work.
Your variable named result must be declared and casted to float data type.
Appreciate the effort and mark this as answer if it helps you.....

Eclipse result integer value only [duplicate]

This question already has answers here:
Division operation is giving me the wrong result [duplicate]
(3 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
Here is my code, Eclipse always return integer value of results, even if h is double. Please help me to fix this.
public static void main(String[] args) {
double h=0.0;
for(int i=1;i<=1000;i++) {
h=h+ 1/i;
}
System.out.println("Harmonic sum "+h);
System.out.println("Harmonic sum "+String.format("%.4f", h));
Result:
Harmonic sum 1.0
Harmonic sum 1,0000
You need to cast the result of your division into double
Try this out:
h=h+ (double)1/i;
I hope this is what you were looking for.

Are mathematical operations different for different data types? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 5 years ago.
Why is it that in the first line sevenTwelfths will evaluate to the expected answer (0.5833), but threeTwentySixths will evaluate to zero? I assumed that since the data type is a double, the operation of dividing 3 by 26 would be a decimal, but it appears as if it does the operation as an integer operation and then coverts that answer to a double and stores it in threeTwentySixths.
double sevenTwelfths = ((double) 7 / 12);
double threeTwentySixths = 3 / 26;
double sevenTwelfths = ((double) 7 / 12);
In First line this called typecasting meaning you are converting your answer in double for example
int sevenTwelfths=((int)7/12));
this mean after dividing 7/12 whatever is the anser convert into int or Type cast into int

My java program show 0.0 in 15/100 [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 6 years ago.
Why does this program show 0.0?.
Below is my code:
class new1{
public static void main (String[]args){
int discount = 15;
float discount1 = 15/100;
System.out.println(discount1);
}
}
In line float discount1 = 15/100; is evaluated using the integer division.
If you want to get expected result, you should write like this
float discount1 = 15.0/100;

Categories

Resources