Page 1 of 2

IsOdd() and IsEven()

Posted: Sat Jan 12, 2013 3:32 pm
by MachineCode
Could these two simple math commands please make it into the 5.10 final release? :)

Re: IsOdd() and IsEven()

Posted: Sat Jan 12, 2013 3:52 pm
by STARGĂ…TE
i think (Number%2) is faster then IsOdd(Number),

Re: IsOdd() and IsEven()

Posted: Sat Jan 12, 2013 4:02 pm
by Little John
MachineCode wrote:Could these two simple math commands please make it into the 5.10 final release? :)
Sorry, I don't think that it's a good idea to build anything and everything into the language. Many other math stuff is not built into PB as well, and especially these two functions we can very easily write ourselves:

Code: Select all

; PB 5.10+

Procedure.i IsOdd (x.i)
   ProcedureReturn Bool(x%2 <> 0)
EndProcedure

Procedure.i IsEven (x.i)
   ProcedureReturn Bool(x%2 = 0)
EndProcedure


;-- Demo
Debug IsOdd(1)
Debug IsEven(1)
Debug IsOdd(2)
Debug IsEven(2)
Regards, Little John

Re: IsOdd() and IsEven()

Posted: Sat Jan 12, 2013 4:16 pm
by moogle
Little John wrote:Sorry, I don't think that it's a good idea to build anything and everything into the language. Many other math stuff is not built into PB as well, and especially these two functions we can very easily write ourselves:

Code: Select all

; PB 5.10+

Procedure.i IsOdd (x.i)
   ProcedureReturn Bool(x%2 <> 0)
EndProcedure

Procedure.i IsEven (x.i)
   ProcedureReturn Bool(x%2 = 0)
EndProcedure


;-- Demo
Debug IsOdd(1)
Debug IsEven(1)
Debug IsOdd(2)
Debug IsEven(2)
Regards, Little John
Exactly, not even needed. Perhaps it could be done if the PB team have nothing to do? :lol:

Re: IsOdd() and IsEven()

Posted: Sat Jan 12, 2013 4:46 pm
by MachineCode
I only brought it up because it's been asked before by many others (not just myself), and since this is the "granted wishes" release and it's not in there... ya know? :) Just hoping it hasn't been forgotten.

Re: IsOdd() and IsEven()

Posted: Sat Jan 12, 2013 6:27 pm
by BasicallyPure
Hi,
This can also be done with bitwise And.
Maybe faster than the % division.
Also, % does not work with negative odd numbers.
-3 % 2 produces -1.
-3 & 1 still prodeces +1.

Code: Select all

; isOdd
; returns 1 (True) if 'n' is odd
; returns 0 (False) if 'n' is even

Procedure isOdd(n.i)
   ProcedureReturn n & 1
EndProcedure

Debug isOdd(9)
Debug isOdd(-358128567)
Debug isOdd(8)
Debug isOdd(-498379896)
B.P.

Re: IsOdd() and IsEven()

Posted: Sat Jan 12, 2013 6:56 pm
by Demivec
BasicallyPure wrote:Also, % does not work with negative odd numbers.
-3 % 2 produces -1.
-3 & 1 still prodeces +1.
Those values are both not false. :)

Re: IsOdd() and IsEven()

Posted: Sat Jan 12, 2013 7:06 pm
by BasicallyPure
BasicallyPure wrote:Those values are both not false.
Yes, but -1 is not true either if you define 'true' as = +1, like the constant #True. :)

Re: IsOdd() and IsEven()

Posted: Sat Jan 12, 2013 7:19 pm
by Little John
BasicallyPure wrote:Also, % does not work with negative odd numbers.
It depends on what you are precisely doing with %.
My both functions above of course do work correctly also with any negative arguments.

Re: IsOdd() and IsEven()

Posted: Sun Jan 13, 2013 10:10 am
by Michael Vogel
If I had to create a workaround for the missing functionality, I would have used &1 and also macros to be in touch with the speed of an internal function.

Code: Select all

Macro Odd(n) : (n&1) : EndMacro
Macro Even(n) : ((~n)&1) : EndMacro

For i=-3 To 3
	Debug Str(i)+" E=" +Str(Even(i))+ " O="+Str(Odd(i))
Next i
Anyway I would like to get tons of new (math) functions in PureBasic to force standardized code. Ootherwise you will see things like IsOdd(), Odd(), NotEven(), UnEven() etc. for the same odd function (and you can't be sure, if these handmade functions will all do the same, isn't it #True?)

Re: IsOdd() and IsEven()

Posted: Sun Jan 13, 2013 12:00 pm
by Little John
Michael Vogel wrote:If I had to create a workaround for the missing functionality
There is no "missing functionality" in this respect. We can easily check whether a given integer is even or odd (as you did with your Macros).
Michael Vogel wrote:Anyway I would like to get tons of new (math) functions in PureBasic to force standardized code. Ootherwise you will see things like IsOdd(), Odd(), NotEven(), UnEven() etc. for the same odd function
After more than 20 years of computer programming, reading programming related forums, and having read literally Gigabytes of source code written by other people, I can't remember having ever seen all those "IsOdd(), Odd(), NotEven(), UnEven()" functions. It seems to me that you are proposing a solution for which there is no problem. :-)

Of course, there are math functions in PB for a good reason. And IMHO it would actually make sense to add some more functions, say e.g. BinomialCoefficient(n,k). But generally speaking, there are very many math functions. We have to live with the fact, that not all math functions can be implemented in PB (or any other programming language). And this is not even necessary, because the programming language provides us with everything that is required to write functions which we need ourselves. This means, it makes sense to build only the most important functions into a language.

IsOdd() and IsEven() are not important functions. If those functions should be built into PB, then for the same reason there must also be IsPositive(), IsNegative(), and whatever ... This is just inflation of built-in functions, which does not add any new functionality, but only "blows up" the set of built-in function names. Then the PB team has to write the documentation for those functions etc. All this would take the PB team lots of time, which would mean spending their working hours very inefficiently. I'm confident that they are not going to do so. :-)

Regards, Little John

Re: IsOdd() and IsEven()

Posted: Sun Jan 13, 2013 4:32 pm
by MachineCode
Relax, everyone. It just seemed a legit request since the commands are in Droopy's Lib (thus showing a need) and also because other people have also expressed a need for them:

http://www.purebasic.fr/english/viewtop ... 12&t=48981
http://www.purebasic.fr/english/viewtop ... 13&t=24865
http://www.purebasic.fr/english/viewtop ... 12&t=14583
http://www.purebasic.fr/english/viewtop ... 13&t=33699

So, the wishes are there. It's not just me asking! :)

Re: IsOdd() and IsEven()

Posted: Sun Jan 13, 2013 5:27 pm
by Tenaja
There are many things like this that wold "only" take Fred maybe 10 minutes to implement...

Since most of them are done in Macros, maybe somebody can start a "standard" PB Macro library for "all of us" to use. Of course, that would not fix the issues with naive readers not finding the file.

Re: IsOdd() and IsEven()

Posted: Sun Jan 13, 2013 6:50 pm
by skywalk
Out of curiosity I searched ~100k lines of my code and found 1 call to IsOdd() and 3 calls to IsEven(). :shock:

Re: IsOdd() and IsEven()

Posted: Mon Jan 14, 2013 3:30 am
by MachineCode
Little John wrote:these two functions we can very easily write ourselves
Oh please, that's no argument. But let's take Sqr() as an example. Why is that command even in the language when we can just as easily use the Pow() command instead? The answer is obvious: Sqr() is there for our convenience and readability, as would be IsOdd() and IsEven().

Code: Select all

Debug Sqr(9) ; Why should Sqr() exist when Pow() can do the job?
Debug Pow(9,0.5)