odd(x) and even(x)

Share your advanced PureBasic knowledge/code with the community.
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

odd(x) and even(x)

Post by jesperbrannmark »

I have been missing odd and even, so here they are:

Code: Select all

Macro odd(a)
  Not Int(a/2)*2=a
EndMacro
Macro even(a)
  Int(a/2)*2=a
EndMacro  

For a=1 To 10
  If odd(a)
    Debug Str(a)+" is odd number."
  ElseIf even(a)
    Debug Str(a)+" is even number."
  EndIf
Next
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: odd(x) and even(x)

Post by STARGÅTE »

:?

not good syntax, it is easier to check the first bit, then you can use it in terms too

Code: Select all

Macro odd(a)
  ((a)&1)
EndMacro
Macro even(a)
  (1-(a)&1)
EndMacro  
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Re: odd(x) and even(x)

Post by einander »

These macros fail if you send a float to them.
Procedures do the same task converting previously the floats to integers.

Code: Select all

Procedure Odd(N)
   ProcedureReturn 1&N
EndProcedure 

Procedure Even(N)
   ProcedureReturn 1-1&N
EndProcedure
Cheers!
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: odd(x) and even(x)

Post by Guimauve »

Hello everyone,

Ok this is my IsOddNumber() / IsEvenNumber() testing instruction.

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : IsOddNumber, IsEvenNumber
; File Name : Is Odd-Even Number.pb.pb
; File version: 1.0.0
; Programmation : OK
; Programmed by : Guimauve
; Date : 13-04-2011
; Mise à jour : 13-04-2011
; Code PureBasic : 4.60
; Plateform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure.b IsOddNumber(Number.l) 
  
  If Number % 2
    IsOdd.b = #True 
  Else
    IsOdd = #False 
  EndIf 
  
  ProcedureReturn IsOdd
EndProcedure 

Procedure.b IsEvenNumber(Number.l) 
  
  If Number % 2
    IsEven.b = #False 
  Else
    IsEven = #True 
  EndIf 
  
  ProcedureReturn IsEven
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
; <<<<< !!! WARNING - YOU ARE NOW IN A TESTING ZONE - WARNING !!! <<<<< 
; <<<<< !!! WARNING - THIS CODE SHOULD BE COMMENTED - WARNING !!! <<<<< 
; <<<<< !!! WARNING - BEFORE THE FINAL COMPILATION. - WARNING !!! <<<<< 
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 

For Number = 0 To 15
  If IsOddNumber(Number)  
    Debug Str(Number) + " is odd number." 
  ElseIf IsEvenNumber(Number) 
    Debug Str(Number) + " is even number." 
  EndIf 
Next 

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Best regards.
Guimauve
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: odd(x) and even(x)

Post by wilbert »

einander wrote:These macros fail if you send a float to them.
Procedures do the same task converting previously the floats to integers.

Code: Select all

Procedure Odd(N)
   ProcedureReturn 1&N
EndProcedure 

Procedure Even(N)
   ProcedureReturn 1-1&N
EndProcedure
Cheers!
A bit faster approach for Even

Code: Select all

Procedure Even(N)
  ProcedureReturn 1&~N
EndProcedure
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: odd(x) and even(x)

Post by Psychophanta »

or still faster

Code: Select all

Macro Even(n)
  (~n#&1)
EndMacro
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: odd(x) and even(x)

Post by Shield »

Never forget putting parenthesis around macro parameters. :wink:

Code: Select all

Macro Even(n)
  (~(n) & 1)
EndMacro
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: odd(x) and even(x)

Post by Psychophanta »

Shield wrote:Never forget putting parenthesis around macro parameters. :wink:

Code: Select all

Macro Even(n)
  (~(n) & 1)
EndMacro
Not needed at all
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: odd(x) and even(x)

Post by netmaestro »

Never? There's nothing wrong with Psychophanta's original macro.
BERESHEIT
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: odd(x) and even(x)

Post by skywalk »

Better safe than sorry. If you pass an argument into the macro, the parentheses preserve the order of operations. :wink:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: odd(x) and even(x)

Post by Psychophanta »

skywalk wrote:Better safe than sorry. If you pass an argument into the macro, the parentheses preserve the order of operations. :wink:
There is not safer to put parentheses there. The order of operations will be the same always in cases like that.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: odd(x) and even(x)

Post by skywalk »

Really :?: I have too many Macros to check for order. So I default to ()'s around the passed parameters. :wink:

Code: Select all

Macro IsEven(i)
  ~(i) & 1
EndMacro
Macro IsEvennp(i)
  ~i & 1
EndMacro
Macro IsEvenps(i)
  (~i#&1)
EndMacro

Debug -22 + -24
Debug IsEven(-22 + -24)
Debug IsEvennp(-22 + -24)
Debug IsEvenps(-22 + -24)

Debug -22 + -23
Debug IsEven(-22 + -23)
Debug IsEvennp(-22 + -23)
Debug IsEvenps(-22 + -23)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: odd(x) and even(x)

Post by Shield »

Psychophanta wrote:
skywalk wrote:Better safe than sorry. If you pass an argument into the macro, the parentheses preserve the order of operations. :wink:
There is not safer to put parentheses there. The order of operations will be the same always in cases like that.
Is it?

Code: Select all

Macro Even(n)
  (~n#&1)
EndMacro
Macro EvenShield(n)
  (~(n) & 1)
EndMacro


If Even(10 + 5)
	Debug Str(10 + 5) + " is even."
Else
	Debug Str(10 + 5) + " is NOT even."
EndIf

If EvenShield(10 + 5)
	Debug Str(10 + 5) + " is even."
Else
	Debug Str(10 + 5) + " is NOT even."
EndIf
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: odd(x) and even(x)

Post by Psychophanta »

Aha! right! :?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply