How to do "Log(x,y)" in sql server 2005 - algorithm

I need to find out how many times to multiply by itself to reach a target value in sql server 2005.
In SQL 2008+ you can do this
SELECT LOG(8,2)
and the answer would be 3 because 2 x 2 x 2 = 8.
But in SQL 2005 the LOG function doesn't accept a base value.
I'm trying to avoid having to create a custom function to do this.
Thank everyone.

Use LOG(8) / LOG(2).
This is because LOG(A) / LOG(B) is equal to the logarithm of A in the base of B. The base of the LOG function doesn't matter (because changing bases is, as we've seen, just multiplying by a constant)

Related

Hive is removing decimals after cast?

In Hive I was performing some tests with operations and got a behaviour that I could not understand.
While doing that operation, Hive is returning below results with 17 decimals after the comma:
select 500/3260;
> 0.15337423312883436
But when I am triyng to cast it with decimal format, the result is:
select cast(500 as decimal(38,18)) / cast(3260 as decimal(38,18));
> 0.153374
I would like to have 18 decimals, but only 6 are displayed.
Could you please explain me why it is giving this result?
Thank you in advance for your help.
I think it's due to decimal division rules detailed here. The link is for SQL server, but apparently the behaviour shown here is the same.
The scale will be set to 6 if it's greater than 6 and if the integral part is greater than 32. In this case, both integral part and scale would be reduced and resulting type is decimal(38,6). Result might be rounded to 6 decimal places or the overflow error will be thrown if the integral part can't fit into 32 digits.
The SQL server implementation was referenced in this Hive document (page 3).

Constraint Satisfaction Problems with solvers VS Systematic Search

We have to solve a difficult problem where we need to check a lot of complex rules from multiple sources against a system in order to decide if the system satisfy those rules or how it should be changed to satisfy them.
We initially started using Constraint Satisfaction Problems algorithms (using Choco) to try to solve it but since the number of rules and variables would be smaller than anticipated, we are looking to build a list of all possibles configurations on a database and using multiple requests based on the rules to filter this list and find the solutions this way.
Is there limitations or disadvantages of doing a systematic search compared to using a CSP solver algorithms for a reasonable number of rules and variables? Will it impact performances significantly? Will it reduce the kind of constraints we can implement?
As examples :
You have to imagine it with a much bigger number of variables, much bigger domains of definition (but always discrete) and bigger number of rules (and some much more complex) but instead of describing the problem as :
x in (1,6,9)
y in (2,7)
z in (1,6)
y = x + 1
z = x if x < 5 OR z = y if x > 5
And giving it to a solver we would build a table :
X | Y | Z
1 2 1
6 2 1
9 2 1
1 7 1
6 7 1
9 7 1
1 2 6
6 2 6
9 2 6
1 7 6
6 7 6
9 7 6
And use queries like (this is just an example to help understand, actually we would use SPARQL against a semantic database) :
SELECT X, Y, Z WHERE Y = X + 1
INTERSECT
SELECT X, Y, Z WHERE (Z = X AND X < 5) OR (Z = Y AND X > 5)
CSP allows you to combine deterministic generation of values (through the rules) with heuristic search. The beauty happens when you customize both of those for your problem. The rules are just one part. Equally important is the choice of the search algorithm/generator. You can cull a lot of the search space.
While I cannot make predictions about the performance of your SQL solution, I must say that it strikes me as somewhat roundabout. One specific problem will happen if your rules change - you may find that you have to start over from scratch. Also, the RDBMS will fully generate all of the subqueries, which may explode.
I'd suggest to implement a working prototype with CSP, and one with SQL, for a simple subset of your requirements. You then will get a good feeling what works and what does not. Be sure to think about long term maintenance as well.
Full disclaimer: my last contact with CSP was decades ago in university as part of my master's (I implemented a CSP search engine not unlike choco, of course a bit more rudimentary, and researched a bit on that topic). But the field will certainly have evolved since then.

Visual Basic 2010, recognizing last two digits of a number

In visual basic 2010, I am trying to take a number that has been selected in a listbox and add the last two digits of the selected number, to a running total. The possible numbers that can be selected are all 4 digits.
(ex: number selected from listbox = 1712. I want to make c = c + 12) but i want to do this for any number selected.
if it helps, the numbers in the listbox range from 1700 - 2400.
Use the modulus operator, in VB it's Mod
c = c + (<numberFromListBox> Mod 100)
Try this...
total+=CDbl(Mid(Listbox.SelectedItems(0),3))

Direct way of converting BASE-14 to BASE-7

