Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

CEIL function behaviour?

Arijit KanrarNov 10 2015 — edited Nov 10 2015

Hi All,

I using Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production.

I have a case where given the number of bits available and the format of a number, I need to find the maximum number of digits required to represent the number.

For e.g. a 8 bit field if stores Hexadecimal formatted number then we need a max of 2 digits because the max the 8 bit field can store is FF hexadecimal.

For this I used this formula:

CEIL (:v_bits_reqd/LOG(

              2,

              CASE :v_number_format

                  WHEN 'BINARY' THEN 2

                  WHEN 'OCTAL' THEN 8

                  WHEN 'HEXADECIMAL' THEN 16

              END))

So the formula just takes the number of bits available and divides it by the number of bits required to represent each digit in that format (for e.g. 1 in binary, 3 in octal, 4 in hexadecimal etc).

Then the result is "rounded-up" using the CEIL function because if I need 2.5 digits it means I need 3 digits.

But when I run this for 8 bits and hexadecimal, the CEIL function should be getting CEIL(8/4) so CEIL(2) = 2.

But I get the result as 3.

To check I set up a test case and sure enough, the CEIL function was giving a result which was 1 more than the correct result when its input was a whole number and not a fraction.

This was my test case:

WITH bits

    AS (    SELECT LEVEL AS bit

              FROM DUAL

        CONNECT BY LEVEL <= 16),

    num_fmt

    AS (SELECT 'BINARY' AS number_format FROM DUAL

        UNION

        SELECT 'OCTAL' AS number_format FROM DUAL

        UNION

        SELECT 'HEXADECIMAL' AS number_format FROM DUAL)

  SELECT b.*,

        f.*,

        CEIL (

            (  b.bit

            / LOG (

                  2,

                  CASE f.number_format

                    WHEN 'BINARY' THEN 2

                    WHEN 'OCTAL' THEN 8

                    WHEN 'HEXADECIMAL' THEN 16

                  END)))

            AS LENGTH,

          b.bit

        / LOG (

              2,

              CASE f.number_format

                WHEN 'BINARY' THEN 2

                WHEN 'OCTAL' THEN 8

                WHEN 'HEXADECIMAL' THEN 16

              END)

            AS num_bits_decimal,

        LOG (

            2,

            CASE f.number_format

              WHEN 'BINARY' THEN 2

              WHEN 'OCTAL' THEN 8

              WHEN 'HEXADECIMAL' THEN 16

            END)

            AS bits_per_digit

    FROM bits b, num_fmt f

ORDER BY number_format, bit;


    

BITSNUMBER_FORMATLENGTHNUM_BITS_DECIMALBITS_PER_DIGIT
1BINARY111
2BINARY221
3BINARY331
4BINARY441
5BINARY551
6BINARY661
7BINARY771
8BINARY881
9BINARY991
10BINARY10101
11BINARY11111
12BINARY12121
13BINARY13131
14BINARY14141
15BINARY15151
16BINARY16161
1HEXADECIMAL10.254
2HEXADECIMAL10.54
3HEXADECIMAL10.754
4HEXADECIMAL214
5HEXADECIMAL21.254
6HEXADECIMAL21.54
7HEXADECIMAL21.754
8HEXADECIMAL324
9HEXADECIMAL32.254
10HEXADECIMAL32.54
11HEXADECIMAL32.754
12HEXADECIMAL434
13HEXADECIMAL43.254
14HEXADECIMAL43.54
15HEXADECIMAL43.754
16HEXADECIMAL544
1OCTAL10.3333333333
2OCTAL10.6666666673
3OCTAL113
4OCTAL21.3333333333
5OCTAL21.6666666673
6OCTAL323
7OCTAL32.3333333333
8OCTAL32.6666666673
9OCTAL433
10OCTAL43.3333333333
11OCTAL43.6666666673
12OCTAL543
13OCTAL54.3333333333
14OCTAL54.6666666673
15OCTAL653
16OCTAL65.3333333333

          

The LENGTH column is the result. As you can see the length is wrong when BITS/BITS_PER_DIGIT is a whole number but surprisingly this doesn't happen for BINARY where the division takes place with 1?

Any suggestions anyone?

Thanks!

Arijit

Message was edited by: Arijit Kanrar Added table of results to explain problem more?

This post has been answered by Frank Kulash on Nov 10 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 8 2015
Added on Nov 10 2015
5 comments
711 views