Why am I getting 0? [duplicate] - java

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 5 years ago.
I keep getting 0 with the following equation, and I'm sure it is something I am missing, but this has been bugging me for the past few days.
int BASE_SIZE = 8;
Point screenSize = new Point(1440,2000);
mMaxSize = mScreenSize.x/BASE_SIZE;
// This line is the line causing issue.
int surfaceViewSize = mMaxSize * ((BASE_SIZE-1)/BASE_SIZE);
This is regardless of if I make the variable an integer, if I use Math.round, I make it a double, anything. I can not for the life of me figure this out.

this integer division here:
(BASE_SIZE-1)/BASE_SIZE
result to be
int surfaceViewSize = mMaxSize * 0;
you need to cast one of the operands into a double or float
replace your operations with:
mMaxSize = 1.0*mScreenSize.x/BASE_SIZE;
int surfaceViewSize = mMaxSize * ((1.0*BASE_SIZE-1)/BASE_SIZE);

int surfaceViewSize = (mMaxSize * (BASE_SIZE-1))/BASE_SIZE;
Try this its just a braces issue

Related

Division in Android Gone Wrong Value [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
When I divided the large number into a small number then the division is correct but when I write the small number to divide a large number the answer returns wrong. In my scenario, the small number always be first. here is my code this code return 7.4074074074074075E-6 but the correct result is 0.0000074074.
double itf = 0.0;
double a = 4.0;
double b = 540000;
itf = a / b;
Log.i(TAG, "savedata: outputvalue=" + itf);
BigDecimal a = new BigDecimal("4");
BigDecimal b = new BigDecimal("540000");
// 0.0000074074
a.divide(b, MathContext.DECIMAL128);
You should use a decimal type. double is outside the scope of support

Why do I get 0.0 when I divide these two integers? [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 show the percentage of day passed using a fixed time. However, when I divide the time passed already by the total amount of time (in seconds) of a day, I get 0.0. I put the current values into the console. Any help is appreciated.
You are performing integer division, and then casting it to a double. You should be doing:
int numOfSecondsSinceMidnight = 61960;
int totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = (((double)numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Or better yet, changing numOfSecondsSinceMidnight and totalDay to doubles:
double numOfSecondsSinceMidnight = 61960;
double totalDay = 86400;
double percentDayPassed = 0;
percentDayPassed = ((numOfSecondsSinceMidnight / totalDay)*100);
System.out.println(percentDayPassed);
Both of which print:
71.71296296296296

Why does a fraction stored in a double become a whole number upon outputting to screen? [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 6 years ago.
So, I have this code which is the beginning of an RSA-encrypter (current math class). Now I'm still in the beginning stages and I notice that when I try to make an array of doubles, they all come out to the screen as whole numbers, what's going on here ?
As you can see in the code I divide the multiples of 7 by 40 and it should come out as fractions (7 / 14 = 0.175 for example) but instead as 0.0.
Help
import java.util.*;
public class Encryption {
static double[] ads = new double[200];
public static void main(String args[]){
for(int i = 0; i < 200; i++){
ads[i] = (7 * i) / 40;
}
for(int i = 0; i < ads.length; i++){
System.out.println(ads[i]);
}
}
}
It's because you're storing an integer into the array in the first place.
Consider this line:
ads[i] = (7 * i) / 40;
In the expression on the right, you first multiply i by 7, then divide it by 40. i is an integer. So, let's say i == 1. Then, 7 * i == 7, which is still an integer. Both 7 and 40 are integers, so when you evaluate 7 / 40, you get integer division. Integer division always rounds down. When the result gets stored into ads[i], it gets converted to a double, but by that point it's already been rounded down.
Try changing this line to the following:
ads[i] = (7 * i) / 40.0;
This works because 40.0 is a double, not an int.
In unrelated news, if you're using double to implement RSA, you're probably doing something wrong. Doubles aren't infinitely precise, and will screw up your result.

Get Float part in Int [duplicate]

This question already has answers here:
How do I get the part after the decimal point in Java?
(15 answers)
Closed 6 years ago.
I am developing an app in which i have to get the float part in terms of integer value.
for example if my number is 153.12324 then output should be 12324.I tried this way but it works wrong some time.
What's the problem in this or is there a better way to do this ?
there is a double value in d
double d =any_double_value;
String[] splitter = String.valueOf(d).split("\\.");
splitter[0].length(); // Before Decimal Count
int count = splitter[1].length();
int integerpart = (int) calcResult;
double floatpart = calcResult - integerpart;
while (count!=0)
{
floatpart=floatpart*10;
count--;
}
int floatpartinInt=(int)floatpart;
this works wrong only in some cases(it gives 1 number less, like if answer is 124 it gives 123) and in cases where answer is long double no like 3.333333333 (10/3)
You can also without split. by using following way,
double value = 3.25;
double fractionalPart = value % 1;
double integralPart = value - fractionalPart;
This may helps you
Use this code its work in your case and enjoy Man
double val=1.9;
String[] arr=String.valueOf(val).split("\\.");
int[] intArr=new int[2];
intArr[0]=Integer.parseInt(arr[0]); // 1
intArr[1]=Integer.parseInt(arr[1]); // 9
You could also do this:
lvalue = (long) value;
fPart = value - lvalue;

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.

Categories