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. 15 11363
geskerr...@hotmail.com schrieb:
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:
>>x % 4
3 ge********@hotmail.com wrote:
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 ge********@hotmail.com wrote:
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
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 ge********@hotmail.com wrote:
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
-- http://www.willmcgugan.com ge********@hotmail.com <ge********@hotmail.comwrote:
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
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
On 2006-12-04, ge********@hotmail.com <ge********@hotmail.comwrote:
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
--
Grant Edwards grante Yow! An INK-LING? Sure --
at TAKE one!! Did you BUY any
visi.com COMMUNIST UNIFORMS??
<ge********@hotmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>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
<ge********@hotmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>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:
whatever # x is divisible by 4
modulus is your friend :)
-smithj
Nick Craig-Wood wrote:
ge********@hotmail.com <ge********@hotmail.comwrote:
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.
Thanks for the tip about integer division and I will experiment with
your other suggestion. ge********@hotmail.com skrev:
Nick Craig-Wood wrote:
>ge********@hotmail.com <ge********@hotmail.comwrote:
>> 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
X *= 4
;-)
--
hilsen/regards Max M, Denmark http://www.mxm.dk/
IT's Mad Science
Max M <ma**@mxm.dkwrites: ge********@hotmail.com skrev:
>Nick Craig-Wood wrote:
>>ge********@hotmail.com <ge********@hotmail.comwrote: 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
X *= 4
;-)
x=4
:)
Paul Rudin wrote:
Max M <ma**@mxm.dkwrites: ge********@hotmail.com skrev:
Nick Craig-Wood wrote: ge********@hotmail.com <ge********@hotmail.comwrote: 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
X *= 4
;-)
x=4
:)
Ensure x is divisible by *any* non-zero integer:
x = 0
:-O
Jonathan Smith wrote:
<ge********@hotmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
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:
whatever # x is divisible by 4
modulus is your friend :)
-smithj
<pendantic>
It's "modulo"; "modulus" is a different operation.
</pedantic>
MRAB wrote:
>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>
Wikipedia says "modulus may refer to... %, the modulo operator of
various programming languages" http://en.wikipedia.org/wiki/Modulus
That being said, you may be right and it may just be a common mistake.
-smithj This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: FLEB |
last post by:
I'm working on a logon system, something generic and modular, as a part of
a few ideas I have running. I'm just wondering, though, what is the best
way to keep a user logged in authentically...
|
by: Colin Mardell |
last post by:
I need to get VBA to produce a msgbox when the number of records in
the background query is exactly divisible by 5.
|
by: Jake Barnes |
last post by:
Using javascript closures to create singletons to ensure the survival
of a reference to an HTML block when removeChild() may remove the last
reference to the block and thus destory the block is...
|
by: Frederick Gotham |
last post by:
I'm trying to devise a compile-time constant for X, where X is the
greatest number which satisfies both the following criteria:
(1) X <= DESIGNATED_MAX_VALUE
(2) X % Y == 0
I'll try to...
|
by: David |
last post by:
Hi
using C#, .NET 1.1
I want to ensure a particular control is in a certain place in the page...
For example, in the head section, I want to ensure that a literal control of
a certain name...
|
by: Qdiddy34 |
last post by:
Hey Guys, I'm trying figure out how to write a JavaScript for a textbox that contains an integer not divisible by 7.
My aspx form has two textboxes (for integers) and a button (multiply). I have...
|
by: KrazyKoder |
last post by:
Hello,
I have MS Access form that basically allows the user to configure an update statement to a single table via the gui. A requirement is to show the count of records that will be updated...
|
by: kris mynn |
last post by:
i need help on how to write a code that will print numbers 1-100,0000 and the #s are divisible by 7,11, and 13. I was told to start with 1001/7, 1001/11, 1001/13. I just don't get this and need it...
|
by: hsn |
last post by:
i want to store a number which is going to be greater than 0 and <=10^1000
which variable will help me to store this number in it.
after storing it i have to check if it is divisible by another...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |