Discussion:
check if n is a power of 2
(too old to reply)
Cliff
2006-01-30 14:16:17 UTC
Permalink
Hi,
I have a function that accepts an input n from a user.I want to force
the user to supply a number that is always a power of 2,i.e one of
2,4,8,16,....
This is what i have as a requirement for the number;

n = power(2,x)
therefore
x = log10(n)/log10(2)
All numbers{n} that give whole numbers i.e 1,2,3 etc when put into
the above equation satisfy my requirement.Now how do I ask Matlab to
check if a number is a whole number?

I thought isinteger(x) would work but later realise it want meant for
such use.

I want to make sure if say x=1.234 i reject the given n unless if n
gives x={whole number}

Thanks
Jos
2006-01-30 14:32:58 UTC
Permalink
Post by Cliff
Hi,
I have a function that accepts an input n from a user.I want to force
the user to supply a number that is always a power of 2,i.e one of
2,4,8,16,....
This is what i have as a requirement for the number;
n = power(2,x)
therefore
x = log10(n)/log10(2)
All numbers{n} that give whole numbers i.e 1,2,3 etc when put into
the above equation satisfy my requirement.Now how do I ask Matlab to
check if a number is a whole number?
I thought isinteger(x) would work but later realise it want meant for
such use.
I want to make sure if say x=1.234 i reject the given n unless if n
gives x={whole number}
Thanks
why not "isinteger(log2(n))"?

hth
Jos
Theophanes Raptis
2016-08-16 15:28:03 UTC
Permalink
Post by Jos
Post by Cliff
Hi,
I have a function that accepts an input n from a user.I want to force
the user to supply a number that is always a power of 2,i.e one of
2,4,8,16,....
This is what i have as a requirement for the number;
n = power(2,x)
therefore
x = log10(n)/log10(2)
All numbers{n} that give whole numbers i.e 1,2,3 etc when put into
the above equation satisfy my requirement.Now how do I ask Matlab to
check if a number is a whole number?
I thought isinteger(x) would work but later realise it want meant for
such use.
I want to make sure if say x=1.234 i reject the given n unless if n
gives x={whole number}
Thanks
why not "isinteger(log2(n))"?
hth
Jos
dpb
2016-08-16 17:58:04 UTC
Permalink
Post by Jos
Post by Cliff
the user to supply a number that is always a power of 2,i.e one of
2,4,8,16,....
...
Post by Jos
why not "isinteger(log2(n))"?
'Cuz ISINTEGER only checks the _class_ (which will still be double), not
the _value_ of the argument.

Several ways, one is

isPow2=(fix(log2(n))==n);



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Steve Amphlett
2006-01-30 14:35:42 UTC
Permalink
Post by Cliff
Hi,
I have a function that accepts an input n from a user.I want to force
the user to supply a number that is always a power of 2,i.e one of
2,4,8,16,....
This is what i have as a requirement for the number;
n = power(2,x)
therefore
x = log10(n)/log10(2)
All numbers{n} that give whole numbers i.e 1,2,3 etc when put into
the above equation satisfy my requirement.Now how do I ask Matlab to
check if a number is a whole number?
I thought isinteger(x) would work but later realise it want meant for
such use.
I want to make sure if say x=1.234 i reject the given n unless if n
gives x={whole number}
Why not take a hint from <nextpow2>? ML has a built-in
<log2> function, which probably (based on some recent TMW posts
here) checks for exact powers of two to give exact integer return
values where possible. So...

n=1024;
floor(log2(n))==log2(n)

ans =

1

Ta da!
Steve Eddins
2006-01-30 14:35:11 UTC
Permalink
----------------------------------------
Post by Cliff
Hi,
I have a function that accepts an input n from a user.I want to force
the user to supply a number that is always a power of 2,i.e one of
2,4,8,16,....
This is what i have as a requirement for the number;
n = power(2,x)
therefore
x = log10(n)/log10(2)
All numbers{n} that give whole numbers i.e 1,2,3 etc when put into
the above equation satisfy my requirement.Now how do I ask Matlab to
check if a number is a whole number?
I suggest that you check n directly using the two-output form of log2:

[f,e] = log2(n);

If f is 0.5, then n is a power of 2.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/log2.html
--
Steve Eddins
http://blogs.mathworks.com/steve
Theophanes Raptis
2016-08-16 15:25:03 UTC
Permalink
Post by Cliff
Hi,
I have a function that accepts an input n from a user.I want to force
the user to supply a number that is always a power of 2,i.e one of
2,4,8,16,....
This is what i have as a requirement for the number;
n = power(2,x)
therefore
x = log10(n)/log10(2)
All numbers{n} that give whole numbers i.e 1,2,3 etc when put into
the above equation satisfy my requirement.Now how do I ask Matlab to
check if a number is a whole number?
I thought isinteger(x) would work but later realise it want meant for
such use.
I want to make sure if say x=1.234 i reject the given n unless if n
gives x={whole number}
Thanks
Loading...