What value are alpha/beta to begin with in Minimax algorithm? - java

I understand the algorithm, as it applies to Alpha-Beta pruning. What I don't understand is since there is no way to represent ∞ in Java, on my first call to the Minimax method, what value should Alpha and Beta be to start out with? (Normally I would think you could make them -∞ and +∞). The only thing I could think of would be 0, but would that produce some unwanted results? Thanks!

It depends on the data type you're using. -∞ and +∞ simply mean the lowest and highest values possible.
If you pick int, the respective values can be Integer.MIN_VALUE and Integer.MAX_VALUE. The algorithm will work just fine.
Also, infinity can be represented in Java. If you really want to, you can use float, which has both a positive and negative infinity value. You could just use Float.POSITIVE_INFINITY and Float.NEGATIVE_INFINITY. For this algorithm,though, I'd stick with integers. Just because they're free from all possibly unexpected behavior related to rounding and precision.

int alpha = Integer.MIN_VALUE
int beta = Integer.MAX_VALUE
Best you can do with no infinity.

Related

How to round down Values in Spark sql

I am querying a postgres sql using the following query. I need to round down the value of min(months_between(current_date,somedate))
for ex: 13.83 should be13 and not 14, even 13.9 should be 13 as well. Is there any function that can round down this value insparksql? Any help will be highly appreciated.
You can use the floor() function. It rounds down its argument.
Note that floor(x) gives the largest integer ≤x. So floor(5.8) returns 5, but floor(-5.8) return -6. If your values may be negative and you want to round them towards 0, you must test their sign and use either floor() or ceil() (that rounds to the upper value).
Also note that casting a float to an int with int() rounds it towards zero whatever its sign. Not sure of the actual behavior in spark-sql, but it may solve also your problem.
Please try using the floor function, reference this link:
https://spark.apache.org/docs/2.3.0/api/sql/index.html#floor

Is there anyway to compute a negative base to the power of a non integer exponent in Java?

According to my own experience and to the Java documentation, in Math.pow(i,j), whenever i is a negative number and j is a non integer, Math.pow will always return NaN, however, according to all of the different calculators I have tried, there are some situations where there is a real solution to a negative base to the power a non integer exponent.
For example:
System.out.println(Math.pow(-3, 0.6));
returns NaN
Are there any work arounds to this?
Thanks in advance.
How would you define that power? The standard method is to find the polar decomposition resp. complex logarithm of the base and plug the power into that,
pow(-3, 0.6) = exp( 0.6 * (log(3) + i*pi) )
= pow(3,0.6) * (cos(0.6*pi) + i*sin(0.6*pi))
Now you could also chose other branches of the logarithm to represent Ln(-3), as it is also true that -1=exp(-i*pi)=exp(i*3*pi)=exp(-5*i*pi)=exp(i*7*pi)=…. Only one among those variants will give a real result to the power. However, how is the computer to know that you want exactly that variant? And what do you do for pow(-3,0.61)?
According to my own experience and to the Java documentation, in Math.pow(i,j), whenever i is a negative number and j is a non integer, Math.pow will always return NaN, however, according to all of the different calculators I have tried, there are some situations where there is a real solution to a negative base to the power a non integer exponent.
For example:
System.out.println(Math.pow(-3, 0.6));
returns NaN
There are not any work arounds to this sorry

How to do custom rounding of numbers in Java?

Suppose I want to round numbers that have mantissa greater than 0.3 'up' and those below 'down'.
How can I do it in Java?
The only thing that came to my mind was Math.round(), but I can't seem to make it follow a certain rule.
Math.floor(x+0.7) should do it.
This should work for an arbitrary mantissa. Just add the offset to the next integer to your value and round down.
The rounding is done by floor. Here is what the java API says to floor:
Returns the largest (closest to positive infinity) double value that
is less than or equal to the argument and is equal to a mathematical
integer.
This solution is similar to the one from #Thomas Stets, but imho it is easier to understand since rounding is done in only one direction.

