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;
Related
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 does one figure out how many times one has to add a short to get the value of another short?
I know, I know, it's a weirdly phrased question. But in more specifics without all the primitive types of Java...
Basically, I have a short x, which we'll use the placeholder 7 for. I also have a short y, which I'll say is the number 5. And then, I have a short z, which I'll say is 236.
Now, what I want to do, is get an integer that counts how many times I have to add the number y (5) to the number x (7) to get to the maximum value of z (236).
Obviously, I could do that somewhat with a pencil and paper right now, but what I need is something that I can input the 3 values and it will give me the integer as the output - the number of times I have to add the value y to the number x to get to the maximum value of z.
If you still don't understand what I'm doing, then a more visual example is:
(Y * someint) + X = Z How would I get to someint?
int result = (z - x) / y;
The result is int even if division has a remainder which is ignored in this case.
As others have stated this is simple division. But if you're looking for other completely unnecessary ways to do this, heres a loop way...
public static int getNumOfAdds(short x, short y, short target){
int i;
for(i = 0; y*i + x < target; i++){}
return i;
}
val = (Z-X)/Y;
rem_val = (Z-X)%Y;
if (rem_val != 0)
val++;
This should do it.
Recursively?
Try something like
Func(short x , short y, short z, int c)
if ( x >= z )
return c
else
return Func(x+y,y,z,c)
and your first call would be Func(x,y,z,0)
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
I've been reading some code that one of my lecturers set and he has written something like "Let us set a maximum and minimum for a value."
Then he writes (and I'm paraphrasing values here)
int x = 5;
x = Math.min(x, 0);
x = Math.max(x, 10);
Then he carries on with his code as if x is still equal to 5 whereas when I run this code through my computer the max and min functions always change the value of x to 0 and then 10.
Does this sound like a mistake on his part? Should he have reverted x before carrying on? Or does this function work in some other way depending on circumstances that it does actually set a maximum and minimum value without changing the original variable?
You are right; he probably meant this:
int x = 5;
x = Math.max(x, 0);
x = Math.min(x, 10);
which keeps x between 0 and 10.
It is a mistake on his part. When you assign a variable with = it sets it to that value.
Yes. It is a mistake on his part.
I want to subtract one integer from another, and the result should floor at 0. So 2 minus 4 should equal 0. I could just do
int result = x - y;
if (result < 0) result = 0;
But is there a more elegant way?
int result = Math.max(0, x - y);
While a lot of people are rushing out with Math.max(...) solutions, I'd like to offer a simple if statement.
if (y > x) {
result = 0;
} else {
result = x - y;
}
It is guaranteed to always return a result raised to 0, it doesn't require invoking an extra stack frame (entering the Math static function would), and it prevents underflow.
In the rare event that X is close to the minimum int, and y is sufficiently large enough, evaluating (x-y) would result in an underflow. The result would be "too large" of a negative number to fit in an int's space and would therefore roll into a nonsense (and probably positive) answer.
By forcing the if statement to guarantee no underflow exists, this solution is also more correct than the Math.max(...) solutions. However, most people don't care because they rarely deal with numbers that get close to causing overflows and underflows.
Use ternary operator ?:
int result = (x - y) > 0 ? (x - y) : 0;