
IsOdd() and IsEven()
-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
IsOdd() and IsEven()
Could these two simple math commands please make it into the 5.10 final release? 

Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!
Re: IsOdd() and IsEven()
i think (Number%2) is faster then IsOdd(Number),
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 more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
-
- Addict
- Posts: 4779
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: IsOdd() and IsEven()
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:MachineCode wrote:Could these two simple math commands please make it into the 5.10 final release?
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)
Re: IsOdd() and IsEven()
Exactly, not even needed. Perhaps it could be done if the PB team have nothing to do?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:
Regards, Little JohnCode: 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)


-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: IsOdd() and IsEven()
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.

Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!
- BasicallyPure
- Enthusiast
- Posts: 539
- Joined: Thu Mar 24, 2011 12:40 am
- Location: Iowa, USA
Re: IsOdd() and IsEven()
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.
B.P.
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)
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
Until you know everything you know nothing, all you have is what you believe.
Re: IsOdd() and IsEven()
Those values are both not false.BasicallyPure wrote:Also, % does not work with negative odd numbers.
-3 % 2 produces -1.
-3 & 1 still prodeces +1.

- BasicallyPure
- Enthusiast
- Posts: 539
- Joined: Thu Mar 24, 2011 12:40 am
- Location: Iowa, USA
Re: IsOdd() and IsEven()
Yes, but -1 is not true either if you define 'true' as = +1, like the constant #True.BasicallyPure wrote:Those values are both not false.

BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
Until you know everything you know nothing, all you have is what you believe.
-
- Addict
- Posts: 4779
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: IsOdd() and IsEven()
It depends on what you are precisely doing with %.BasicallyPure wrote:Also, % does not work with negative odd numbers.
My both functions above of course do work correctly also with any negative arguments.
- Michael Vogel
- Addict
- Posts: 2798
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: IsOdd() and IsEven()
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.
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?)
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
-
- Addict
- Posts: 4779
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: IsOdd() and IsEven()
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:If I had to create a workaround for the missing functionality
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.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

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
-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: IsOdd() and IsEven()
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!
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!

Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!
Re: IsOdd() and IsEven()
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.
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()
Out of curiosity I searched ~100k lines of my code and found 1 call to IsOdd() and 3 calls to IsEven(). 

The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: IsOdd() and IsEven()
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().Little John wrote:these two functions we can very easily write ourselves
Code: Select all
Debug Sqr(9) ; Why should Sqr() exist when Pow() can do the job?
Debug Pow(9,0.5)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!