LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2014, 02:00 PM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 101

Rep: Reputation: Disabled
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.

Last edited by srinietrx; 11-01-2014 at 02:02 PM.
 
Old 11-01-2014, 02:11 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

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

Last edited by NevemTeve; 11-02-2014 at 01:40 AM.
 
Old 11-01-2014, 02:29 PM   #3
srinietrx
Member
 
Registered: May 2013
Posts: 101

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-01-2014, 10:57 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
int bits= (int) floor (ln (num) / ln (2)) + 1;
No?

Quote:
Originally Posted by srinietrx View Post
Actually I thought that this formula(log n/log 2) fails after certain number. But I will stick with this.
It fails for 0.
 
Old 11-02-2014, 01:39 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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;

Last edited by NevemTeve; 11-02-2014 at 01:54 AM.
 
Old 11-02-2014, 08:31 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by NevemTeve View Post
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).
 
1 members found this post helpful.
Old 11-02-2014, 11:35 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.

Last edited by NevemTeve; 11-02-2014 at 11:37 AM.
 
Old 11-04-2014, 07:13 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,633
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to compare a decimal variable with a number? massy Programming 2 03-03-2014 05:43 AM
How to find the first decimal number in a string ? 5883 Programming 3 08-08-2013 06:48 PM
convert number (not hex) into Decimal number drManhattan Programming 10 10-15-2011 08:53 PM
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 02:48 AM
i need to split a decimal number in such a way that each unit is between 1 and 26 anurupr Programming 8 03-07-2010 10:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration