Ensure a variable is divisible by 4

G

geskerrett

I am sure this is a basic math issue, but is there a better way to
ensure an int variable is divisible by 4 than by doing the following;

x = 111
x = (x /4) * 4

Just seems a bit clunky to me.
 
K

Kay Schluehr

I am sure this is a basic math issue, but is there a better way to
ensure an int variable is divisible by 4 than by doing the following;

x = 111
x = (x /4) * 4

Just seems a bit clunky to me.

Division with rest:
3
 
L

Larry Bates

I am sure this is a basic math issue, but is there a better way to
ensure an int variable is divisible by 4 than by doing the following;

x = 111
x = (x /4) * 4

Just seems a bit clunky to me.
Use modulo operator '%'

if not x % 4:
#
# Arrive here if x is modulo 4 divisable
#

-Larry Bates
 
G

George Sakkis

I am sure this is a basic math issue, but is there a better way to
ensure an int variable is divisible by 4 than by doing the following;

x = 111
x = (x /4) * 4

Just seems a bit clunky to me.

if x % 4 == 0:
# x is divisible by 4


George
 
W

Will McGugan

I am sure this is a basic math issue, but is there a better way to
ensure an int variable is divisible by 4 than by doing the following;

x = 111
x = (x /4) * 4

Just seems a bit clunky to me.

Depends what you mean by 'make it divisable'. Do you want to check it is
divisible or do you want to make it divisible? And if you want to make
it divisible do you want to go to the next multiple of 4, or the previous?


Will McGugan
 
T

Tim Chase

I am sure this is a basic math issue, but is there a better way to
ensure an int variable is divisible by 4 than by doing the following;

x = 111
x = (x /4) * 4

Just seems a bit clunky to me.

You're right...you'll want to read up on the "modulo" operator:

if x % 4 <> 0:
print "Hey, x isn't divisible by 4"

http://docs.python.org/lib/typesnumeric.html

To do what you describe above, you can also use

x = x - (x % 4)

which isn't greatly better in the clunkiness department. In both
cases, non-divisible-by-4 numbers get bumped down (to the "left"
on the number line) in the event that it's not divisible by 4.

-tkc
 
N

Nick Craig-Wood

I am sure this is a basic math issue, but is there a better way to
ensure an int variable is divisible by 4 than by doing the following;

x = 111
x = (x /4) * 4

You should use // for future compatibility which is guaranteed to be
an integer division whereas / isn't (see "from __future__ import
division")

Eg

(x // 4) * 4

For the particular case of 4 being 2**2, you might consider

x & ~0x3

which is a common idiom.

If you want to round to the next largest 4 then add 3 first, eg

for x in range(0,12):
(x + 3) & ~0x3

Which prints 0,4,4,4,4,8,8,8,8,12...

You could also consider the funky

x>>2<<2
 
G

Grant Edwards

I am sure this is a basic math issue, but is there a better
way to ensure an int variable is divisible by 4

if x & 3:
print "not divisible by 4"

x &= ~3

print "it is now: x = %d"

If you want to round to nearest power of 4 rather than truncate:

x = (x+2) & ~3
 
P

Paul McGuire

I am sure this is a basic math issue, but is there a better way to
ensure an int variable is divisible by 4 than by doing the following;

x = 111
x = (x /4) * 4

Just seems a bit clunky to me.

All numbers are divisible by 4. (cf. Little Man Tate)

-- Paul
 
J

Jonathan Smith

if ( x % 4 ) == 0:
whatever # x is divisible by 4

modulus is your friend :)

-smithj
 
G

geskerrett

Nick said:
You should use // for future compatibility which is guaranteed to be
an integer division whereas / isn't (see "from __future__ import
division")

Eg

(x // 4) * 4

For the particular case of 4 being 2**2, you might consider

x & ~0x3

which is a common idiom.

Thanks for the tip about integer division and I will experiment with
your other suggestion.
 
M

MRAB

Jonathan said:
if ( x % 4 ) == 0:
whatever # x is divisible by 4

modulus is your friend :)

-smithj

<pendantic>
It's "modulo"; "modulus" is a different operation.
</pedantic>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top