How to determine if one number is a power of another?

Started by
13 comments, last by neurokaotix 20 years, 1 month ago
quote:Original post by cowsarenotevil
Yes, becuause dividing a number by two will return TRUE if it''s divisible by two, and FALSE if not. Oh wait, it doesn''t, so it''s not.
No; because dividing a number by two will return an integer if it''s divisible by two, and a rational if it''s not.
Advertisement
please...don''t start an argument on even and odd numbers, which is ultimately what the problem was about. modulo is our friend.
Well, R2D22U2..
quote:Original post by Cedric
quote:Original post by cowsarenotevil
Yes, becuause dividing a number by two will return TRUE if it''s divisible by two, and FALSE if not. Oh wait, it doesn''t, so it''s not.
No; because dividing a number by two will return an integer if it''s divisible by two, and a rational if it''s not.


Which isn''t easy to check on a computer due to floating point inaccuracies. You''ll need to check against some epsilon and so forth. Modulo math is the way to go.
-YoshiXGXCX ''99
quote:Original post by YoshiN
quote:Original post by Cedric
quote:Original post by cowsarenotevil
Yes, becuause dividing a number by two will return TRUE if it''s divisible by two, and FALSE if not. Oh wait, it doesn''t, so it''s not.
No; because dividing a number by two will return an integer if it''s divisible by two, and a rational if it''s not.


Which isn''t easy to check on a computer due to floating point inaccuracies. You''ll need to check against some epsilon and so forth. Modulo math is the way to go.


int n = blah;
if(n/2*2 == n) cout<<"yay!";
I''m closing the thread because it has nothing to do with game development and the original poster now has the answer he requested.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

This topic is closed to new replies.

Advertisement