round number to the nearest power of 2..

Michele Verità's icon

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

Gary Lee Nelson's icon

Try this...

Max Patch
Copy patch and select New From Clipboard in Max.

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
www.timara.oberlin.edu/GaryLeeNelson

Gary Lee Nelson's icon
Michele Verità's icon

Thank you very much!

Mattijs's icon

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

Max Patch
Copy patch and select New From Clipboard in Max.

Best,
Mattijs

Gary Lee Nelson's icon

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
www.timara.oberlin.edu/GaryLeeNelson

Mattijs's icon

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
> www.timara.oberlin.edu/GaryLeeNelson
>
>
>
----------------------------------------------------

Steven Miller's icon

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
SFIFEM
Atrium Sound Space
OVOS

Gary Lee Nelson's icon

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
www.timara.oberlin.edu/GaryLeeNelson

Steven Miller's icon

> 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
For example 2^-2=0.25, 2^-3=0.125, etc.

The statement about input integers 0, 1 still stands.

----
Steven M. Miller

Home
SFIFEM
Atrium Sound Space
OVOS

Gary Lee Nelson's icon

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.

Max Patch
Copy patch and select New From Clipboard in Max.

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

Gary Lee Nelson's icon

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
> For example 2^-2=0.25, 2^-3=0.125, etc.
>
> The statement about input integers 0, 1 still stands.
>
>
> ----
> Steven M. Miller
>
> Home
> >
> SFIFEM
> >
> Atrium Sound Space
> >
> OVOS
>
> >
>
>
>
>
>
>

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

Steven Miller's icon

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
SFIFEM
Atrium Sound Space
OVOS

Gary Lee Nelson's icon
Gary Lee Nelson's icon
Steven Miller's icon

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
SFIFEM
Atrium Sound Space
OVOS

Michele Verità's icon

ahahahah!i love it too!

Mattijs's icon

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

Steven Miller's icon

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
SFIFEM
Atrium Sound Space
OVOS

Stefan Tiedje's icon
dlurk's icon

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.

Mattijs's icon

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.

Gary Lee Nelson's icon

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

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

Max Patch
Copy patch and select New From Clipboard in Max.

>

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