round number to the nearest power of 2..

Feb 25, 2007 at 1:23pm

round number to the nearest power of 2..

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

#30457
Feb 25, 2007 at 2:44pm

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

#97460
Feb 25, 2007 at 3:04pm

#97461
Feb 25, 2007 at 3:15pm

Thank you very much!

#97462
Feb 25, 2007 at 3:19pm

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

#97463
Feb 25, 2007 at 5:29pm

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
> –
> SmadSteck – http://www.smadsteck.nl
> Interactive audiovisual sampling soft- and hardware
>

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

#97464
Feb 25, 2007 at 6:55pm

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
> > –
> > SmadSteck – http://www.smadsteck.nl
> > Interactive audiovisual sampling soft- and hardware
> >
>
>
> Cheers
> Gary Lee Nelson
> Oberlin College
> http://www.timara.oberlin.edu/GaryLeeNelson
>
>
>
—————————————————-

#97465
Feb 25, 2007 at 7:28pm

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

Home < http://pubweb.csf.edu/~smill>
SFIFEM <
http://sfifem.csf.edu>
Atrium Sound Space <
http://atrium.csf.edu>
OVOS <
http://pubweb.csf.edu/~smill/ovos.html>

#97466
Feb 25, 2007 at 7:33pm


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

#97467
Feb 25, 2007 at 7:43pm

> 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>
SFIFEM <
http://sfifem.csf.edu>
Atrium Sound Space <
http://atrium.csf.edu>
OVOS <
http://pubweb.csf.edu/~smill/ovos.html>

#97468
Feb 25, 2007 at 7:48pm


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

#97469
Feb 25, 2007 at 8:01pm

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.

How about this addition to the patch I sent…get the sign of the input then
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

#97470
Feb 25, 2007 at 8:03pm

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 > >
>
>
>
>
>
>

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

#97471
Feb 25, 2007 at 8:17pm

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

Home < http://pubweb.csf.edu/~smill>
SFIFEM <
http://sfifem.csf.edu>
Atrium Sound Space <
http://atrium.csf.edu>
OVOS <
http://pubweb.csf.edu/~smill/ovos.html>

#97472
Feb 25, 2007 at 8:33pm

#97473
Feb 25, 2007 at 8:49pm

#97474
Feb 25, 2007 at 9:05pm

#97475
Feb 25, 2007 at 11:31pm

#97476
Feb 25, 2007 at 11:58pm

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

Home < http://pubweb.csf.edu/~smill>
SFIFEM <
http://sfifem.csf.edu>
Atrium Sound Space <
http://atrium.csf.edu>
OVOS <
http://pubweb.csf.edu/~smill/ovos.html>

#97477
Feb 26, 2007 at 12:29am

#97478
Feb 26, 2007 at 7:24am

ahahahah!i love it too!

#97479
Feb 26, 2007 at 9:14am

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

#97480
Feb 26, 2007 at 5:02pm

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

Home < http://pubweb.csf.edu/~smill>
SFIFEM <
http://sfifem.csf.edu>
Atrium Sound Space <
http://atrium.csf.edu>
OVOS <
http://pubweb.csf.edu/~smill/ovos.html>

#97481
Feb 26, 2007 at 5:06pm

#97482
Feb 26, 2007 at 5:36pm

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.

#97483
Feb 26, 2007 at 5:51pm

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.

#97484
Feb 26, 2007 at 5:56pm

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

#97485

You must be logged in to reply to this topic.