Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Powers of two

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
zizi21
Asked:
zizi21
  • 6
  • 4
1 Solution
 
RishadanPortCommented:
Can't you use the math class pow function?

pow(number, power);
0
 
zizi21Author Commented:
let say someone input 8,

i need to give the answer 3...
0
 
RishadanPortCommented:
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
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
RishadanPortCommented:
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
 
RishadanPortCommented:
correction:

printf("%8.1d" result);  
--> should be printf("%8.1d", result);
0
 
zizi21Author Commented:
thanks...
0
 
zizi21Author Commented:
it works with ceil..can i assume it is correct or do i need to check for floor ??
0
 
RishadanPortCommented:
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
 
zizi21Author Commented:
thanks

0
 
Infinity08Commented:
>> 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
 
RishadanPortCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now