Forums > MaxMSP

### round number to the nearest power of 2..

Feb 25 2007 | 1:23 pm

hi all..actually i can’t figure out how i can do this: i need a way to round a number to the nearest power of 2
3 to 4
6 to 8
25 to 32
etc..
thanks
mic

Feb 25 2007 | 2:44 pm

Try this…

#P window setfont "Sans Serif" 9.;
#P flonum 44 256 91 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window linecount 1;
#P newex 44 220 192 196617 expr pow(2\,int((log(\$f1)/log(2.))+.5));
#P newex 246 270 27 196617 int;
#P newex 247 245 27 196617 + 0.5;
#P flonum 245 325 91 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 247 218 27 196617 / 1.;
#P newex 245 297 92 196617 expr pow(2\,\$f1);
#P flonum 361 166 38 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 291 167 60 196617 loadmess 2;
#P flonum 297 224 91 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 292 189 21 196617 log;
#P flonum 244 115 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 247 191 21 196617 log;
#P connect 2 0 7 1;
#P connect 2 0 3 0;
#P connect 1 0 11 0;
#P connect 1 0 0 0;
#P connect 11 0 12 0;
#P connect 7 0 9 0;
#P connect 0 0 7 0;
#P connect 10 0 6 0;
#P connect 9 0 10 0;
#P connect 6 0 8 0;
#P connect 5 0 2 0;
#P connect 4 0 2 0;
#P window clipboard copycount 13;

On 2/25/07 8:23 AM, "mic" wrote:

>
> hi all..actually i can’t figure out how i can do this: i need a way to round a
> number to the nearest power of 2
> 3 to 4
> 6 to 8
> 25 to 32
> etc..
> thanks
> mic
>

Cheers
Gary Lee Nelson
Oberlin College
http://www.timara.oberlin.edu/GaryLeeNelson

Feb 25 2007 | 3:04 pm

Feb 25 2007 | 3:15 pm

Thank you very much!

Feb 25 2007 | 3:19 pm

And this is my max-interpretation of the next-highest power of two

#P newex 15 174 27 9109513 + 1;
#P number 15 237 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 15 195 27 9109513 t 1 i;
#P newex 15 153 27 9109513 i;
#P newex 15 132 28 9109513 sel 1;
#P newex 15 90 27 9109513 i;
#P newex 15 48 27 9109513 t b i;
#P newex 15 69 40 9109513 uzi 16;
#P newex 15 111 27 9109513 >> 1;
#P newex 15 216 27 9109513 < <;
#P number 15 31 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
#P connect 3 2 2 1;
#P connect 3 2 7 1;
#P connect 10 0 8 0;
#P connect 7 0 10 0;
#P connect 4 1 5 1;
#P connect 3 0 5 0;
#P connect 4 0 3 0;
#P connect 5 0 2 0;
#P connect 2 0 6 0;
#P connect 0 0 4 0;
#P connect 6 0 7 0;
#P connect 8 1 1 1;
#P connect 8 0 1 0;
#P connect 1 0 9 0;

Best,
Mattijs

Feb 25 2007 | 5:29 pm

This doesn’t work for floats.

On 2/25/07 10:19 AM, "Mattijs Kneppers" wrote:

>
> And this is my max-interpretation of the next-highest power of two
>
> #P newex 15 174 27 9109513 + 1;
> #P number 15 237 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 15 195 27 9109513 t 1 i;
> #P newex 15 153 27 9109513 i;
> #P newex 15 132 28 9109513 sel 1;
> #P newex 15 90 27 9109513 i;
> #P newex 15 48 27 9109513 t b i;
> #P newex 15 69 40 9109513 uzi 16;
> #P newex 15 111 27 9109513 >> 1;
> #P newex 15 216 27 9109513 < <;
> #P number 15 31 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> #P connect 3 2 2 1;
> #P connect 3 2 7 1;
> #P connect 10 0 8 0;
> #P connect 7 0 10 0;
> #P connect 4 1 5 1;
> #P connect 3 0 5 0;
> #P connect 4 0 3 0;
> #P connect 5 0 2 0;
> #P connect 2 0 6 0;
> #P connect 0 0 4 0;
> #P connect 6 0 7 0;
> #P connect 8 1 1 1;
> #P connect 8 0 1 0;
> #P connect 1 0 9 0;
>
>
>
> Best,
> Mattijs
> —
> Interactive audiovisual sampling soft- and hardware
>

Cheers
Gary Lee Nelson
Oberlin College
http://www.timara.oberlin.edu/GaryLeeNelson

Feb 25 2007 | 6:55 pm

Quote: Gary Lee Nelson wrote on Sun, 25 February 2007 18:29
—————————————————-
> This doesn’t work for floats.

Um, then I guess you’ll have to round the float to an int first. Shouldn’t change the outcome, right?

This one probably costs less cpu cycles compared to the logs. But it doesn’t round, it finds the next-highest power of two.

>
>
> On 2/25/07 10:19 AM, "Mattijs Kneppers" wrote:
>
> >
> > And this is my max-interpretation of the next-highest power of two
> >
> > #P newex 15 174 27 9109513 + 1;
> > #P number 15 237 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> > #P newex 15 195 27 9109513 t 1 i;
> > #P newex 15 153 27 9109513 i;
> > #P newex 15 132 28 9109513 sel 1;
> > #P newex 15 90 27 9109513 i;
> > #P newex 15 48 27 9109513 t b i;
> > #P newex 15 69 40 9109513 uzi 16;
> > #P newex 15 111 27 9109513 >> 1;
> > #P newex 15 216 27 9109513 < <;
> > #P number 15 31 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> > #P connect 3 2 2 1;
> > #P connect 3 2 7 1;
> > #P connect 10 0 8 0;
> > #P connect 7 0 10 0;
> > #P connect 4 1 5 1;
> > #P connect 3 0 5 0;
> > #P connect 4 0 3 0;
> > #P connect 5 0 2 0;
> > #P connect 2 0 6 0;
> > #P connect 0 0 4 0;
> > #P connect 6 0 7 0;
> > #P connect 8 1 1 1;
> > #P connect 8 0 1 0;
> > #P connect 1 0 9 0;
> >
> >
> >
> > Best,
> > Mattijs
> > —
> > Interactive audiovisual sampling soft- and hardware
> >
>
>
> Cheers
> Gary Lee Nelson
> Oberlin College
> http://www.timara.oberlin.edu/GaryLeeNelson
>
>
>
—————————————————-

Feb 25 2007 | 7:28 pm

This doesn’t work for negative integers, nor does it return the
correct answer for input integers 0, 1.
I believe the correct outputs should be 1, 2 respectively, but it
returns 0, 0.

On Feb 25, 2007, at 8:19 AM, Mattijs Kneppers wrote:

>
> And this is my max-interpretation of the next-highest power of two
>
> #P newex 15 174 27 9109513 + 1;
> #P number 15 237 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> #P newex 15 195 27 9109513 t 1 i;
> #P newex 15 153 27 9109513 i;
> #P newex 15 132 28 9109513 sel 1;
> #P newex 15 90 27 9109513 i;
> #P newex 15 48 27 9109513 t b i;
> #P newex 15 69 40 9109513 uzi 16;
> #P newex 15 111 27 9109513 >> 1;
> #P newex 15 216 27 9109513 < <;
> #P number 15 31 35 9 0 0 0 139 0 0 0 221 221 221 222 222 222 0 0 0;
> #P connect 3 2 2 1;
> #P connect 3 2 7 1;
> #P connect 10 0 8 0;
> #P connect 7 0 10 0;
> #P connect 4 1 5 1;
> #P connect 3 0 5 0;
> #P connect 4 0 3 0;
> #P connect 5 0 2 0;
> #P connect 2 0 6 0;
> #P connect 0 0 4 0;
> #P connect 6 0 7 0;
> #P connect 8 1 1 1;
> #P connect 8 0 1 0;
> #P connect 1 0 9 0;
>
>
>

—-
Steven M. Miller

Feb 25 2007 | 7:33 pm

On 2/25/07 1:55 PM, "Mattijs Kneppers" wrote:

> Um, then I guess you’ll have to round the float to an int first. Shouldn’t
> change the outcome, right?

Yup. Result will be different.

>
> This one probably costs less cpu cycles compared to the logs. But it doesn’t
> round, it finds the next-highest power of two.

