How to print a DOUBLE with a variable INT - java

I'm doing my school homework and I got stuck. I set my variable as INT and now I can't change it because I'm using it for other calculations. Is there a way which I can print 3 numbers after the dot? I tried with printf and (double) but it didn't work.
This is my piece of code:
for (int i = 0; i < 2; i++) // Printing Inverse
{
System.out.print("[");
output.print("[");
for (int j = 0; j < 2; j++) {
sol[i][j] = adj[i][j] / det;
System.out.printf((double) sol[i][j] + " ");
output.print((double)sol[i][j] + " ");
}
}

By doing the calculation as a int, you are basically "throwing away" information. This means that from that point, it is not possible to get it back.
Since your current code looks like intVariable = intVariable / intVariable; print((double)intVariable), the compiler automatically uses integer division, what basically discards all decimals
You want your code to look like doubleVariable = intVariable / doubleVariable;print(doubleVariable), which would mean casting the divider in the division to a double, and changing the type of sol so it could hold doubles instead of the type you have now.

Is there a way which I can print 3 numbers after the dot? I tried with printf and (double) but it didn't work.
Well casting the int to double is not enough to get the expected output.
To print the casted double with three decimals, you need to use %3f as first parameter in the printf method, like this:
System.out.printf("%.3f", (double) sol[i][j]);
This is a live working demo.
Note:
Note that the result of the division in sol[i][j] = adj[i][j] / det; will be a double, so you need to declare your variables as double instead of int to avoid getting Exceptions.

I'm guessing that sol is a two-dimensional array of int, hence - as #B001 mentioned in his comment - it has no numbers after the decimal point. If all you want to do is print the result of the division: adj[i][j] / det as a double, I suggest using another variable just for printing the result, i.e.
double result = (double) adj[i][j] / det;
sol[i][j] = (int) result;
System.out.printf("%.3f", result);

I think you should redefine this variable again, that will not be catastrofic, just be carefull. Eclipse/NetBean will be notifying you if your variable has totally changed or not.
I am not sure, but I think that somewhere in Eclipse you could find a button that changes your variable and all its references.

Related

Find the modo value

I have given a log value Y , i want to calculate the anti log of Y i.e
ans = (Math.pow(10,Y))%mod
where mod = 1e9+7 and the anti log of Y will always be integer i.e Y is calculate as follow Y= log(a) a is very large integer of range 10^100000
So for given Y i need to calculate ans ? How to do that considering the mod operation.
My Approach
double D = Y -(int)Y
long Pow = (long)Y
for(int i=1;i<=Pow;i++) ans = (ans*10)%mod;
ans = (ans*Math.pow(10,D))%mod
But it's not correct can someone suggest be efficient approach here ? BigDecimal can be useful there ?
For Example:
Y = 16.222122660468525
Using the straight forward method and rounding off i.e Math.log(10,Y) give me 1667718169966651 but using loops it's give me 16677181699666510. I am not using mod now just explaining that there is an error.
Here Y is small so direct method works and we can take mod easily. if Y is range of 10000 it will not work and overflow so we have to used mod.
I guess it's should work
double D = Y -(int)Y
long Pow = (long)Y
for(int i=1;i<=Pow;i++) ans = (ans*10)%mod;
ans = (ans*Math.pow(10,D))
ans = Math.round(ans)
ans%=mod
There is an error in your judgement here - the loop method is not at fault.
The value of a in your example has 17 integer digits. From this stackoverflow post, a double has ~16 significant digits of precision. Thus both the loop and direct calculations are in fact being limited by lack of precision.
(Just to confirm, using a high precision calculator, the value of a is 16677181699666650.8689546562984070600381634077.... Thus both of your values are incorrect - unless you copied them wrongly?)
Thus your loop method is not the problem; you just need a
higher-precision method to do the last step (calculating pow(10, frac(Y))).
As a side note, there is a more efficient way of doing the loop part - this post has more details.

How to generate a sequence between two values in java?