Nan from maths equation

Hi I have the following equation in a piece of java code:
double z = 0.002378 * (Math.pow((1 - (Math.pow(6.875, -6) * y)), 4.2561));
when I set y to be very large values, i.e 200000 I get Nan (Not a number) It's working okay at slightly lower values, 130000
Can anyone tell me why that is?
Additionally I've tried to port the above code from an original BASIC program:
.002378*(1-(6.875*10^-6*ALT))^4.2561
I may have done it wrong? The order of operations isn't very explicit in the BASIC code
Thanks
As the Javadoc for Math.pow explains:
If the first argument is finite and less than zero [… and] the second argument is finite and not an integer, then the result is NaN.
So whenever your y is great enough that 1 - (Math.pow(6.875, -6) * y is negative, you'll get NaN.
(This makes sense when you consider the underlying math. A negative number to a non-integer power is not a real number, and double has no way to represent complex numbers.)
Edited for updated question:
Your Basic code has 6.875*10^-6 (meaning 6.875 × 10−6), but your Java code has Math.pow(6.875, -6) (meaning 6.875−6), which is a somewhat greater value, so your Java code triggers this problem for somewhat smaller values of y. This may be why you're seeing this problem now. To match the Basic code, you should change Math.pow(6.875, -6) to 6.875e-6.
Raising a negative number to a non-integer power results in an imaginary number in complex number mathematics, a NaN in Java arithmetic. If you really need to do that calculation, you need a complex number package. However, it is more likely that there is an error in your equation or you are trying to use it outside its range of validity.
Negtive number with real number power may get NAN

Rounding negative numbers in Java

According to Wikipedia when rounding a negative number, you round the absolute number. So by that reasoning, -3.5 would be rounded to -4. But when I use java.lang.Math.round(-3.5) returns -3. Can someone please explain this?
According to the javadoc
Returns the closest long to the
argument. The result is rounded to an
integer by adding 1/2, taking the
floor of the result, and casting the
result to type long. In other words,
the result is equal to the value of
the expression:
(long)Math.floor(a + 0.5d)
Conceptually, you round up. In other words, to the next integer greater than the value and -3 is greater than -3.5, while -4 is less.
There are a variety of methods of rounding; the one you are looking at is called Symmetrical Arithmetic Rounding (as it states). The section you are referring to states: "This method occurs commonly used in mathematical applications, for example in accounting. It is the one generally taught in elementary mathematics classes." This seems to acknowledge that it is not a rule that is globally agreed upon, just the one that is most common.
Personally, I don't recall ever being taught that rule in school. My understanding of rounding has always been that .5 is rounded up, regardless of the sign of the number. Apparently the authors of Java have the same understanding. This is Asymmetrical Arithmetic Rounding.
Different tools and languages potentially use different rounding schemes. Excel apparently uses the symmetric method.
(Overall, I would advise that if you find a conflict between Wikipedia and experience, you look for information elsewhere. Wikipedia is not perfect.)
For what it's worth, java.math.BigDecimal has selectable rounding modes if you need more control over that sort of thing.
The Wikipedia article you cite does not say that's the only way to round, just the common way to round. Also mentioned in that article are several alternatives (unfortunately none of which describe Java's rounding method - even though they call it out as "Asymmetric Arithmetic Rounding" when indicating what JavaScript does).
You need to decide how you want your numbers rounded, then use that method. If Java's implementation matches that, then great. otherwise you'll need to implement it on your own.
According to Javadocs:
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)
Turns out the convention is to round up. I guess Wikipedia is fallible. Turns out Microsoft got it wrong, though, as they round it to -4 as well, which is not convention (I checked with someone who has a PhD in math).
//for Example dividing an Int A with 200 ;
public class Solution {
public int solve(int A) {
double B = (double) A /200;
if (B<0){
B= (int) Math.round(Math.abs(B));
return (int) B * -1 ;
}
else{
B= (int) Math.round(B);
return (int) B ;
}
}
}

Categories