LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Number of bits (field in general) required to store decimal number (https://www.linuxquestions.org/questions/programming-9/number-of-bits-field-in-general-required-to-store-decimal-number-4175524006/)

srinietrx 11-01-2014 02:00 PM

Number of bits (field in general) required to store decimal number
 
I am trying to write program as
if I give any decimal number as input n, it should give output
x = log2n i.e
log n/log2

Quote:

For example
n = 35;
x = log 35/log2
= 5.12
rounding off next number gives 6.
6 fields are required.
I can write program in this way.But if n value increases, it may lead to wrong result.

So Is there is any better way or algorithm to implement in program.
Guide me please.

NevemTeve 11-01-2014 02:11 PM

Code:

int bits= (int) floor (ln (num) / ln (2));
Edit: it's wrong, see below

srinietrx 11-01-2014 02:29 PM

yes.
Thank you for introducing new library function floor().But in my case I need to use ceil() as to round up value.
Actually I thought that this formula(log n/log 2) fails after certain number. But I will stick with this.

ntubski 11-01-2014 10:57 PM

Code:

int bits= (int) floor (ln (num) / ln (2)) + 1;
No?

Quote:

Originally Posted by srinietrx (Post 5262984)
Actually I thought that this formula(log n/log 2) fails after certain number. But I will stick with this.

It fails for 0.

NevemTeve 11-02-2014 01:39 AM

You are right, it is ceil what you need, my bad.

Code:

int bits= (int) ceil (ln (num) / ln (2));
Also you can calculate discrete logarithm:
Code:

for (ntmp= num, ndigits= 0; ntmp>0; ntmp /= 2) ++ndigits;

ntubski 11-02-2014 08:31 AM

Quote:

Originally Posted by NevemTeve (Post 5263189)
Code:

int bits= (int) ceil (ln (num) / ln (2));

I look forward to seeing you encode the number "1" into 0 bits (actually, I suppose you could do it if encoding 0 is not needed).

Quote:

Also you can calculate discrete logarithm:
Code:

for (ntmp= num, ndigits= 0; ntmp>0; ntmp /= 2) ++ndigits;

This isn't the discrete logarithm (but it is the number bits you'll need).

NevemTeve 11-02-2014 11:35 AM

Thank you for pointing out my faults.
Anyways, let's try something to encode positive integers with bitvectors. Well, every positive integer can be written in binary, and the first binary digit is always 'one'. Let's store every bits, except for the first one:
Code:

1 -    []
 2 -  [0]
 3 -  [1]
 4 -  [00]
 7 -  [11]
 4 -  [00]
 8 - [000]
15 - [111]

PS: Having re-read the Original Post, I've to admit I cannot tell what exactly the question is.

sundialsvcs 11-04-2014 07:13 AM

A few comments here:

(1) There are several well-known floating point number formats. All of them store an exponent and a mantissa. The actual number of "digits" represented internally is reflected in the mantissa, which is then raised to the power of the exponent. Probably the most-common format is the Extended floating-point type of the now-ubiquitous Intel® microprocessors.

(2) Each time you see a floating-point number, it has been converted to a printable (decimal) representation ... which, as it happens, is not exactly equal to its float-binary (internal) representation. Thus, "5.12" might be displayed as "5.1" or "5." and in any case it probably is not exactly-equal to "5.1200000...".

(3) Determination of "how many digits of the answer are significant" is really a basic engineering question, and would be even if you were using a slide-rule. Functions such as ln() are, after a certain point, just estimations. There's a certain, predictable amount of numeric error, and the subsequent series of operations that you perform will increase (never "decrease") that accumulated error.

(4) "Truly-decimal" floating point packages do exist ... which represent the numbers in question as individual digits and perform math exactly the way that you'd do it by hand. But these are typically used for accounting, not engineering applications, and I would not expect any of them to include transcendantal functions.


All times are GMT -5. The time now is 01:55 PM.