How to round down Values in Spark sql - java

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

Related

How to round doubles to a specified decimal place without Libraries?

Basically I'm supposed to write a method, that uses a double "x" and an int "y" and rounds (>=5 upwards, <5 downwards) "x" to the decimal place specified by y (between 1-8). The method is supposed to return a double. However since I just started I don't have a clue how to achieve this. The exercise prior to this one way easier.
If read answers to similar question but they are not quit what I need, because I can't use the Math library or other libraries. I'm allowed to make auxiliary methods to substitute this.
Rounding like that with double isn't going to work. Doubles and floats are represented with a fixed number of bits of data, and work in binary not decimal. That means that some numbers can't be represented. .1 can't be stored exactly.
In order to do this, you need to do use BigDecimal, which is a class that can store any exact number. Math using BigDecimal is less efficient, but it doesn't have the accuracy issues of doubles.

Why does Java give different answers for fractions with different parentheses

I had to calculate this: 1*5000*500/(10000*24645.16239360158)
But then realized I should multiply it by 1000. I get 2 different answers depending on how I do it. I'm not sure why, though, as the placement of parentheses shouldn't matter for multiplication. Would appreciate any insight!
System.out.println(Double.toString(1000*1*5000*500/(10000*24645.16239360158)));
outputs
-7.283243937828597
for sure incorrect because it's negative
System.out.println(Double.toString(1000*(1*5000*500/(10000*24645.16239360158))));
on the other hand, outputs
10.143978603480635
(correct)
Basically in the second case we multiply the result by a 1000 after we've calculated it, and that somehow works.
Thanks!
You need to know that int * int yields an int (5 * 5). And int * double yields an double (5 * 5.0).
As you probably know, those data types have different maximal sizes and store values in a different way.
Here are some of them:
You can use Integer.MAX_VALUE and Integer.MIN_VALUE for the exact bounds.
If you need to exceed those values, but also want to have an int, you should use the class BigInteger. It can handle arbitrary big integers.
Else you will have what is called an overflow, when you do Integer.MAX_VALUE + 1 the result will be Integer.MIN_VALUE. If you recall how this number is stored in bytes, for example something like 111 then +1 would return 1000 but there is no place to store the first 1, you'll receive 000 which is the MIN_VALUE.
You also should know that when you compute 5 / 2 it will not return 2.5 but 2, as you divide two int, the result will also be an int. If you want to have a double, then you need to do something like this 5 / 2.0 or (5 + 0.0) / 2.
this expression 1000*1*5000*500/(10000*24645.16239360158),first calculate 1000*1*5000*500,the result is -1794967296,overflow.
this expression 1000*(1*5000*500/(10000*24645.16239360158)) overflow do not happend.
The evaluation of any expression is done by operator precedence parser. If you want to change the priority of precedence, You would've to use parentheses.
In the first case '/' and '*' having same priority table.
In second case '*' is getting some priority over '/'. Thats why your getting diffrent values as output.
It is because of Java's implicit type casting. By default Java would use int. As user2357112 commented, the number is too large for int.
If you want your first version to work, add a "d" to tell Java to use a double.
Double d = 1000d*1*5000*500/(10000*24645.16239360158)
System.out.println(d);
You'd get:
10.143978603480635

Not sure how to use Math.pow()

I am new to Java. I am writing a small program to calculate the value of a number raised to a power, when I ran into a problem with negative numbers raised to a fractional exponent.
System.out.println(Math.pow(-8, 1/3f));
The output is NaN while I'm expecting -2?
What am I doing wrong? Are there any alternatives to calculate problems like this?
Any help appreciated.
This case is described in documentation .
If the first argument is finite and less than zero. <...>
if the second argument is finite and not an integer, then the result is NaN.
As far as I know there is no method in Java standard library to do it, so you have to implement it manually.

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

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

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.

Categories