[patch] irrMath.h "isPowOf2"

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

[patch] irrMath.h "isPowOf2"

Post by REDDemon »

Irrlicht is missing a method for check if a number is a power of 2.

Code: Select all

 
        /** returns if "a" is a power of 2. Behaviour is undefined for floats,doubles and bools*/
        template <class T>
        inline bool isPowOf2(T a)
        {
            return ((a-1) & a) == 0; // 01000 - 00001 = 00111 ... 00111 & 01000 = 0
        }
 
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [patch] irrMath.h "isPowOf2"

Post by CuteAlien »

Hm, speed or exactness... to handle zero correct it should be:

Code: Select all

a && ((a-1) & a) == 0;
or (imho less confusing)

Code: Select all

return a && !((a-1) & a);
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: [patch] irrMath.h "isPowOf2"

Post by hendu »

I thought 0 is a valid pow of 2?
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: [patch] irrMath.h "isPowOf2"

Post by REDDemon »

REDDemon wrote:Irrlicht is missing a method for check if a number is a power of 2.

Code: Select all

 
        /** returns if "a" is a power of 2. Behaviour is undefined for floats,doubles and bools*/
        template <class T>
        inline bool isPowOf2(T a)
        {
            return ((a-1) & a) == 0; // 01000 - 00001 = 00111 ... 00111 & 01000 = 0
        }
 
yes that class threat both 0 and 1 as power of 2. because in integers number dominion:

2^-inf

is still 0 and a so 0 is a power of 2 (and a power of any other number) and i think is correct. If you prefer to put into irrlicht that 0 is not a power of 2 just document it :)
but AFAIK with integers is correct that 0 is a power of any number to -inf.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [patch] irrMath.h "isPowOf2"

Post by CuteAlien »

I would say 0 is not a valid power of 2 as negative infinite isn't an integer number, but I guess as long as it's documented what it results in it probably doesn't matter anyway for the cases in which it will be used :-)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: [patch] irrMath.h "isPowOf2"

Post by hybrid »

Isn't this just defined on unsigned anyway? So why bother with the template when we can simply add a version taking u32 and avoiding all those cases where a user implements an undefined behavior?
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: [patch] irrMath.h "isPowOf2"

Post by REDDemon »

:) yup good Idea.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
gerdb
Posts: 194
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

Re: [patch] irrMath.h "isPowOf2"

Post by gerdb »

hi, i guess a feature request for nextPowerOf2(u32 value) and prevPowerOf2(u32 value) will be advised too.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: [patch] irrMath.h "isPowOf2"

Post by REDDemon »

maybe you mean.. "nearestUpperPOT2 or "nearestLowerPOT2"?
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply