How to tell if a number is odd

  • Thread starter Thread starter Jasonm
  • Start date Start date
J

Jasonm

This seems like it should be a very simple matter, but I canot find a
function or an easy mathematical method to determine if a number is odd or
even.

More specifically I need to determine if a number in format X.XX between
0.40 and 3.00 is odd or even.

Does anyone have any ideas? I really feel as though I am missing something
stupid!

Thanks, Jasonm
 
Hi Jasonm,

The simple way to tell if a number is odd or even is to use the modulo
function, which returns the remainder in a division... modulo in Access is a
backslash, \

eg: 3\2 returns 1
2\2 returns 0

Do the modulo, and if it's 1, you know it's an odd number.

Hope this helps!!

Damian.

ps: Naturally, odd or even only matters if you have a whole number...
 
Mod gives the remainder after division.
So the number is odd if Mod 2 returns 1.

In query design:
MyFieldIsOdd: (Int([MyField]) Mod 2 = 1)

Note that this ignores the fraction part, and looks at the integer only.

If it does not give the results you want for negative numbers, replace Int()
with Fix().
 
Allen Thanks for the reply, I had found a similar post of yours and had
tried unsucessfully to impliment it before posting this question. The
problem that I had was that my number could be a 0.7 and the Mod function
rounds to integers!

I ended up rounding to the correct number of decimals, multiplying by 10 (or
100 as the decimal demanded) to dreate an integer and then used the mod
function. This seems to have solved my problem... I will do some testing
over the next fee days to see if I can get it to fail, but I am not thinking
that it will!

Thanks very much!

Jasonm
Allen Browne said:
Mod gives the remainder after division.
So the number is odd if Mod 2 returns 1.

In query design:
MyFieldIsOdd: (Int([MyField]) Mod 2 = 1)

Note that this ignores the fraction part, and looks at the integer only.

If it does not give the results you want for negative numbers, replace
Int() with Fix().

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Jasonm said:
This seems like it should be a very simple matter, but I canot find a
function or an easy mathematical method to determine if a number is odd
or even.

More specifically I need to determine if a number in format X.XX between
0.40 and 3.00 is odd or even.

Does anyone have any ideas? I really feel as though I am missing
something stupid!
 
Thanks for your reply! As I said in my reply to Allen the trouble that I had
was that my input could be a 0.7 and I needed to be able to make that 0.6,
so I had to address the decimal.

What I ended up doing is rounding the input to the correct number of decimal
places, multiplying by 10 and then using the mod function on the integer
that remained. This seems to have solved my problem.

Thank-you very much for your prompt reply! I really appreciate everyones
help here!

Jasonm
 
Damian said:
Hi Jasonm,

The simple way to tell if a number is odd or even is to use the modulo
function, which returns the remainder in a division... modulo in
Access is a backslash, \

eg: 3\2 returns 1
2\2 returns 0

Do the modulo, and if it's 1, you know it's an odd number.

Hope this helps!!

Actually the \ operator does integer division. 7\3 = 2

To find the remainder use Mod...

3 mod 2 = 1
4 mod 2 = 0
 
The real problem here is that the definition of an "even" number,
mathematically speaking, is an INTEGER that 2 divides into EVENLY, and that's
not what you're trying to obtain!

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via AccessMonster.com
 

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

Back
Top