Given (3AC) in base-14. Convert it into BASE-7.
A simple approach is to convert first 3AC into BASE-10 & then to BASE-7 which results in 2105.
I was just wondering that does there exist any direct way of conversion from BASE-14 to BASe-7?
As others have said, there is no straightforward technique, because 14 is not a power of 7.
However, you don't need to go through base-10. One approach is to write routines that perform base-14 arithmetic (specifically addition and multiplication), and then use them to process each base-7 digit in turn: multiply it by the relevant power-of-7, and then add it to an accumulator.
I have found one approach.
There is no need to calculate for base 10 and then base 7. It can be done using this formula!
If a no X is represented in base 14 as
X = an a(n-1) a(n-2) .... a(0)
then in base 7 we can write it as
X=.....rqp
where
p=(2^0)a(0)%7;
q=((2^1)a(1) + p/7)%7
r=((2^2)a(2) + q/7)%7
..........
nth term=((2^n)a(n) + (n-1)th term/7)%7
(will go further because a no. in base 14 will require more digits in base 7).
The logic is simple, just based on properties of bases, and taking into account the fact that 7 is half of 14. Else it would have been a tedious task.
Eg. here it is given 3AC.
C =12;
so last digit is (2^0 * 12)%7 = 5
A=10
next digit is (2^1 * 10 + 12/7)%7 = (20+1)%7=21%7=0
next is 3;
next digit is (2^2 * 3 + 21/7)%7 = (12+3)%7=15%7=1
next is nothing(0);
next digit is (2^3 * 0 + 15/7)%7 = (0+2)%7=2%7=2
Hence, in base 7 number will be, 2105. This method may seem confusing and difficult, but with a little practice, it may come very handy in solving similar types of problems! Also, even if the number is very long, like 287AC23B362, we don't have to unnecessarily find base 10, which may consume atleast some time, and directly compute base 7!
No, there's not really an easy way to do as you wish because 14 is not a power of 7.
The only tricks that I know of for something like this (ex easily going from hex to binary) require that one base be a power of the other.
Link gives a reasonable clear answer. In short, it's a bit of a pain from the methods I know.

Oracle Bankers rule

Why Oracle is not using Bankers rule (the rounding method)?
Accurate decimal arithmatic is a large and complex subject.
Google 'mike colishaw decimal rounding' if you want to read the ahem Oracle on the subject.
Basically there are many rounding schemes which are possible:-
Round everthing down - the default in most languages including C as Oracle is written in C this is probably why they do this.
Round everything up - rarely seen but occasionally needs to be implemented because of obscure market and tax rules.
Basic Half Rounding - anything above .5 rounds up everything else rounds down.
Generous Half Rounding - anything below .5 rounds down everthing else rounds up.
Bankers Rounding - Even numbers follow the Basic Half Rounding rule, odd numbers the Generous Half Rounding rule. This is rarely seen in actual banks which prefer rounding up if the moneys coming thier way and rounding down when its going the clients way.
ORACLE NUMBER is actually a pretty good Decimal Arithmatic implementation and is accurate as far as it goes.
Oracle has implemented round half away from zero:
SQL> select round(22.5) from dual
2 /
ROUND(22.5)
-----------
23
SQL> select round(23.5) from dual
2 /
ROUND(23.5)
-----------
24
SQL> select round(-23.5) from dual
2 /
ROUND(-23.5)
------------
-24
SQL> select round(-22.5) from dual
2 /
ROUND(-22.5)
------------
-23
SQL>
Why don't they change it to Bankers' Rounding? Well, for most purposes round half away from zero is good enough. Plus there's that old fallback, changing it would likely break too much of the existing codebase - Oracle's own as well as all their customers.
Old thread, but someone may still need this. Oracle's binary floats and binary doubles follow the banker's rounding rule when rounding to a whole number. So you can use that. It's ugly but it works:
given : price = 2.445
SQL> select round(to_binary_float(price * 100)) / 100 as price_rounded from dual;
price_rounded
-------------
2.44
given : price = 2.435
SQL> select round(to_binary_float(price * 100)) / 100 as price_rounded from dual;
price_rounded
-------------
2.44
The multiply and divide by 100 are necessary in this example. I haven't been able to figure out the specifics of the behavior, but select round(to_binary_float(price), 2) for some decimal, price, does not seem to consistently round up or down by the same rules. I have found, however, that rounding to a whole number consistently gives me what I need.
You can always implement your own function for banker's rounding as described here.
Banker's rounding round's 0.5 to 0: it round's towards even numbers.

Resources