Showing posts with label base. Show all posts
Showing posts with label base. Show all posts

Thursday, July 26, 2012

Logarithms


This example was tested on Oracle 11.2. You can display logarithms as follows. Select log(a,b) from dual where a is the base and b is the number you want the logarithm of:

SQL> select log(2,8) "Log base 2 of 8" from dual
  2  /

Log base 2 of 8
---------------
              3

SQL> select log(9,3) "Log base 9 of 3" from dual
  2  /

Log base 9 of 3
---------------
             .5

SQL> select log(1000000,100)
  2  "Log base 1000000 of 100" from dual
  3  /

Log base 1000000 of 100
-----------------------
             .333333333

SQL>

The base must be a positive number > 1:

SQL> select log(0,1) "Log base 0 of 1" from dual
  2  /
select log(0,1) "Log base 0 of 1" from dual
           *
ERROR at line 1:
ORA-01428: argument '0' is out of range

SQL>

The number must be positive:

SQL> select log(5,-3) "Log base 5 of -3" from dual
  2  /
select log(5,-3) "Log base 5 of -3" from dual
             *
ERROR at line 1:
ORA-01428: argument '-3' is out of range 

SQL>

To multiply 2 numbers, take the logarithm of each and add them together. Then raise the base to the power of the sum to get the answer:

SQL> select power(3,log(3,6)+log(3,7))
  2  "Should be 42" from dual
  3  /

Should be 42
------------
          42

SQL>