November 28, 2020, 02:06:42 PM

### Author Topic: [bb] Another IsOdd() function. by dangerdave [ 1+ years ago ]  (Read 791 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Another IsOdd() function. by dangerdave [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Another IsOdd() function.
Author : dangerdave
Posted : 1+ years ago

Description : I put this here not to snub Edzup's post, but show that there is usually more than one way to do something.

All that is needed to see if a number is odd is check the the lowest bit of the number:
result = number And 1
--------------------------------
In fact, I would just inline it instead of having all of the function overhead.
Example:
If IsOdd(number) then ...
would be:
If (number And 1) then ...

Code :
Code: BlitzBasic
1. print IsOdd(97543)
2.
3. function IsOdd(x)
4.   return x And 1
5. end function

Genexi2(Posted 1+ years ago)

Either its me or Blitz doesnt use "And" as a math operator at all, and instead for statement usage.Anyway, was bored and came up with another way of doing it :(decided to use select since they're faster than mod)
Code: [Select]
`Print IsOdd(975471)Function IsOdd(x) y\$ = Right(Str(x),1) Select y Case 0 : Return 0 Case 1 : Return 1 Case 2 : Return 0 Case 3 : Return 1 Case 4 : Return 0 Case 5 : Return 1 Case 6 : Return 0 Case 7 : Return 1 Case 8 : Return 0 Case 9 : Return 1 End Select End FunctionWaitKey()`

Michael Reitzenstein(Posted 1+ years ago)

Your function is both longwinded and an order of magnitude slower, Genexi. And is a (single) native CPU operation, string operations are extremely slow in comparison.

Rob Farley(Posted 1+ years ago)

And another one... OK, not elegant either, but stressing the point of several ways of doing the same thing.
Code: [Select]
`Print IsOdd(97541)Function IsOdd(x)  Return Floor((x+1)/2)-Floor(x/2)End Function`

Michael Reitzenstein(Posted 1+ years ago)

<div class="quote"> OK, not elegant either, but stressing the point of several ways of doing the same thing. </div>Yeah, there are often multiple ways to do the same thing, except in this case one of them is so superior to the others, it isn't funny!

Michael Reitzenstein(Posted 1+ years ago)

<div class="quote"> OK, not elegant either, but stressing the point of several ways of doing the same thing. </div>Yeah, there are often multiple ways to do the same thing, except in this case one of them is so superior to the others, it isn't funny!

Dreamora(Posted 1+ years ago)

It's both far more work than needed in the worst case:
Code: [Select]
`Function IsOdd(x)  return x mod 2end function`

Floyd(Posted 1+ years ago)

(x And 1) is clearly the preferred method.The one with strings is extremely inefficient and the one with Floor is just plain wrong.

Michael Reitzenstein(Posted 1+ years ago)

Dreamora - that one does *exactly* the same thing as the original function, but I believe And will be significantly faster (if Blitz doesn't optimise the const Mod into an And).

Dreamora(Posted 1+ years ago)

I knowBut it's a very good method for other stuff too, not only the even-odd check.But it needs some math knowledge to find out how usefull and powerfull mod really is ...And is very usefull if you use bitflags. for stuff like that there is no more efficient way

Michael Reitzenstein(Posted 1+ years ago)

<div class="quote"> And is very usefull if you use bitflags. for stuff like that there is no more efficient way </div>What, If Var And %10 doesn't work?

Dreamora(Posted 1+ years ago)

then you can use connected mod checks :)not (Var mod 2) and not (Var mod 4) ...or you copy the flag and use shr / shl to check the flag

Bug Face(Posted 1+ years ago)

Sorry but mod is a very slow way of doing this. The fastest way of achieving the odd is not to do it in a fucntion at all but in line it, and when it is a one-liner, why wouldn't you?Of course for bitwise AND has the disadvantage that it can only mask to powers of 2, where as mod does it with anything. Let's just take a moment to remember that a mod is calculated by determining the remainder of a division. I would take steps to organize my data so I didn't have to use a mod and could do it with a bitwise operation. I have to give points to the guy who wrote the string one for the most over implemented code I have ever seen. So! In the spirit of optimisation and paying homage to someone who clearly works for microsoft (:>) I have optimised it ;-)
Code: [Select]
`Function isOdd(x) Select Right(Str(x),1) Case 0,2,4,6,8 : Return 0 Case 1,3,5,7,9 : Return 1 End SelectEnd Function`I have a taste for this now! This would work ;-)
Code: [Select]
`Fucntion isEven(x)       return (Not x) and 1 End FunctionFunction isOdd(x)       return Not isEven(x)End Function`

Shambler(Posted 1+ years ago)

Another useful microsoft function...
Code: [Select]
`Function PlusOne(x)return x+1End Function`

Floyd(Posted 1+ years ago)

And here's the power of leveraged code.I've always wanted a PlusTwo function. Now I don't have to worry about the implementation details.
Code: [Select]
`Function PlusTwo(x) Return PlusOne( PlusOne( x ) ) End Function`

AntonyWells(Posted 1+ years ago)

And here's the safe mode version of microsoft's entry into this pioneering new field.
Code: [Select]
`Function SafeModeIsOdd_COM_DX_API_OBJECT_ANNOYING_WORDS( value#=1,isValueANumber=possibly)     if not isValueANumber=true           return MS_THIS_IS_NOT_A_NUMBER     else           ask=input(value+"< What is the fraction>")           return ask     endifend function`The MS motto,If in doubt, ask the user.

AntonyWells(Posted 1+ years ago)

-Double Post.

Bug Face(Posted 1+ years ago)

I cannot thank the later posters enough... In the interests of code re-use, I have managed to remove an additional isEven function from my ealier post
Code: [Select]
`Function PlusOne(x)return x+1End FunctionFunction isOdd(x)    return (Not PlusOne(x)) And 1End Function`

Michael Reitzenstein(Posted 1+ years ago)

Actually, that would be:
Code: [Select]
`Function PlusOne(x)Return x+1End FunctionFunction isOdd(x)    Return (~PlusOne(x)) And 1End Function`

Michael Reitzenstein(Posted 1+ years ago)

Actually, that would be:
Code: [Select]
`Function PlusOne(x)Return x+1End FunctionFunction isOdd(x)    Return (~PlusOne(x)) And 1End Function`

jfk EO-11110(Posted 1+ years ago)

Funny thread, anywayPrint IsOdd(97541)The calling of a function that forces the use of a stack with Return adress, Parameter and Return-value handover is such an overhead.When you are a Hardcore Coder then you will recognize something likeprint (97541 and 1)as isOdd all the time.

aindovin(Posted 1+ years ago)

=) i'm happy because i think i came up with a fun new way of doing this.Print isodd(15)               ; test of an odd numberPrint isodd(10)               ; test of an even numberprint isodd(97541)            ; test of your number?Function isodd(value)         ;  our odd testing function     testnum%=((-1)^(value))  ; the way this works is we put -1 to our value's power. -1 x 1 = -1 // -1 x 2 = 1 =D OMG THATS INSANE HOW THAT WORKS.Return testnum%               ;End Function ;

n8r2k(Posted 1+ years ago)

<a href="../Community/postscdd2-2.html?topic=44481#495317" target="_blank">http://www.blitzbasic.com/Community/posts.php?topic=44481#495317[/url] I asked a question on this subject a year ago. There are some other ways in there to test for odd/evenness (is that a word)

mm(Posted 1+ years ago)

even this entry is odd

WendellM(Posted 1+ years ago)

<div class="quote"> The calling of a function that forces the use of a stack with Return adress, Parameter and Return-value handover is such an overhead. </div>That is true.  In tests, it takes three times as long to do the "And" test in a function compared to doing the test inline.  Of course, we're talking 6 millisecs for a million tests compared to 2 millisecs, so it doesn't really matter, but I'll conveniently ignore that... <div class="quote"> When you are a Hardcore Coder then you will recognize something likeprint (97541 and 1)as isOdd all the time.  </div>This can be made easier by using a const:
Code: [Select]
`Const IsOdd = 1Print (97541 And IsOdd)`At least that hints at what the And is doing.  Think of it as the abbreviated (speedo) version of a function (swim trunks): not as comfortable for observers to look at, but faster, and still better than seeing what lies underneath directly.But if you prefer slowness and deliberate obfuscation, I think aindovin's use of exponentiation wins.

n8r2k(Posted 1+ years ago)