The two values that i have are:
firstval=200.000
Secondval=399.999,
I have to generate a numbers such that when the first decimal part should get incremented till 999 for the integral part, next the integral part should be incremented and then decimal part resets to 000 and starts incrementing for the new number . And this happens till 399.
Like
200.001,200.002.....200.999,201.000,201.002....399.998,399.999"
There is a nice way to get required array with Java 8 Stream API
(1) Use double incrementation
double[] sequence = DoubleStream.iterate(200.0, d -> d + 0.001).limit((int) (1 + (399.999 - 200.0) / 0.001)).toArray();
Note, that summing up lots of doubles will likely give some error, for example on my laptop the last number in the sequence is 399.99899999686227
(2) Better way is to generate integer stream and map it to doubles:
double[] sequence = IntStream.range(200000, 400000).mapToDouble( i -> i * 0.001).toArray();
In this case no error from adding multiple doubles will be accumulated
double start = 200.0;
double end = 399.999;
double increment = 0.001;
for (double v = start; v < end + increment / 2; v += increment) {
System.out.printf("%.3f\n", v);
}
Here's another way to do it:
public static void counterGenerator(double start, double end) {
DecimalFormat counterInDecimalFormat = new DecimalFormat("0.000");
String counterInString = counterInDecimalFormat.format(start);
System.out.println(counterInString); // This is the counter in String format.
double counter = Double.parseDouble(counterInString);
if (counter < end)
counterGenerator(start + 0.001, end);
return;
}
In the above example, you have your counter in String format in the variable called counterInString
But you need not worry about the problems associated with incrementing a Double which is actual residing in a String variable. In the code, you can see the incrementing task is being done by a double counter which gets convert back to String by using the DecimalFormat class.
Here keeping your counter as a String helps you to retain the 0s after decimal in numbers like 200.000.
Hope it helps!

Math.round and Math.ceil not working

I'm trying to round the users input but I can seem to get my double to round to an int. Basically, when I enter 4.4999 it wont round up to 5.
Any ideas?
Math.ceil() returns the ceiled value. It can't change the value of the variable it takes as argument, because Java passes arguments by value. So you need to do
hours = Math.ceil(hours);
The actual solution is to use double inside the ceil method.
Math.ceil(7 * 50 / 100) will return 3.0 even though the actual value resulting from 7*50/100 is 3.5. It is because since everything is int, the result of 350/100 itself will be 3.
If however, if you give Math.ceil(7 * 50 / 100D), the result would be 4.0.
So, the 4.999 in your question should be a double and not a result of an integer operation like 4999/1000.
Just make sure that whatever you give inside a the ceil is double and not an int.
Both function return the rounded (or ceiled) values, but don't change the variable passed as parameter.
Use eg. hours = Math.ceil(hours);.
Math.ciel returns a Double. Something like this should work (inside of your hours > 0 block):
cost += Math.ceil(hours) * hourlyRate;
You're not assigning the result of Math.ceil(hours) back to hours so it will never round.
int a = 15
int b = 2;
int x = (int) Math.ceil( a / b );
int y = (int) Math.ceil( (double) a / (double) b );
Results:
x: 7
y: 8

why is it an infinite for loop?

I am trying to go through a for loop and it is just like any other for loop but it seems to be infinite; I must be doing something stupid. Can anyone see it?
double x;
for(x = -1; x < 1; x+=2/20) {
double y = b * Math.sqrt(1-Math.pow(x-FocusToCenter, 2));
System.out.println("X:"+x+" Y: "+roundFourDecimal(y));
}
And if change x+=2/20, to x+=0.1 then it is perfectly fine.
What does 2/20 evaluate to ? I suspect since they're both integers, you're getting a 0, and hence not increasing x at all.
Trying expressing them as doubles e.g. 2.0 / 20.0
Integer Division , 2/20 = 0
i.e. x=x+0
thus the iterator is not being modified and hence the infinite loop
To have work what you are trying to do, try 2.0/20.0
2/20 is integer arithmetic operation so result is zero. Basically you are not incrementing x in the loop. In order to thread this as float division you need this:
2.0/20
or
((double)2) / 20;

Why does int exp1 = 14/20*100; equals '0' in java?

I'm trying to do some basic math and it keeps popping up as 0. I'm sure it has to do with it being an int but I don't know how to work around it. I need to use integers but the math to arrive at those integers uses decimals. How do I do it?
That's integer division.
To get non-integer results, use doubles instead.
This is not special to blackberry, it's standard java behaviour.
This is because you're doing integer math:
int subexpr1 = 14 / 20; // 0
int subexpr2 = subexpr1 * 100; // 0
Use a double instead or change the order
int expr1 = (int) 14.0/20 * 100; // Very small possibility of rounding errors
int expr2 = 14 * 100 / 20; // Will ignore fraction parts
You can change it to 14*100/20 - and then it will give what you want.
I.e. change the sequence of operations (14/20 is 0)
Your result is being cast as an int, so you are losing precision.
Try
double exp1 = 14/20.0*100;

Categories

Resources