IsOdd() and IsEven()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

IsOdd() and IsEven()

Post by MachineCode »

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!
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: IsOdd() and IsEven()

Post by STARGÅTE »

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 moreTypeface - Sprite-based font include/module
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: IsOdd() and IsEven()

Post 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
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: IsOdd() and IsEven()

Post 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:
Image
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: IsOdd() and IsEven()

Post 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.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: IsOdd() and IsEven()

Post 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.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
Demivec
Addict
Addict
Posts: 4261
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: IsOdd() and IsEven()

Post 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. :)
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: IsOdd() and IsEven()

Post 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. :)
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: IsOdd() and IsEven()

Post 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.
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: IsOdd() and IsEven()

Post 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?)
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: IsOdd() and IsEven()

Post 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
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: IsOdd() and IsEven()

Post 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! :)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: IsOdd() and IsEven()

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: IsOdd() and IsEven()

Post by skywalk »

Out of curiosity I searched ~100k lines of my code and found 1 call to IsOdd() and 3 calls to IsEven(). :shock:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: IsOdd() and IsEven()

Post 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)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply