Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / A function that can tell if a variable is an odd or even number

Author
Message
JoeOh
17
Years of Service
User Offline
Joined: 25th Feb 2009
Location:
Posted: 27th Jan 2014 14:09
I know that Qbasic had the MOD function. But I guess Darkbasic Pro doesn't have something similar. I know that dividing any number by 2 will give a xx.5 or xx.0 showing it's odd or even. But I need the function to return a 1 for even, 0 for odd.

Not sure how to crack this one.

Any clues?

Home is where my souped-up computer is...
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 27th Jan 2014 14:19 Edited at: 27th Jan 2014 14:29
First of all, there is a mod operator, and it's called mod.



And if there weren't one, you seem to be on the right track and could just do something like this:



In that case (num/2.0) is a proper float division (resulting in 3.5 for num = 7 for instance) and (num/2) is an integer division (7/2 = 3). Alternatively you could write it like this:



If you stick to integers as possible input, you could also use bit masks instead of mod (might be faster).




But I'd rather stick to the first solution.

Edit: Actually this would be somewhat easier than the int division method above:



But again, this might only work for positive integers.

JoeOh
17
Years of Service
User Offline
Joined: 25th Feb 2009
Location:
Posted: 27th Jan 2014 15:06
I guess I'm blind then. I looked up and down the core command list. Maybe i should have looked in the math section.

duhhhh.

Thanks again-

Home is where my souped-up computer is...
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Jan 2014 02:55
I don't believe DB lists the MOD command in the help files and it doesn't highlight it up as a keyword in the IDE either. But it's there. And you can editor the syntax file so that MOD does get highlighted.

chafari
Valued Member
20
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 28th Jan 2014 14:48
Hi there.
Quote: "But I need the function to return a 1 for even, 0 for odd."


You can easily make a function with this code:



I'm not a grumpy grandpa
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 29th Jan 2014 01:45
Quote: "I don't believe DB lists the MOD command in the help files "


I guess it doesn't because it's a math operator like +-/*^ etc not a command. You'll find it described in the Operators section of the Help file under Principles as it should be.



Powered by Free Banners
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th Jan 2014 08:54
I don't know why Lee just didn't make it % like the other operators.

ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 29th Jan 2014 11:58
Quote: "I don't know why Lee just didn't make it % like the other operators."


I guess because % is already reserved for binary numbers (%1011011 etc). Also, since DBP seems to target beginners (not exclusively but among others), using "mod" was just more intuitive than "%", which might be standard in almost all other programming languages, but somebody who hasn't seen it before would surely wonder why.
Well, I personally would prefer % instead of mod as well, but life's hard I guess.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 29th Jan 2014 12:56
I prefer mod because it corresponds to the way I was taught maths so is more natural to me. Of course such analogies can't be applied consistently - take ^ and * for example. Regarding the command versus operator issue, mod is implemented as a function in some languages i.e. as x = mod(y,5).

As usual with any language it pays to browse the Help files from time to time - even when you think you know the language inside out. You usually discover something new or something you'd forgotten about. Much the same applies to browsing dictionaries for new words I suppose.



Powered by Free Banners
MonoCoder
20
Years of Service
User Offline
Joined: 4th Dec 2005
Location: england
Posted: 29th Jan 2014 13:43
DBP lacks % for mod, but it does have %%:

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th Jan 2014 17:22
Quote: "mod is implemented as a function in some languages i.e. as x = mod(y,5)"

Yea I've run into that, it's always annoyed me. Or instead of doing something simple like 2^3 I'd have to write Math.pow(2,3)

Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 30th Jan 2014 15:34 Edited at: 30th Jan 2014 15:38
My solution is similar to Chafari's...



Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Jan 2014 17:05 Edited at: 30th Jan 2014 17:25
@Monocoder (<- edit for clarity)

Now try mod 3.



Powered by Free Banners
MonoCoder
20
Years of Service
User Offline
Joined: 4th Dec 2005
Location: england
Posted: 30th Jan 2014 18:47 Edited at: 30th Jan 2014 18:53


?

(mod and %% give the same result in dbp v1.071)


edit: I used 2 as an example just because it gives the even-or-odd result.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Jan 2014 19:04
@MonoCoder

I stand corrected. Thanks.

I prefer mod because I know what it means whereas %% doesn't mean anything to me - and I can't see it in the Help file either. I assumed %% was some kind of bitwise operator but didn't take the trouble to check.



Powered by Free Banners

Login to post a reply

Server time is: 2026-07-07 01:53:08
Your offset time is: 2026-07-07 01:53:08