Solved

Powers of two

Posted on 2008-10-14
11
186 Views
Last Modified: 2012-05-05
hi,

  number = log(input)/log(2);

could anyone pls help me...to get the power of two of the input, i am doing this but i got incorrect answer for 8 ...

0
Comment
Question by:zizi21
  • 6
  • 4
11 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22717021
Can't you use the math class pow function?

pow(number, power);
0
 

Author Comment

by:zizi21
ID: 22717044
let say someone input 8,

i need to give the answer 3...
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22717063
ah so you do logs..

Unfortunately the problem is because the log function returns a double number, and double numbers can't be stored 100 percent accurately.

You will probably get some invalid result because of this.

Try using the "ceil, and floor" functions to help you get the correct answer.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22717114
You actually could just just try using sprintf

like this:

double result = <log calculation>;

here is an example of only printing only 1 decimal
printf("%8.1d" result);  <-- creates a entire width of 8, and only 1 decimal space.

You can read up on this stuff.

here is a link I found:
http://cermics.enpc.fr/~ts/C/FUNCTIONS/format.html
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22717116
correction:

printf("%8.1d" result);  
--> should be printf("%8.1d", result);
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:zizi21
ID: 22717132
thanks...
0
 

Author Comment

by:zizi21
ID: 22717136
it works with ceil..can i assume it is correct or do i need to check for floor ??
0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 500 total points
ID: 22717171
you can't assume.

There are a few problems. with the approach with using the ceil / floor.

1. What happens if you do a log / log and the actual answer should have a decimal...
Then when you use the ceil / floor to the nearest integer, you will obviously get the wrong answer.

I suggest that instead of using ceil/floor, you simply do the division, and only print out a certain precision of decimal places. You may want to add other tests, but I am unsure exactly how to approach with 1 single method that is the absolute best.

Usually, the programmer just rounds to a specific decimal place
0
 

Author Comment

by:zizi21
ID: 22717208
thanks

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22718415
>> to get the power of two of the input,

If the input will be an integer value, this is relatively easy, since you have binary operators.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22724260
I agree; I think I probably should have mentioned using bitwise operators, but I wasn't 100 percent clear on if he would be doing this approach only for powers of 2....

Have a look up on "Binary bitwise operators"

Operators: ">>" and "<<" are quite helpful
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now