I have some lines of code that produces a number after calculation which is currently in JavaScript and here is the code:
if ( y2 != y1 )
{
// calculate rate
var f;
var y;
if ( y2 > y1 )
{
f = cpi[y2] / cpi[y1];
y = y2 - y1;
}
else
{
f = cpi[y1] / cpi[y2];
y = y1 - y2;
}
var r = Math.pow(f, 1/y);
r = (r-1)*100;
r = Math.round(r*100) / 100;
System.out.println( "number: " + r.toFixed(2) + "%." );
}
I converted the above JS code to Java and here is the code:
DecimalFormat decimalFormat = new DecimalFormat("0.##");
if (cpi[to] != cpi[from]) {
double f, y;
if (cpi[to] > cpi[from]) {
f = cpi[to] / cpi [from];
y = to - from;
}
else {
f = cpi[from] / cpi[to];
y = from - to;
}
q = Math.pow(f, 1/y);
q = (q-1)*100;
q = Math.round(q*100)/100;
Toast.makeText(getApplicationContext(), "number: " + String.valueOf(decimalFormat.format(q)), 2000).show();
}
The JavaScript code produces: number: 2.39
While the Java code produces number: 2
Why am I getting two different value? I will post what cpi[to], cpi[from], to and from values are if needed.
In this line
q = Math.round(q*100)/100;
both operands of the division operation are integral, therefore the result is also an integral type. Use 100.0 as the divisor to coerce the result to a double.
what is q type?
try to put 100.0 also
If you devide two integers in java the result is integer. Every operation with double and integer or with two doubles creates double.
1)In this line
1)q = Math.round(q*100)/100;
2)you are deviding two integers, so it has same output as:
2)q = (int) (Math.round(q*100)/100);
3)you can use casting to double for example:
3)q = Math.round(q*100)/(double)100;
4)or using the 100.0 which makes this number double:
4)q = Math.round(q*100)/100.0;
5)this should work too, because first the result of Math.round is converted to double and then devided by 100:
5)q = (double)Math.round(q*100)/100;
6)However this WILL NOT work, because first Math.round is devided by 100 and it creates integer, so the result of this operation is rounded down and AFTER then casted to double. So it will be double but still rounded down, because it was rounded before it becomes double.
6)q = (double)(Math.round(q*100)/100);
Most likely your issue is Math.round(double) returns a long value. So d would contain a long instead of a double at that point.
Related
The following code is designed to factorize a number typed into the variable x.
public class testMod{
public static void main(String[]args){
double x = 11868681080091051216000;
StringBuilder output = new StringBuilder("1 * ");
for(double y = 2; y <= x; y++){
while (x % y == 0) {
System.out.print("Calculating... \n");
String printNumber = y + " * ";
x = x / y;
output.append(printNumber);
System.out.print(output.substring(0, output.length() - 2) + "\n");
}
}
}
}
The problem is that the compiler treats 11868681080091051216000 as an int, regardless of the attempt to assign it to a double. As such, it's out of range.
To specify a double literal, you can simply append D to the end – but do note that you'll lose precision this way:
double x = 11868681080091051216000D;
System.out.println(x); // prints 186868108009105E22
If you need the full precision, you can use a BigInteger instead, but you'll still need to specify that number in expressions that Java can handle, such as a product of its factors.
So I need to calculate a value.
The input I get is this:
a is seed/m2. The value might a for example 56 but it might be 56.7 also.
b is in g's. for instance 600g
c is % value, might be 90.6 also
d is % value, might be 90.6 also
The result I get should be as kg/ha
Regular int does not cut it. The value of (56 * 600 / 100 / 100) / 100
will be 0.0336. I could multiply it with 10000 but I would lose the precision.
I also tried BigDecimal for this but it gave me a ArithmeticException: “Non-terminating decimal expansion; no exact representable decimal result” when I changed the values of my % variables to something else than 100.
What would be the best option to go with this? The calculation was easy to do in exel as it knew how to convert each value automatically, but doing it in Java code is another thing.
My solutions:
int version:
int a = Integer.decode(germinativeSeed.getText().toString());
int b = Integer.decode(seedMass.getText().toString());
int c = Integer.decode(clean.getText().toString());
int d = Integer.decode(germinative.getText().toString());
int result2 = ( a * b / c / d) / 100;
result is 0
BigDecimal solution:
BigDecimal result2;
BigDecimal a = new BigDecimal(germinativeSeed.getText().toString());
BigDecimal b = new BigDecimal(seedMass.getText().toString());
BigDecimal c;
BigDecimal d;
if (clean.getText().toString().equals("")) {
c = new BigDecimal("100");
} else {
c = new BigDecimal(clean.getText().toString());
}
if (germinative.getText().toString().equals("")) {
d = new BigDecimal("100");
} else {
d = new BigDecimal(germinative.getText().toString());
}
BigDecimal hundred = new BigDecimal("100");
BigDecimal test = new BigDecimal("10000");
result2 = a.multiply(b);
result2 = result2.divide(c, 2, RoundingMode.HALF_UP);
result2 = result2.divide(d, 2, RoundingMode.HALF_UP);
result2 = result2.divide(hundred, 2, RoundingMode.HALF_UP);
result2 = result2.multiply(test);
Result is correct with this only if % values are 100%.
double seed = (double) seedInput;
double m2 = (double) m2Input;
double b = (double) bInput; // unit 'g' is not relevant
double c = (double) cInput;
double d = (double) dInput;
double a = seed / m2;
int result2 = ( a * b / c / d) / 100.0;
So I converted everything to double so you won't have problems with implicit conversions to int.
Your problem comes when you have rational numbers like 1/3, this cannot be represented in a bigdecimal, as it has an infinite representation.
If you really need very big precision you should crate a new bigrational class, where you would store a nominator and denominator, and calculate with them. The code would be much mode complicated.
If you don't need that go for doubles.
Try using float or double (preferred double because of the precision).
Let suppose that I have double x. I would return nearest whole number of x. For example:
if x = 6.001 I would return 6
if x = 5.999 I would return 6
I suppose that I should use Math.ceil and Math.floor functions. But I don't know how return nearest whole number...
For your example, it seems that you want to use Math.rint(). It will return the closest integer value given a double.
int valueX = (int) Math.rint(x);
int valueY = (int) Math.rint(y);
public static void main(String[] args) {
double x = 6.001;
double y = 5.999;
System.out.println(Math.round(x)); //outputs 6
System.out.println(Math.round(y)); //outputs 6
}
The simplest method you get taught in most basic computer science classes is probably to add 0.5 (or subtract it, if your double is below 0) and simply cast it to int.
// for the simple case
double someDouble = 6.0001;
int someInt = (int) (someDouble + 0.5);
// negative case
double negativeDouble = -5.6;
int negativeInt = (int) (negativeDouble - 0.5);
// general case
double unknownDouble = (Math.random() - 0.5) * 10;
int unknownInt = (int) (unknownDouble + (unknownDouble < 0? -0.5 : 0.5));
int a = (int) Math.round(doubleVar);
This will round it and cast it to an int.
System.out.print(Math.round(totalCost));
Simple
Math.round() method in Java returns the closed int or long as per the argument
Math.round(0.48) = 0
Math.round(85.6) = 86
Similarly,
Math.ceil gives the smallest integer as per the argument.
Math.round(0.48) = 0
Math.round(85.6) = 85
Math.floor gives the largest integer as per the argument.
Math.round(0.48) = 1
Math.round(85.6) = 86
I have written a simple Java program as shown here:
public class Test {
public static void main(String[] args) {
int i1 =2;
int i2=5;
double d = 3 + i1/i2 +2;
System.out.println(d);
}
}
Since variable d is declared as double I am expecting the result of this program is 5.4 but I got the output as 5.0
Please help me in understanding this.
i1/i2 will be 0. Since i1 and i2 are both integers.
If you have int1/int2, if the answer is not a perfect integer, the digits after the decimal point will be removed. In your case, 2/5 is 0.4, so you'll get 0.
You can cast i1 or i2 to double (the other will be implicitly converted)
double d = 3 + (double)i1/i2 +2;
i1/i2 when converted to int gives 0. ie. why you are getting 5.0. Try this :
public static void main(String args[])
{
int i1 =2;
int i2=5;
double d = 3 + (double)i1/(double)i2 +2;
System.out.println(d);
}
This line is done in parts:
double d = 3 + i1/i2 +2;
double d = 3 + (i1/i2) +2;
double d = 3 + ((int)2/(int)3) +2;
double d = 3 + ((int)0) +2;
double d = (int)5;
double d = 5;
The double just means that the answer will be cast to a double, it doesn't have any effect till the answer is computed. You should write
double d = 3d + (double)i1/i2 +2d; //having one double in each "part" of the calculation will force it to use double maths, 3d and 2d are optional
i1/i2 will be 0 because both i1 and 12 are integers.
if you cast i1 or i2 to double then it will give the desired output.
double d = 3 + (double)i1/i2 +2;
This link provides information about data type conversion, both implicit and explicit type.
To provide exact answer to the question will be :
double d = 3 + (double)i1/i2 + 2
int i1 =2;
int i2=5;
double d = 3 + (double)i1/(double)i2 +2;
if i1/i2 will be fractional value then double will help it to be in fraction instead of int.
so now you will the result as you want. or you can also use following code
double d = 3+(double)i1/i2+2;
In this line i1 is converted into double which will be divided with i2 and result will be in double, so again result will be as 5.4
Since i1=2 and i2=5 are integer type and when you divide (2/5) them, It gives integer value (0) because fractional part(.4) get discarded.
So put (double)i1/i2 on the equation.
The code that I have,
public static void main(String[] args) {
int x = 27;
int y = 5;
double z = x / y;
System.out.println(" x = " + x + " y = "+y +" z = "+z);
}
In the above code I know that to print out the decimal place .4 for the variable z we have to use printf, but my question is why does the variable z is not storing the 5.4 and just storing 5?
I mean int / int then the out put is stored in a double, which is perfectly capable of holding decimal values but it is not, what is the logic?
This is happening because the values that you are dividing with are int and not double and they are not going to output the decimal places, to be more clear take this for example
double z = 27 /5;
same as yours
double z = 27.0/5.0;
now z = 5.4;
So this shows that the datatype that you are performing calculation with also should be the same as the datatype you are expecting the output to be.
You need to cast one of the operands to a double
double z = (double) x / y;
The reason is x / y stand-alone is an int, so it is really evaluating as 5 and then parsing to a double.
You have to cast the integers before you divide I believe.
Like this,
double z = (double) x / (double) y;
What you're doing in the line:
double z = x / y;
is integer division, and then you convert the outcome to double