Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Praveen.

Asked: December 02, 2016 - 3:02 pm UTC

Last updated: December 04, 2016 - 1:19 pm UTC

Version: 11.2

Viewed 1000+ times

You Asked

Hi Chris&Connor..Good Morning!
You both are doing good job.

My question is: The minimum height of a Red-Black tree H = FLOOR(LOG(2,N)), where N is nodes. Ideally, when N=4 then H=2 and when N=8 then H=3 and so on as per the formula. But, the LOG(2,4) = 1.99999999...98 very very close to 2 but not 2, so I modified my formula something like FLOOR(round(LOG(2,N),30)) and it's working fine.
I am just wondering why LOG(2,8) is not exactly 3 in Oracle.

and Connor said...

Its nothing special with LOG, just more the reality of floating point arithmetic in Oracle (and just about anything else that uses floating point arithmetic).

eg

SQL> col x format 999.9999999999999999999999999999999999
SQL> select 3*(1/3) x from dual;

                                      X
---------------------------------------
   1.0000000000000000000000000000000000

SQL> col x format 999.999999999999999999999999999999999999999999999999999999
SQL> select 3*(1/3) x from dual;

                                                          X
-----------------------------------------------------------
    .999999999999999999999999999999999999999900000000000000




Is this answer out of date? If it is, please let us know via a Comment