Page 1 of 1
odd(x) and even(x)
Posted: Wed Jan 25, 2012 2:32 pm
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
Re: odd(x) and even(x)
Posted: Wed Jan 25, 2012 2:43 pm
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
Re: odd(x) and even(x)
Posted: Wed Jan 25, 2012 3:34 pm
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!
Re: odd(x) and even(x)
Posted: Wed Jan 25, 2012 4:15 pm
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
Re: odd(x) and even(x)
Posted: Wed Jan 25, 2012 5:43 pm
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
Re: odd(x) and even(x)
Posted: Fri Jan 27, 2012 8:54 pm
by Psychophanta
Re: odd(x) and even(x)
Posted: Fri Jan 27, 2012 9:09 pm
by Shield
Never forget putting parenthesis around macro parameters.
Re: odd(x) and even(x)
Posted: Fri Jan 27, 2012 9:27 pm
by Psychophanta
Shield wrote:Never forget putting parenthesis around macro parameters.
Not needed at all
Re: odd(x) and even(x)
Posted: Fri Jan 27, 2012 9:29 pm
by netmaestro
Never? There's nothing wrong with Psychophanta's original macro.
Re: odd(x) and even(x)
Posted: Fri Jan 27, 2012 9:36 pm
by skywalk
Better safe than sorry. If you pass an argument into the macro, the parentheses preserve the order of operations.

Re: odd(x) and even(x)
Posted: Fri Jan 27, 2012 9:39 pm
by Psychophanta
skywalk wrote:Better safe than sorry. If you pass an argument into the macro, the parentheses preserve the order of operations.

There is not safer to put parentheses there. The order of operations will be the same always in cases like that.
Re: odd(x) and even(x)
Posted: Fri Jan 27, 2012 9:57 pm
by skywalk
Really

I have too many Macros to check for order. So I default to ()'s around the passed parameters.
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)
Re: odd(x) and even(x)
Posted: Fri Jan 27, 2012 9:58 pm
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.

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
Re: odd(x) and even(x)
Posted: Fri Jan 27, 2012 10:28 pm
by Psychophanta
Aha! right!
