Power function with negative values in hive - hive

I am trying to use Power(a,b) function in hive query.
Whenever b is negative value, the power function results in incorrect value.
This is happening only if parameters in power function are negative. Please help.

You can easily calculate negative exponent. Calculate positive exponent and take the reciprocal:
power(a,-b) = 1/power(a,b)
See the explanation here: http://www.mathsisfun.com/algebra/negative-exponents.html

Related

How to handle very big numbers in snowflake?

I've a python program that goes over tables in a DB (not mine) and for each column from type number it performs some mathematical operations such as stdev. However, there are some columns with very numbers and when I'm trying to execute:
select STDDEV(big_col) from table1;
I'm getting the error:
Number out of representable range: type FIXED[SB16](38,0){not null}, value 3.67864e+38
Any idea how can I handle this one? It's ok for me just to ignore this values in this case but I don't want my query to fail.
Thanks,
Nir.
As #dnoeth mentioned in the comment section, casting the standard deviation as DOUBLE should fix the issue: STDDEV(CAST(big_col as DOUBLE)).
The OP asked how the resulting standard deviation seems to be significantly smaller than e+38 (which is the max number of digits that the NUMBER format can hold), then why do we need to cast this number as DOUBLE?
The reason for this lies in the standard deviation formula:
The first step in this process is to subtract the mean of the column from each individual value in that column. All those values are then squared. Now this square determines the 88 upper bound for the values that the NUMBER format needs to be able to handle before further arithmetic operations, like dividing by the number of records (N), and taking a square root reduce it down to the final answer. The final value of standard deviation that you get from this process could be significantly smaller than the sum of squares that's required to be calculated first.

Round SQL String Data to correct decimal place, then return string data without floating point errors

We are trying to implement a reporting system using software that queries our SQL database. Due to a variety of circumstances, we have a need to round data within the SQL queries. Our goal is to avoid floating point errors, unwanted trailing zeros, and complexity of nested functions (if possible).
The incoming data is always type nvarchar(...) and needs to remain in a string format, which is causing problems for us. Here is an example of what I mean (tested using w3schools.com):
SELECT
STR(235.415, 10, 2) AS StringValue1,
STR('235.415', 10, 2) AS StringValue2,
STR(ROUND(235.415, 2),10,2) AS RoundValue1,
STR(ROUND('235.415', 2),10,2) AS RoundValue2,
STR(CAST('235.415' As NUMERIC(8,2)),10,2) As CastValue1
And, the result:
I know that the issue is a conversion to floating point data type when handling strings. I think the last option, i.e. casting to numeric, is the answer to my issue. However, I can't tell if this output is correct because the CAST guarantees there will not be an error, or because I got lucky for this specific instance.
Is there any type of SQL round function (or combination of functions) that takes string input, outputs string data, and doesn't involve floating point arithmetic? -- Thanks in advance!
NUMERIC/DECIMAL and MONEY donĀ“t uses floating point arithmetic. The are in fact integers with a fixed comma.
Be aware that if you have large sums or do some calculations with these values, your rounding error can get pretty big, pretty fast. So it is advisable to take some moments to think about where you store a value with which precision and when you want to round.

Procedure for Arithmetic (Division) in tcl

Need to divide two numbers (can be floating) in tcl and check if the number is an exact multiple.
!($x % $y) doesn't work as the operand expects integers.
Many floating point numbers used on computers are just an approximation of the specified value. So expecting to be able to check if one value is an exact multiple of another value will likely lead to disappointment.
For example: expr {fmod(1, 0.1)} => 0.09999999999999995 because 0.1 cannot be represented exactly in binary floating point format.
I'm afraid you will have to reconsider your requirements.
See also https://en.wikipedia.org/wiki/Floating-point_arithmetic

How to identify records which have clusters or lumps in data?

I have a tableau table as follows:
This data can be visualized as follows:
I'd like to flag cases that have lumps/clusters. This would flag items B, C and D because there are spikes only in certain weeks of the 13 weeks. Items A and E would not be flagged as they mostly have a 'flat' profile.
How can I create such a flag in Tableau or SQL to isolate this kind of a case?
What I have tried so far?:
I've tried a logic where for each item I calculate the MAX and MEDIAN. Items that need to be flagged will have a larger (MAX - MEDIAN) value than items that have a fairly 'flat' profile.
Please let me know if there's a better way to create this flag.
Thanks!
Agree with the other commenters that this question could be answered in many different ways and you might need a PhD in Stats to come up with an ideal answer. However, given your basic requirements this might be the easiest/simplest solution you can implement.
Here is what I did to get here:
Create a parameter to define your "spike". If it is going to always be a fixed number you can hardcode this in your formulas. I called min "Min Spike Value".
Create a formula for the Median Values in each bucket. {fixed [Buckets]: MEDIAN([Values])} . (A, B, ... E = "Buckets"). This gives you one value for each letter/bucket that you can compare against.
Create a formula to calculate the difference of each number against the median. abs(sum([Values])-sum([Median Values])). We use the absolute value here because a spike can either be negative or positive (again, if you want to define it that way...). I called this "Spike to Current Value abs difference"
Create a calculated field that evaluates to a boolean to see if the current value is above the threshold for a spike. [Spike to Current Value abs difference] > min([Min Spike Value])
Setup your viz to use this boolean to highlight the spikes. The beauty of the parameter is you can change the value for what a spike should be and it will highlight accordingly. Above the value was 4, but if you change it to 8:

SSAS - calculated column with an If statment

I have a calculated column that basicly calculates an average time on page.
fix([Measures].[Time on Page] / ([Measures].[Pageviews] -[Measures].[Exits])) /3600)
the problem is that if Measures].[Time on Page] is 0 its messing up the Calculation due to a devide by 0 error. Is there a way to test for this, maybe add an If statment into the Calcualtion?
If you are using 2012 version, you can use the new divide function.
Here is a link describing how to use that. http://cwebbbi.wordpress.com/2013/07/26/new-mdx-divide-function/
Basically, Divide (, [,])
Alternatively you can use an IF statement to check if the divisior is 0.
But there are cases where we have near 0 numbers as well. For example if you have adjustment data to correct the actuals, you may end up actual value 5, and adjustment value -5 and as a result you expect 0. That may not happen in all the cases. Because of how numbers aggregated internally etc. So the number could become 0.00000001 instead of 0.
For such cases, you need to adjust your condition.