View previous topic :: View next topic |
Author |
Message |
TheRock
Joined: 21 May 2007 Posts: 28 Location: The Old Dominion, USA
|
Posted: Sat Sep 01, 2007 2:45 am Post subject: Easy way to tell if a number is even or odd? |
|
|
I searched through the help file without any success; I was hoping there was/is and easy way to tell if a integer is even or odd.
Select case doesn't seem to do it, and an If-Then doesn't accept 'is even' either.
I'm open to ideas.
Thanks,
TR |
|
Back to top |
|
|
jenalcom
Joined: 10 Apr 2004 Posts: 365 Location: Perth, Western Australia
|
Posted: Sat Sep 01, 2007 3:03 am Post subject: |
|
|
You could always use the mod function.
eg
temp = myvariable mod 2
if temp = 1 .... ' myvariable is odd
if temp = 0 .... ' myvariable is even
mod gets the remainder after dividing the variable by the stated number. In this case the number is 2, so mod can only return 1 or 0.
Alan |
|
Back to top |
|
|
DToolan
Joined: 14 Aug 2004 Posts: 1384 Location: Dallas / Fort Worth, Texas (USA)
|
Posted: Sat Sep 01, 2007 3:46 am Post subject: |
|
|
Code: | IF MyInteger.0 = 1 THEN
'odd number
ELSE
'even number
ENDIF |
|
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Sat Sep 01, 2007 9:07 am Post subject: |
|
|
Hi,
If you variable is a "simple number" like a byte, word etc, couldn't you just look at bit 0. If it's set then the number is odd.
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
Luciano
Joined: 29 Nov 2004 Posts: 3149 Location: Italy
|
Posted: Sat Sep 01, 2007 11:21 am Post subject: |
|
|
Hi,
Run this code in the Bascom simulator and you will see
that the least significant bit will tell you if the number
is even or odd. This is the demonstration that what
suggested by DToolan works with signed integers.
Best regards,
Luciano
Code: | $regfile = "m16def.dat"
$crystal = 4000000
$hwstack = 32
$swstack = 16
$framesize = 40
Dim MyInteger as Integer
Dim I as Byte
MyInteger = 11
For I = 1 to 21
MyInteger = MyInteger - 1
IF MyInteger.0 = 1 THEN
Print MyInteger ; " is odd"
ELSE
Print MyInteger ; " is even"
ENDIF
Next I
End |
The output in the simulator:
Code: | 10 is even
9 is odd
8 is even
7 is odd
6 is even
5 is odd
4 is even
3 is odd
2 is even
1 is odd
0 is even
-1 is odd
-2 is even
-3 is odd
-4 is even
-5 is odd
-6 is even
-7 is odd
-8 is even
-9 is odd
-10 is even |
|
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Sat Sep 01, 2007 11:28 am Post subject: |
|
|
Hi Luciano,
Thanks for explaining what I've just said.
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
Luciano
Joined: 29 Nov 2004 Posts: 3149 Location: Italy
|
Posted: Sat Sep 01, 2007 1:22 pm Post subject: |
|
|
Hi Ian,
The original question was about signed integers and DToolan has provided an
example with integers. Your post has explained to test bit 0, but has not clearly
stated that this technique works with signed data types like INTERGER and LONG
as well as unsigned data types like BYTE and WORD.
Note that you can store "simple numbers" (1, 2, 3) in any numeric data type, but
you won't know if the number is even or odd if you test the least significant bit
of a SINGLE or a DOUBLE.
Best regards,
Luciano |
|
Back to top |
|
|
eddy
Joined: 09 Apr 2004 Posts: 67 Location: Near Chicago, IL USA
|
Posted: Sat Sep 01, 2007 8:52 pm Post subject: |
|
|
Hi Luciano,
You are correct, you cannot determine even or odd by checking bit 0 of a single/double. But singles and doubles are fractional and only whole numbers can be even or odd. So, it isn't really valid to test for even/odd for a single/double without first determining the integer value (or converting to integer).
Just clarifying so someone doesn't try to work with singles & doubles as even or odd.
-Eddy _________________ Eddy Wright
Wright Hobbies, LLC
http://www.wrighthobbies.net |
|
Back to top |
|
|
Luciano
Joined: 29 Nov 2004 Posts: 3149 Location: Italy
|
Posted: Sun Sep 02, 2007 8:03 am Post subject: |
|
|
Hi Eddy,
For some mathematicians a number having fractional part of zero is a whole number.
http://mathworld.wolfram.com/WholeNumber.html
* * *
The data type SINGLE and DOUBLE:
A SINGLE or a DOUBLE can be used to represent a whole number. Of course this is
working only with small numbers because of the resolution of these data types.
If you use SINGLE or DOUBLE in your Bascom program you will use more program
space, more RAM and the program will be slow. Using SINGLE or DOUBLE while
working with whole numbers is a unnecessary waste of resources.
Best regards,
Luciano |
|
Back to top |
|
|
eddy
Joined: 09 Apr 2004 Posts: 67 Location: Near Chicago, IL USA
|
Posted: Sun Sep 02, 2007 3:42 pm Post subject: |
|
|
Hi Luciano,
I was going by the more common definition of even and odd:
"The formal definition of an odd number is an integer of the form n=2k +1, where k is an integer. The definition of an even number is n=2k where k is an integer." - From Wikipedia
I have found that sometimes people do use single/double to represent whole numbers without realizing the precision errors that can accumulate and throw off their calculations. They were use to the PC world where .1 +.1 actually equals .2!
I usually find creative ways to avoid using floating point math. Integer math is so much faster...
-Eddy _________________ Eddy Wright
Wright Hobbies, LLC
http://www.wrighthobbies.net |
|
Back to top |
|
|
|