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 / Mathematical Logic

Author
Message
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 24th Feb 2013 21:42 Edited at: 25th Feb 2013 16:35
Have you ever written a statement like this: if n = 1 then m = 1 else m = 0 and then (assuming both are boolean) felt a bit silly for not writing m = n?
What about if n = 1 then m = 0 else m = 1? Would you feel equally silly (again assuming both are boolean) for not writing it as m = 0-n?
Did you know that you can use the results of relational comparisons to feed into further logic?: if (m>0) and (n=4) then print "Wha!" and that multiplication can be used like the logical AND, and sometimes even reduce the number of operations? (a*b is the same as a and b) Did you know that addition does the same for logical OR?

This is just a tiny peak into the world of mathematical conditions. Sometimes they are faster than conditional statements, sometimes they are slower, but overall I find it a lot of fun to try and condense something that would usually take several lines into one expression. It's become sort of a hobby within a hobby for me. Sometimes you can come up with code that's very useful by doing this, especially if the result of the condition is simply to assign a value.

I'll follow with some examples I think are particularly pleasing:

Perhaps the simplest, but nonetheless elegant, example I can think of is toggling a value between 1 and 0: n = 1-n.

A common problem, probably the first problem you face as a new coder, is to manage key presses; the challenge is usually to extract one instance of a key press from a key being held down. The common solution is called a "trip switch": because it only fires once until it is reset (in this case when the key is released) like a circuit breaker in a fuse box. The usual code is like so:

I used this for a very long time until discovering a mathematical solution: k = keystate(x) * (k + 1). The "+1" is very important here, it's what allows the switch to activate (k=1) in the first place (since on compile k=0), but upon the next test "+1" comes into play again and increments k. As long as key x is being held k will continue to count upwards, and so it can be used as a simple "trip" if k=1 and the incremental values can also be utilised, but when key x is released it pulls the rug under k and wipes its value.

Wrapping a values to stay within a certain range is another fairly common need. For angles we have WRAPVALUE but for different ranges it can be tricky for newcomers. Here's a mathematical solution for ranges with a lower limit of 0:
Let n equal the subject variable, Let b equal the upper limit
n = ((n < 0) + (n / b - int(n / b))) * b

and for any lower limit I added a few extra assignments to help the processor:
Let n equal the subject variable, Let a equal the lower limit, Let b equal the upper limit
n = n - a : b = b - a
n = ((n < 0) + (n / b - int(n / b))) * b + a



Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 25th Feb 2013 03:42 Edited at: 25th Feb 2013 03:47
Quote: " if n = 1 then m = 1 else m = 0"

You can assign boolean results to a variable since they return basically as an integer 0 or 1. (edit: I see you did that below already)

m = (n > 0)


Quote: "if a*b = 0 is the same as if a=0 and b=0"

If a was 42 and b was 0, the first statement would not equal the second. I'm sure you see that, so maybe I misunderstood what you were implying.


Also, shorter condensed code is not always necessarily faster.


Your wrap method can be faster, but it can be slower in some cases. I ran a few tests and found for and number that's less than 3 times the bounding value, the repeat statement worked faster. But obviously, anything greater than that then your method is faster with a consistent time. I didn't try anything with the lower limit.




"You're all wrong. You're all idiots." ~Fluffy Rabbit
WLGfx
16
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 25th Feb 2013 04:09 Edited at: 25th Feb 2013 04:10
In Ansi C the "expr1 ? retTrue : retFalse" comes to mind.

string Answer = ( input == 'y' ? "Hell yeah!" : "Aww heck..." );

http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 25th Feb 2013 04:56
Quote: "and that multiplication can be used like the logical AND, and sometimes even reduce the number of operations? (if a*b = 0 is the same as if a=0 and b=0) Did you know that addition does the same for logical OR?"


isnt this backards, multiplication gives logical OR

a=1
b=0
a*b=0

if either condition is not 0 it will still return true, both must not equal 0 to return false thus if a=0 or b=0

while addition gives logical AND

a=1
b=0
a+b=1

here both must be 0 to return true thus if a=0 and b=0

Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 25th Feb 2013 05:54 Edited at: 25th Feb 2013 05:57
i tend to set toggle = 1 and then go on to use toggle = -toggle in boolean fashion. it's probably a wasteful habit but it is pretty simple/handy.

Virtual Nomad @ California, USA . DBPro V7.7 . Matrix1Utils 05.27.12
AMD Phenomâ„¢ X4 9750 Quad-Core @ 2.4 GHz . 8 GB PC2-6400 RAM
ATI Radeon HD 3650 @ 512 MB . Vista Home Premium 64 Bit
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 25th Feb 2013 06:10 Edited at: 25th Feb 2013 06:13
Quote: "isnt this backards, multiplication gives logical OR "

Not quite. Think about the truth table for AND and truth table for OR



If you multiple p and q you get the equivalent of AND and if you add p and q, you get the equivalent of OR.

Edit- the spacing didn't come out right.

Quote: "What about if n = 1 then m = 0 else m = 1? Would you feel equally silly (again assuming both are boolean) for not writing it as m = 0-n?"

Should that be m = abs(0-n)?

Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 25th Feb 2013 06:25 Edited at: 25th Feb 2013 06:34
Quote: "Not quite. Think about the truth table for AND and truth table for OR"


thats true if you are looking at the result value as the logical return, however the statement given was 'if a*b=0' not 'if a*b'

by testing the result value against 0 it flips

if 1*1=0 gives if 1=0 returns false so if 1=0 or 1=0 returns false
if 1*0=0 gives if 0=0 returns true so if 1=0 or 0=0 returns true
if 0*0=0 gives if 0=0 returns true so if 0=0 or 0=0 returns true

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 25th Feb 2013 07:04
Oh I see what you're saying. I thought we were comparing to 0 and stopping there, not looking at it as a whole. Yep, you're right.

Silverman
17
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 25th Feb 2013 16:26 Edited at: 9th Mar 2013 14:05
hi OBese87,

your equation is interesting, it could also be of interest to users DBClassic.
I often use the way:

for 3 or 4 keys, it is not long to write, but more keys, I can use an equation similar to yours:
k = keystate(x) | (2*(k&1))

there has only 4 results: 0,1,3,2
0 = long released
1 = short press
3 = long press
2 = short released

and the way is easy to put in a function:


DirectX 9.0c (February 2010)/ DBClassic v1.20
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 25th Feb 2013 16:53
Quote: "If a was 42 and b was 0, the first statement would not equal the second. I'm sure you see that, so maybe I misunderstood what you were implying."

You're right, I don't know why I added the "=0", changed.

@Ortu
You are right also!: "a*b = 0" equals "a=0 or b=0". Sorry for the confusion.

@Silverman
(You mean "2 = short release" and "3 = long press".) That's a nice one. It adds the ability to perform an action when the key is released, but sacrifices the counting functionality.


Login to post a reply

Server time is: 2024-05-05 12:43:56
Your offset time is: 2024-05-05 12:43:56