If CPU is an issue, computer a function with the desired number and scale of
values and store them in a buffer~ with peek~ and poke~. Same issue as
storing a sin wave for synthesis rather than computing each sample.

Cheers
Gary Lee Nelson
Oberlin College
http://www.timara.oberlin.edu/GaryLeeNelson

Feb 25 2007 | 7:43 pm

> From: Steven Miller
> Date: February 25, 2007 12:28:16 PM MST
> Subject: Re: [maxmsp] Re: round number to the nearest power of 2..
>
> This doesn’t work for negative integers, nor does it return the
> correct answer for input integers 0, 1.
> I believe the correct outputs should be 1, 2 respectively, but it
> returns 0, 0.
>

Sorry, I should have said "this doesn’t work for negative powers of
two, i.e. floats, where 0< x<1"
For example 2^-2=0.25, 2^-3=0.125, etc.

The statement about input integers 0, 1 still stands.

—-
Steven M. Miller

Feb 25 2007 | 7:48 pm

On 2/25/07 1:55 PM, "Mattijs Kneppers" wrote:

> Um, then I guess you’ll have to round the float to an int first. Shouldn’t
> change the outcome, right?

Yup. Result will be different.

>
> This one probably costs less cpu cycles compared to the logs. But it doesn’t
> round, it finds the next-highest power of two.

If CPU is an issue, computer a function with the desired number and scale of
values and store them in a buffer~ with peek~ and poke~. Same issue as
storing a sin wave for synthesis rather than computing each sample.

Cheers
Gary Lee Nelson
Oberlin College
http://www.timara.oberlin.edu/GaryLeeNelson

Feb 25 2007 | 8:01 pm

On 2/25/07 2:28 PM, "Steven Miller" wrote:

> This doesn’t work for negative integers

I think I may be in over my head mathematically but are any negative
integers powers of two? Isn’t that what the imaginary part of a complex
number is about? Very small positive floats can be very large negative
powers of two.

pass abs value to the computation then multiply the result by the sign.

#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 44 261 29 196617 * 1.;
#P newex 43 191 40 196617 abs 0.;
#N vpatcher 10 59 393 323;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 241 125 85 196617 if \$f1>0.0 then 1;
#P newex 144 125 90 196617 if \$f1<0.0 then -1;
#P newex 44 125 92 196617 if \$f1==0.0 then 0;
#P inlet 97 71 15 0;
#P outlet 87 194 15 0;
#P connect 1 0 2 0;
#P connect 4 0 0 0;
#P connect 2 0 0 0;
#P connect 3 0 0 0;
#P connect 1 0 3 0;
#P connect 1 0 4 0;
#P pop;
#P newobj 261 219 36 196617 p sign;
#P flonum 44 290 91 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 44 220 192 196617 expr pow(2\,int((log(\$f1)/log(2.))+.5));
#P flonum 44 157 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P connect 0 0 4 0;
#P connect 0 0 3 0;
#P connect 1 0 5 0;
#P connect 3 0 5 1;
#P connect 5 0 2 0;
#P connect 4 0 1 0;
#P window clipboard copycount 6;

Cheers
Gary Lee Nelson
Oberlin College
http://www.timara.oberlin.edu/GaryLeeNelson

Feb 25 2007 | 8:03 pm

Zero cannot be a power of anything.

On 2/25/07 2:43 PM, "Steven Miller" wrote:

>
>
>> From: Steven Miller
>> Date: February 25, 2007 12:28:16 PM MST
>> Subject: Re: [maxmsp] Re: round number to the nearest power of 2..
>>
>>
>> This doesn’t work for negative integers, nor does it return the correct
>> answer for input integers 0, 1.
>> I believe the correct outputs should be 1, 2 respectively, but it returns 0,
>> 0.
>>
>
> Sorry, I should have said "this doesn’t work for negative powers of two, i.e.
> floats, where 0< x<1"
> For example 2^-2=0.25, 2^-3=0.125, etc.
>
> The statement about input integers 0, 1 still stands.
>
>
> —-
> Steven M. Miller
>
> Home < http://pubweb.csf.edu/~smill < http://pubweb.csf.edu/~smill>
> <
http://pubweb.csf.edu/~smill > > < http://sfifem.csf.edu>
> SFIFEM <
http://sfifem.csf.edu < http://sfifem.csf.edu> < http://sfifem.csf.edu >
> >
> Atrium Sound Space <
http://atrium.csf.edu < http://atrium.csf.edu>
> <
http://atrium.csf.edu > >
> OVOS <
http://pubweb.csf.edu/~smill/ovos.html
> <
http://pubweb.csf.edu/~smill/ovos.html>
> <
http://pubweb.csf.edu/~smill/ovos.html > >
>
>
>
>
>
>

Feb 25 2007 | 8:17 pm

On Feb 25, 2007, at 1:03 PM, Gary Lee Nelson wrote:

> Zero cannot be a power of anything.

Yes, but we’re talking about ‘the next higher power of two’, not
powers of two directly.
The next higher power of two above zero is 1 (2^0).

—-
Steven M. Miller

Feb 25 2007 | 8:33 pm

Feb 25 2007 | 8:49 pm

Feb 25 2007 | 9:05 pm

Feb 25 2007 | 11:31 pm

Feb 25 2007 | 11:58 pm

LMAO!!

Too funny…you get a beer on me, Mattjis!

On Feb 25, 2007, at 4:31 PM, Mattijs Kneppers wrote:

>
> Michele: ".. so I’m still wondering how to round a number to the
> nearest power of two.."
>
> – Michele’s girlfriend: "oh no, please"
>
> etc., etc.
>

—-
Steven M. Miller

Feb 26 2007 | 12:29 am

Feb 26 2007 | 7:24 am

ahahahah!i love it too!

Feb 26 2007 | 9:14 am

Quote: smill wrote on Mon, 26 February 2007 00:58
—————————————————-
> LMAO!!
>
> Too funny…you get a beer on me, Mattjis!
>

:) Guess it’s time for cycling to add a new table in their database that keeps track of drinks ppl promised each other. Although I can’t come to collect it if I don’t know your location!

Cheers,
Mattijs

Feb 26 2007 | 5:02 pm

When I come to Amsterdam to collect the one you owe me, I’ll make
good on the one I owe you.
;)
On Feb 26, 2007, at 2:14 AM, Mattijs Kneppers wrote:

>
> Quote: smill wrote on Mon, 26 February 2007 00:58
> —————————————————-
>> LMAO!!
>>
>> Too funny…you get a beer on me, Mattjis!
>>
>
> :) Guess it’s time for cycling to add a new table in their database
> that keeps track of drinks ppl promised each other. Although I
> can’t come to collect it if I don’t know your location!
>

—-
Steven M. Miller

Feb 26 2007 | 5:06 pm

Feb 26 2007 | 5:36 pm

Mattijs Kneppers wrote:
> :) Guess it’s time for cycling to add a new table in their database that keeps track of drinks ppl promised each other.

maxobjects.com could add a few extra tables to seat the buyers and their
drinks with the, ah, objects of their affection (and perhaps their
preferences and locale).

Peter Nyboer’s recipes should be included, of course.

Feb 26 2007 | 5:51 pm

Quote: smill wrote on Mon, 26 February 2007 18:02
—————————————————-
> When I come to Amsterdam to collect the one you owe me, I’ll make
> good on the one I owe you.
> ;)

That’s a deal.

Feb 26 2007 | 5:56 pm

Yeah, that needed fixing. It was one of the first object I wrote.

On 2/26/07 12:06 PM, "Stefan Tiedje" wrote:

> #P outlet 39 144 15 0;
> #P window setfont "Sans Serif" 9.;
> #P window linecount 1;
> #P newex 72 60 31 196617 == 0.;
> #P inlet 39 35 15 0;
> #P newex 39 123 43 196617 +;
> #P newex 39 102 31 196617 – 1;
> #P newex 39 81 31 196617 < < 1;
> #P newex 39 60 31 196617 > 0.;
> #P connect 5 0 3 1;
> #P connect 4 0 0 0;
> #P fasten 4 0 5 0 44 55 77 55;
> #P connect 3 0 6 0;
> #P connect 2 0 3 0;
> #P connect 1 0 2 0;
> #P connect 0 0 1 0;
> #P window clipboard copycount 7;

Cheers
Gary Lee Nelson
Oberlin College
http://www.timara.oberlin.edu/GaryLeeNelson

Viewing 27 posts - 1 through 27 (of 27 total)

Forums > MaxMSP